diff --git a/src/env-cmd.ts b/src/env-cmd.ts index 814659f..d29f587 100644 --- a/src/env-cmd.ts +++ b/src/env-cmd.ts @@ -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', diff --git a/test/env-cmd.spec.ts b/test/env-cmd.spec.ts index b8887b6..7372df9 100644 --- a/test/env-cmd.spec.ts +++ b/test/env-cmd.spec.ts @@ -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.'); + }); })