-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (32 loc) · 1.16 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
#!/usr/bin/env node //this line is to access app any where in your teminal by writing 'watchit' and then the file path you want to watch
const chokidar = require("chokidar");
const debounce = require("lodash.debounce");
const program = require("caporal");
const fs = require("fs");
const { spawn } = require('child_process');
const chalk = require('chalk')
program
.version('0.0.1')
.argument('[filename]', 'Name of a file to execute')
.action(async ({ filename }) => {
const name = filename || 'index.js';
try{
await fs.promises.access(name);
} catch{
throw new Error(`Could not find the file '${name}'`)
}
let proc;
const start = debounce(() => {
if(proc){
proc.kill();
}
console.log(chalk.blue('>>>> Starting process...'));
proc = spawn('node', [name], { stdio : 'inherit' });
}, 500);
chokidar.watch('.')
.on("add", start)
.on("change", start)
.on("unlink", start);
});
program.parse(process.argv);
//use ' watchit help ' on your terminal to see more informations