enoent.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. var isWin = process.platform === 'win32';
  3. var resolveCommand = require('./util/resolveCommand');
  4. var isNode10 = process.version.indexOf('v0.10.') === 0;
  5. function notFoundError(command, syscall) {
  6. var err;
  7. err = new Error(syscall + ' ' + command + ' ENOENT');
  8. err.code = err.errno = 'ENOENT';
  9. err.syscall = syscall + ' ' + command;
  10. return err;
  11. }
  12. function hookChildProcess(cp, parsed) {
  13. var originalEmit;
  14. if (!isWin) {
  15. return;
  16. }
  17. originalEmit = cp.emit;
  18. cp.emit = function (name, arg1) {
  19. var err;
  20. // If emitting "exit" event and exit code is 1, we need to check if
  21. // the command exists and emit an "error" instead
  22. // See: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  23. if (name === 'exit') {
  24. err = verifyENOENT(arg1, parsed, 'spawn');
  25. if (err) {
  26. return originalEmit.call(cp, 'error', err);
  27. }
  28. }
  29. return originalEmit.apply(cp, arguments);
  30. };
  31. }
  32. function verifyENOENT(status, parsed) {
  33. if (isWin && status === 1 && !parsed.file) {
  34. return notFoundError(parsed.original, 'spawn');
  35. }
  36. return null;
  37. }
  38. function verifyENOENTSync(status, parsed) {
  39. if (isWin && status === 1 && !parsed.file) {
  40. return notFoundError(parsed.original, 'spawnSync');
  41. }
  42. // If we are in node 10, then we are using spawn-sync; if it exited
  43. // with -1 it probably means that the command does not exist
  44. if (isNode10 && status === -1) {
  45. parsed.file = isWin ? parsed.file : resolveCommand(parsed.original);
  46. if (!parsed.file) {
  47. return notFoundError(parsed.original, 'spawnSync');
  48. }
  49. }
  50. return null;
  51. }
  52. module.exports.hookChildProcess = hookChildProcess;
  53. module.exports.verifyENOENT = verifyENOENT;
  54. module.exports.verifyENOENTSync = verifyENOENTSync;
  55. module.exports.notFoundError = notFoundError;