watch.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. var childProcess = require('child_process');
  2. var watch = require('watch')
  3. function runScript(scriptPath, processArguments, callback) {
  4. // keep track of whether callback has been invoked to prevent multiple invocations
  5. var invoked = false;
  6. var process = childProcess.fork(scriptPath, processArguments);
  7. // listen for errors as they may prevent the exit event from firing
  8. process.on('error', function (err) {
  9. if (invoked) return;
  10. invoked = true;
  11. callback(err);
  12. });
  13. // execute the callback once the process has finished running
  14. process.on('exit', function (code) {
  15. if (invoked) return;
  16. invoked = true;
  17. var err = code === 0 ? null : new Error('exit code ' + code);
  18. callback(err);
  19. });
  20. }
  21. watch.watchTree(`${__dirname}\\..\\player\\js\\`, function (f, curr, prev) {
  22. runScript(`${__dirname}\\build.js`, ["reduced"], function (err) {
  23. if (err) throw err;
  24. console.log('finished running some-script.js');
  25. });
  26. })