-
Notifications
You must be signed in to change notification settings - Fork 13
/
serve-client-form.js
77 lines (57 loc) · 2.11 KB
/
serve-client-form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var bodyParser = require('body-parser')
const express = require('express');
const fs = require('fs');
const inquirer = require('inquirer');
var livereload = require('livereload');
const open = require('open');
const analyzer = require('./static-analyzer/staticAnalyzer').getModuleAnalyzer();
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
const port = 7331;
const formNumber = process.argv[2];
const formNumbers = [1, 2, 3, 4, 6];
if (formNumbers.find(value => value === formNumber) === null) {
throw new Error(`Form numbers may only be ${formNumbers.join(', ')}.`);
}
app.use(express.static(__dirname));
app.get('/', function (request, response) {
response.sendStatus(201);
});
app.get('/form/:formId', function (request, response) {
const formId = request.params.formId;
response.sendFile(`${__dirname}/client-views/form-${formId}.html`);
});
const formAnalyzerFileNames = {
1: '1_first-form.js',
2: '2_second-form.js',
3: '3_third-form.js',
5: '5_async-form.js'
};
app.post('/analyze/:formNumber/:analyzer', function (request, response) {
const analyzerName = request.params.analyzer;
const formNumber = request.params.formNumber;
const formFileName = formAnalyzerFileNames[formNumber];
const analyzerOptions = request.body;
const source = fs.readFileSync(`./jsforms-source/${formFileName}`, { encoding: 'utf8' });
const analysisResult = analyzer[analyzerName](source, analyzerOptions);
response.json({ result: analysisResult });
});
app.listen(port, function () {
console.log(`App started on port ${port}`);
var server = livereload.createServer();
server.watch(__dirname + "/jsforms-source");
server.watch(__dirname + "/test");
open(`http://localhost:${port}/form/${formNumber}`);
inquirer.prompt([
{
message: 'When ready, press enter to quit:',
name: 'quit',
type: 'list',
choices: ['quit'],
validate: response => response.trim().toLowerCase() === 'q'
}
]).then(function () {
process.exit();
});
});