Skip to content

Commit

Permalink
feat(cli): provide a more helpful error if there's no command
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Dec 10, 2024
1 parent 4790a82 commit 8651995
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/env-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions test/env-cmd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
})

0 comments on commit 8651995

Please sign in to comment.