Skip to content

Commit 8651995

Browse files
committed
feat(cli): provide a more helpful error if there's no command
1 parent 4790a82 commit 8651995

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/env-cmd.ts

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export async function EnvCmd(
4545
commandArgs = commandArgs.map(arg => expandEnvs(arg, env))
4646
}
4747

48+
if (!command) {
49+
throw new Error(
50+
'env-cmd cannot be used as a standalone command. ' +
51+
'Refer to the documentation for usage examples: https://npm.im/env-cmd',
52+
);
53+
}
54+
4855
// Execute the command with the given environment variables
4956
const proc = spawn(command, commandArgs, {
5057
stdio: 'inherit',

test/env-cmd.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,22 @@ describe('EnvCmd', (): void => {
209209
assert.fail('Should not get here.')
210210
},
211211
)
212+
213+
it('provides a helpful error if the CLI is incorrectly invoked', async () => {
214+
getEnvVarsStub.returns({ BOB: 'test' });
215+
try {
216+
await envCmdLib.EnvCmd({
217+
command: '',
218+
commandArgs: [],
219+
envFile: {
220+
filePath: './.env',
221+
},
222+
});
223+
} catch (e) {
224+
assert.instanceOf(e, Error);
225+
assert.include(e.message, 'cannot be used as a standalone');
226+
return;
227+
}
228+
assert.fail('Should not get here.');
229+
});
212230
})

0 commit comments

Comments
 (0)