3 var isWin = process.platform === 'win32';
4 var resolveCommand = require('./util/resolveCommand');
6 var isNode10 = process.version.indexOf('v0.10.') === 0;
8 function notFoundError(command, syscall) {
11 err = new Error(syscall + ' ' + command + ' ENOENT');
12 err.code = err.errno = 'ENOENT';
13 err.syscall = syscall + ' ' + command;
18 function hookChildProcess(cp, parsed) {
25 originalEmit = cp.emit;
26 cp.emit = function (name, arg1) {
29 // If emitting "exit" event and exit code is 1, we need to check if
30 // the command exists and emit an "error" instead
31 // See: https://github.com/IndigoUnited/node-cross-spawn/issues/16
32 if (name === 'exit') {
33 err = verifyENOENT(arg1, parsed, 'spawn');
36 return originalEmit.call(cp, 'error', err);
40 return originalEmit.apply(cp, arguments);
44 function verifyENOENT(status, parsed) {
45 if (isWin && status === 1 && !parsed.file) {
46 return notFoundError(parsed.original, 'spawn');
52 function verifyENOENTSync(status, parsed) {
53 if (isWin && status === 1 && !parsed.file) {
54 return notFoundError(parsed.original, 'spawnSync');
57 // If we are in node 10, then we are using spawn-sync; if it exited
58 // with -1 it probably means that the command does not exist
59 if (isNode10 && status === -1) {
60 parsed.file = isWin ? parsed.file : resolveCommand(parsed.original);
63 return notFoundError(parsed.original, 'spawnSync');
70 module.exports.hookChildProcess = hookChildProcess;
71 module.exports.verifyENOENT = verifyENOENT;
72 module.exports.verifyENOENTSync = verifyENOENTSync;
73 module.exports.notFoundError = notFoundError;