Merge pull request #396 from toddbluhm/kh/invoke-error

feat(cli): provide a more helpful error if there's no command
This commit is contained in:
Kyℓe Hensel 2024-12-11 18:37:20 +11:00 committed by GitHub
commit 24e8d18f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -45,6 +45,13 @@ export async function EnvCmd(
commandArgs = commandArgs.map(arg => expandEnvs(arg, env))
}
if (!command) {
throw new Error(
'env-cmd cannot be used as a standalone command. ' +
'Refer to the documentation for usage examples: https://npm.im/env-cmd',
);
}
// Execute the command with the given environment variables
const proc = spawn(command, commandArgs, {
stdio: 'inherit',

View File

@ -209,4 +209,22 @@ describe('EnvCmd', (): void => {
assert.fail('Should not get here.')
},
)
it('provides a helpful error if the CLI is incorrectly invoked', async () => {
getEnvVarsStub.returns({ BOB: 'test' });
try {
await envCmdLib.EnvCmd({
command: '',
commandArgs: [],
envFile: {
filePath: './.env',
},
});
} catch (e) {
assert.instanceOf(e, Error);
assert.include(e.message, 'cannot be used as a standalone');
return;
}
assert.fail('Should not get here.');
});
})