-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathwatch.js
206 lines (172 loc) · 4.37 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* eslint-disable no-console */
import sane from 'sane';
import { resolve as resolvePath } from 'path';
import { spawn } from 'child_process';
import flowBinPath from 'flow-bin';
process.env.PATH += ':./node_modules/.bin';
var cmd = resolvePath(__dirname);
var srcDir = resolvePath(cmd, './src');
function exec(command, options) {
return new Promise((resolve, reject) => {
var child = spawn(command, options, {
cmd,
env: process.env,
stdio: 'inherit',
});
child.on('exit', code => {
if (code === 0) {
resolve(true);
} else {
reject(new Error('Error code: ' + code));
}
});
});
}
var flowServer = spawn(flowBinPath, ['server'], {
cmd,
env: process.env,
});
var watcher = sane(srcDir, { glob: ['**/*.js'] })
.on('ready', startWatch)
.on('add', changeFile)
.on('delete', deleteFile)
.on('change', changeFile);
process.on('SIGINT', () => {
watcher.close();
flowServer.kill();
console.log(CLEARLINE + yellow(invert('stopped watching')));
process.exit();
});
var isChecking;
var needsCheck;
var toCheck = {};
var timeout;
function startWatch() {
process.stdout.write(CLEARSCREEN + green(invert('watching...')));
}
function changeFile(filepath, root, stat) {
if (!stat.isDirectory()) {
toCheck[filepath] = true;
debouncedCheck();
}
}
function deleteFile(filepath) {
delete toCheck[filepath];
debouncedCheck();
}
function debouncedCheck() {
needsCheck = true;
clearTimeout(timeout);
timeout = setTimeout(guardedCheck, 250);
}
function guardedCheck() {
if (isChecking || !needsCheck) {
return;
}
isChecking = true;
var filepaths = Object.keys(toCheck);
toCheck = {};
needsCheck = false;
checkFiles(filepaths).then(() => {
isChecking = false;
process.nextTick(guardedCheck);
});
}
function checkFiles(filepaths) {
console.log('\u001b[2J');
return parseFiles(filepaths)
.then(() => runTests(filepaths))
.then(testSuccess =>
lintFiles(filepaths).then(lintSuccess =>
typecheckStatus().then(
typecheckSuccess => testSuccess && lintSuccess && typecheckSuccess,
),
),
)
.catch(() => false)
.then(success => {
process.stdout.write(
'\n' + (success ? '' : '\x07') + green(invert('watching...')),
);
});
}
// Checking steps
function parseFiles(filepaths) {
console.log('Checking Syntax');
return Promise.all(
filepaths.map(filepath => {
if (isJS(filepath) && !isTest(filepath)) {
return exec('babel', [
'--optional',
'runtime',
'--out-file',
'/dev/null',
srcPath(filepath),
]);
}
}),
);
}
function runTests(filepaths) {
console.log('\nRunning Tests');
return exec(
'jest',
allTests(filepaths) ? filepaths.map(srcPath) : ['src'],
).catch(() => false);
}
function lintFiles(filepaths) {
console.log('Linting Code\n');
return filepaths.reduce(
(prev, filepath) =>
prev.then(prevSuccess => {
if (isJS(filepath)) {
process.stdout.write(' ' + filepath + ' ...');
return exec('eslint', [srcPath(filepath)])
.catch(() => false)
.then(success => {
console.log(
CLEARLINE + ' ' + (success ? CHECK : X) + ' ' + filepath,
);
return prevSuccess && success;
});
}
return prevSuccess;
}),
Promise.resolve(true),
);
}
function typecheckStatus() {
console.log('\nType Checking\n');
return exec(flowBinPath, ['status']).catch(() => false);
}
// Filepath
function srcPath(filepath) {
return resolvePath(srcDir, filepath);
}
// Predicates
function isJS(filepath) {
return filepath.indexOf('.js') === filepath.length - 3;
}
function allTests(filepaths) {
return filepaths.length > 0 && filepaths.every(isTest);
}
function isTest(filepath) {
return isJS(filepath) && filepath.indexOf('__tests__/') !== -1;
}
// Print helpers
var CLEARSCREEN = '\u001b[2J';
var CLEARLINE = '\r\x1B[K';
var CHECK = green('\u2713');
var X = red('\u2718');
function invert(str) {
return `\u001b[7m ${str} \u001b[27m`;
}
function red(str) {
return `\x1B[K\u001b[1m\u001b[31m${str}\u001b[39m\u001b[22m`;
}
function green(str) {
return `\x1B[K\u001b[1m\u001b[32m${str}\u001b[39m\u001b[22m`;
}
function yellow(str) {
return `\x1B[K\u001b[1m\u001b[33m${str}\u001b[39m\u001b[22m`;
}