This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatch.js
67 lines (59 loc) · 1.46 KB
/
watch.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
if (process.argv.length < 3) {
console.error('lack of 3rd argument.(connection to <prod>, <stg>, or <local>)');
process.exit(1);
}
const cp = require('child_process');
const watch = require('watch');
const path = require('path');
const minimatch = require('minimatch');
const express = require('express');
const env = process.argv[2];
const rootDir = __dirname;
const publicDir = rootDir + '/dest/public';
function runServer() {
const app = express();
app.use((req, res, next) => {
if (req.url.indexOf('/login') >= 0 || req.url.indexOf('/master') >= 0) {
res.set({
'Content-Type': 'text/html'
});
}
next();
});
app.use(express.static(publicDir));
app.listen(3030, _ => {
console.log('Example app listening on port 3030!')
});
}
const queued = {
build: false
};
function taskBuild(cb) {
if (queued.build) {
queued.build = false;
// console.log('build start\n');
const args = [rootDir + '/build.sh', env];
const sh = cp.spawn('sh', args, {
stdio: 'inherit'
});
sh.on('close', cb);
} else {
cb();
}
}
function runWatcher() {
taskBuild(_ => {
setTimeout(runWatcher, 300);
});
}
function schedule(type, stat) {
queued[type] = true;
}
watch.createMonitor('src', monitor => {
monitor.on("created", schedule.bind(null, 'build'));
monitor.on("changed", schedule.bind(null, 'build'));
monitor.on("removed", schedule.bind(null, 'build'));
});
schedule('build');
runWatcher();
runServer();