From 28966a853243f3f2a7f46dda8bc0d5917b94a3cf Mon Sep 17 00:00:00 2001 From: John Gee Date: Fri, 6 Dec 2024 12:47:50 +1300 Subject: [PATCH] Refactor to reduce churn and avoid breaking unit tests --- lib/command.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/command.js b/lib/command.js index 77df60c1c..90e1c05ae 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1165,17 +1165,6 @@ Expecting one of '${allowedValues.join("', '")}'`); launchWithNode = sourceExt.includes(path.extname(executableFile)); - function throwMissingExecutable() { - const executableDirMessage = executableDir - ? `searched for local subcommand relative to directory '${executableDir}'` - : 'no directory for search for local subcommand, use .executableDir() to supply a custom directory'; - const executableMissing = `'${executableFile}' does not exist - - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead - - if the default executable name is not suitable, use the executableFile option to supply a custom name or path - - ${executableDirMessage}`; - throw new Error(executableMissing); - } - let proc; if (process.platform !== 'win32') { if (launchWithNode) { @@ -1188,7 +1177,6 @@ Expecting one of '${allowedValues.join("', '")}'`); proc = childProcess.spawn(executableFile, args, { stdio: 'inherit' }); } } else { - if (!fs.existsSync(executableFile)) throwMissingExecutable(); args.unshift(executableFile); // add executable arguments to spawn args = incrementNodeInspectorPort(process.execArgv).concat(args); @@ -1225,9 +1213,19 @@ Expecting one of '${allowedValues.join("', '")}'`); } }); proc.on('error', (err) => { - // @ts-ignore: because err.code is an unknown property - if (err.code === 'ENOENT') { - throwMissingExecutable(); + if ( + // @ts-ignore: because err.code is an unknown property + (process.platform !== 'win32' && err.code === 'ENOENT') || + (process.platform === 'win32' && !fs.existsSync(executableFile)) + ) { + const executableDirMessage = executableDir + ? `searched for local subcommand relative to directory '${executableDir}'` + : 'no directory for search for local subcommand, use .executableDir() to supply a custom directory'; + const executableMissing = `'${executableFile}' does not exist + - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead + - if the default executable name is not suitable, use the executableFile option to supply a custom name or path + - ${executableDirMessage}`; + throw new Error(executableMissing); // @ts-ignore: because err.code is an unknown property } else if (err.code === 'EACCES') { throw new Error(`'${executableFile}' not executable`);