Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): provide a more helpful error if there's no command #396

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
});
})
Loading