11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst isWin = process.platform === 'win32';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cifunction notFoundError(original, syscall) {
61cb0ef41Sopenharmony_ci    return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
71cb0ef41Sopenharmony_ci        code: 'ENOENT',
81cb0ef41Sopenharmony_ci        errno: 'ENOENT',
91cb0ef41Sopenharmony_ci        syscall: `${syscall} ${original.command}`,
101cb0ef41Sopenharmony_ci        path: original.command,
111cb0ef41Sopenharmony_ci        spawnargs: original.args,
121cb0ef41Sopenharmony_ci    });
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction hookChildProcess(cp, parsed) {
161cb0ef41Sopenharmony_ci    if (!isWin) {
171cb0ef41Sopenharmony_ci        return;
181cb0ef41Sopenharmony_ci    }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    const originalEmit = cp.emit;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci    cp.emit = function (name, arg1) {
231cb0ef41Sopenharmony_ci        // If emitting "exit" event and exit code is 1, we need to check if
241cb0ef41Sopenharmony_ci        // the command exists and emit an "error" instead
251cb0ef41Sopenharmony_ci        // See https://github.com/IndigoUnited/node-cross-spawn/issues/16
261cb0ef41Sopenharmony_ci        if (name === 'exit') {
271cb0ef41Sopenharmony_ci            const err = verifyENOENT(arg1, parsed, 'spawn');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci            if (err) {
301cb0ef41Sopenharmony_ci                return originalEmit.call(cp, 'error', err);
311cb0ef41Sopenharmony_ci            }
321cb0ef41Sopenharmony_ci        }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci        return originalEmit.apply(cp, arguments); // eslint-disable-line prefer-rest-params
351cb0ef41Sopenharmony_ci    };
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction verifyENOENT(status, parsed) {
391cb0ef41Sopenharmony_ci    if (isWin && status === 1 && !parsed.file) {
401cb0ef41Sopenharmony_ci        return notFoundError(parsed.original, 'spawn');
411cb0ef41Sopenharmony_ci    }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    return null;
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cifunction verifyENOENTSync(status, parsed) {
471cb0ef41Sopenharmony_ci    if (isWin && status === 1 && !parsed.file) {
481cb0ef41Sopenharmony_ci        return notFoundError(parsed.original, 'spawnSync');
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    return null;
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cimodule.exports = {
551cb0ef41Sopenharmony_ci    hookChildProcess,
561cb0ef41Sopenharmony_ci    verifyENOENT,
571cb0ef41Sopenharmony_ci    verifyENOENTSync,
581cb0ef41Sopenharmony_ci    notFoundError,
591cb0ef41Sopenharmony_ci};
60