-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatch.js
More file actions
52 lines (44 loc) · 1.28 KB
/
Copy pathwatch.js
File metadata and controls
52 lines (44 loc) · 1.28 KB
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
'use strict';
const watch = require('watch');
// https://www.npmjs.com/package/watch
const opts = {
ignoreDotFiles: true,
ignoreUnreadableDir: true,
};
watch.watchTree('./src', opts, () => {
process.stdout.write('Rebuilding...');
runCommand('npm', ['run', 'build'], (err, stderr, stdout) => {
if (err) {
console.log(' Error', err.stack);
return;
}
if (stderr) {
console.log(' Fail');
console.log(formatMessage(stderr))
return;
}
if (stdout) {
console.log(' Built');
console.log(formatMessage(stdout));
}
});
});
// cb signature is (err, stderrString, stdoutString)
function runCommand(cmd, args, cb) {
const spawn = require('child_process').spawn;
const child = spawn(cmd, args);
let stderrMessage = '';
let stdoutMessage = '';
child.on('error', (err) => cb(err));
child.stderr.on('data', buf => stderrMessage += buf.toString());
child.stderr.on('end', () => cb(null, stderrMessage));
child.stdout.on('data', buf => stdoutMessage += buf.toString());
child.stdout.on('end', () => cb(null, null, stdoutMessage));
};
// Adds a 2 space gutter before stdout/stderr output so it stands out
function formatMessage(msg) {
//if (!msg) return;
return msg.split('\n')
.map(s => ' ' + s)
.join('\n');
}