-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (43 loc) · 1.21 KB
/
index.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
const boxen = require("boxen");
const {bold, dim, yellow} = require("chalk");
const {spawn} = require("child_process");
const {watch} = require("chokidar");
const kill = require("tree-kill");
module.exports = (
{files = ".", command = "npm start", ignore = "node_modules"}
) => {
const spawnOpts = {
shell: true,
stdio: "inherit"
};
const watchOpts = {
ignored: ignore
};
const boxOpts = {
margin: 1,
padding: 1,
borderColor: "yellow"
};
const strtName = yellow.bold("strt");
const ignoreMessage = dim(`(ignoring "${ignore}")`);
const startMessage = `
${strtName} is now running.
- Watching "${yellow.bold(files)}" for changes ${ignoreMessage}.
- Running command "${yellow.bold(command)}".
`;
const restartMessage = `
${strtName} spotted a change, re-running "${yellow.bold(command)}" now.
`;
const watcher = watch(files, watchOpts);
let previousProcess = spawn(command, spawnOpts);
console.log(boxen(startMessage, boxOpts));
watcher.on("change", path => {
kill(previousProcess.pid, "", err => {
if (err) {
console.error(err);
}
console.log(boxen(restartMessage, boxOpts));
previousProcess = spawn(command, spawnOpts);
});
});
};