Skip to content

Commit

Permalink
Refactor to reduce churn and avoid breaking unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Dec 5, 2024
1 parent 749bb07 commit 28966a8
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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`);
Expand Down

0 comments on commit 28966a8

Please sign in to comment.