Guest User

Untitled

a guest
Aug 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. const spawn = require('child_process').spawn;
  2. const fs = require('fs');
  3.  
  4. function debounce(func, wait, immediate) {
  5. var timeout;
  6. return function() {
  7. var context = this, args = arguments;
  8. var later = function() {
  9. timeout = null;
  10. if (!immediate) func.apply(context, args);
  11. };
  12. var callNow = immediate && !timeout;
  13. clearTimeout(timeout);
  14. timeout = setTimeout(later, wait);
  15. if (callNow) func.apply(context, args);
  16. };
  17. };
  18.  
  19. function fileChanged(_, filename) {
  20. process.stdout.write('\033[2J'); // CLEAR
  21. console.log(`main.js changed, reloading...`)
  22. var child = spawn('node', [filename]);
  23.  
  24. child.stdin.pipe(process.stdin);
  25. child.stdout.pipe(process.stdout);
  26.  
  27. child.on('close', (code) => {
  28. console.log(`child process exited with code ${code}`);
  29. });
  30.  
  31. child.unref()
  32. }
  33.  
  34. function runOnceThenWatch(filename) {
  35. fileChanged(null, filename)
  36. fs.watch(filename, debounce(fileChanged, 100));
  37. }
  38.  
  39. runOnceThenWatch('main.js')
Add Comment
Please, Sign In to add comment