Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const spawn = require('child_process').spawn;
- const fs = require('fs');
- function debounce(func, wait, immediate) {
- var timeout;
- return function() {
- var context = this, args = arguments;
- var later = function() {
- timeout = null;
- if (!immediate) func.apply(context, args);
- };
- var callNow = immediate && !timeout;
- clearTimeout(timeout);
- timeout = setTimeout(later, wait);
- if (callNow) func.apply(context, args);
- };
- };
- function fileChanged(_, filename) {
- process.stdout.write('\033[2J'); // CLEAR
- console.log(`main.js changed, reloading...`)
- var child = spawn('node', [filename]);
- child.stdin.pipe(process.stdin);
- child.stdout.pipe(process.stdout);
- child.on('close', (code) => {
- console.log(`child process exited with code ${code}`);
- });
- child.unref()
- }
- function runOnceThenWatch(filename) {
- fileChanged(null, filename)
- fs.watch(filename, debounce(fileChanged, 100));
- }
- runOnceThenWatch('main.js')
Add Comment
Please, Sign In to add comment