-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (28 loc) · 889 Bytes
/
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
#!/usr/bin/env node
//Debounce function
const debounce = require('lodash.debounce');
//chokidar watches for new/changed files
const chokidar = require('chokidar');
//CLI Tool
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 (err) {
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' });
}, 200);
chokidar.watch('.').on('add', start).on('change', start).on('unlink', start);
});
program.parse(process.argv);