You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

4 years ago
  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. };