enoent.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. const isWin = process.platform === 'win32';
  3. function notFoundError(original, syscall) {
  4. return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
  5. code: 'ENOENT',
  6. errno: 'ENOENT',
  7. syscall: `${syscall} ${original.command}`,
  8. path: original.command,
  9. spawnargs: original.args,
  10. });
  11. }
  12. function hookChildProcess(cp, parsed) {
  13. if (!isWin) {
  14. return;
  15. }
  16. const originalEmit = cp.emit;
  17. cp.emit = function (name, arg1) {
  18. // If emitting "exit" event and exit code is 1, we need to check if
  19. // the command exists and emit an "error" instead
  20. // See https://github.com/IndigoUnited/node-cross-spawn/issues/16
  21. if (name === 'exit') {
  22. const err = verifyENOENT(arg1, parsed, 'spawn');
  23. if (err) {
  24. return originalEmit.call(cp, 'error', err);
  25. }
  26. }
  27. return originalEmit.apply(cp, arguments); // eslint-disable-line prefer-rest-params
  28. };
  29. }
  30. function verifyENOENT(status, parsed) {
  31. if (isWin && status === 1 && !parsed.file) {
  32. return notFoundError(parsed.original, 'spawn');
  33. }
  34. return null;
  35. }
  36. function verifyENOENTSync(status, parsed) {
  37. if (isWin && status === 1 && !parsed.file) {
  38. return notFoundError(parsed.original, 'spawnSync');
  39. }
  40. return null;
  41. }
  42. module.exports = {
  43. hookChildProcess,
  44. verifyENOENT,
  45. verifyENOENTSync,
  46. notFoundError,
  47. };