-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
97 lines (79 loc) · 2.42 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var express = require('express');
var promiseFromChildProcess = require('./promise-from-child-process.js');
var exec = require('child_process').exec;
var execFile = require('child_process').execFile;
var fs = require('fs');
var cuid = require('cuid');
var normalHandler = require('./normal-handler.js');
var app = express();
// populate hash for version master folders
var masterHash = {};
var contents = fs.readdirSync('./webpack-builder/');
if(contents === undefined || contents === null || contents.length === 0) {
console.log("Error reading webpack-builder directory");
process.exit;
}
contents.forEach(function (dir) {
if(dir.match(/.+[-].+[-].+/g) !== null) {
masterHash[dir] = true;
}
});
// assign id to request
var requestId = function requestId(req, res, next) {
req.requestId = cuid();
next();
};
app.use(requestId);
// configure route
app.get('/', function (req, res) {
// res.write('This may take a couple minutes (for real)..');
var masterFolder = req.query.v.replace(/[.]/g, '-');
if(masterHash[masterFolder] === undefined) {
//set to false and initialize first
masterHash[masterFolder] = false;
promiseFromChildProcess(exec('bash scripts/version-init.sh ' + req.query.v))
.then(function (result) {
// console.log(result);
console.log("Initialized MUI version " + req.query.v);
masterHash[masterFolder] = true;
return normalHandler(req, res);
})
.catch(function (err) {
console.error('ERROR: ', err);
res.send("oops something went wrong: " + err);
});
}
else if(masterHash[masterFolder] === true) {
//continue as usual
normalHandler(req, res)
.catch(function (err) {
console.error('ERROR: ', err);
res.send("oops something went wrong: " + err);
});
}
else if(masterHash[masterFolder] === false) {
//check every three seconds
var checker = setInterval(function () {
if(masterHash[masterFolder] === false) {
//do nothing, let interval repeat
}
else {
//clear interval and continue as usual
clearInterval(checker);
normalHandler(req, res)
.catch(function (err) {
console.log('ERROR: ', err);
res.send("oops something went wrong: " + err);
});
}
}, 3000);
}
else {
res.send("oops something went wrong: hit else");
}
});
var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});