-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
83 lines (73 loc) · 2.07 KB
/
start.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
require('colors');
const express = require('express');
const watchman = require('fb-watchman');
const { build } = require('./build');
const app = express();
const subscriptionName = 'anotreservice_watchman_sub';
let lastBuild = null;
const makeSubscription = (client, watch, relativePath) => {
const sub = {
expression: ['allof', ['match', '*']],
fields: ['name', 'size', 'mtime_ms', 'exists', 'type']
};
if (relativePath) {
sub.relative_root = relativePath;
}
client.command(['subscribe', watch, subscriptionName, sub],
(error, resp) => {
if (error) {
console.error('failed to subscribe: ', error);
return;
}
console.log('👀 Watching files changes...'.green);
});
client.on('subscription', (resp) => {
if (resp.subscription !== subscriptionName) return;
resp.files.forEach((file) => {
const { name } = file;
if (!['.git/', 'build/'].some(path => name.startsWith(path))) {
const currentTime = (new Date()).getTime();
if (currentTime - 500 >= lastBuild) {
console.log(`ℹ️ Detected change (${`${name}`.cyan}) since last build: ${'Rebuilding'.yellow}...`);
build();
lastBuild = currentTime;
}
}
});
});
}
const init = () => build()
.then(() => {
lastBuild = (new Date()).getTime();
app.use(express.static(`${__dirname}/build`));
app.listen(8000, () => {
console.log('App listening on port 8000.')
});
const client = new watchman.Client();
let watch = null;
// https://facebook.github.io/watchman/docs/nodejs.html
client.capabilityCheck({ optional: [], required: ['relative_root'] },
(error) => {
if (error) {
console.log(error);
client.end();
return;
}
client.command(['watch-project', __dirname],
(error, resp) => {
if (error) {
console.error('Error initiating watch:'.red, error);
return;
}
if ('warning' in resp) {
console.log('Warning: '.red, resp.warning);
}
({
watch
} = resp);
console.log('✅ Watch has been set up.'.green);
makeSubscription(client, watch);
});
});
});
init();