Skip to content
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
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ To use a custom env filename or path, pass the `-f` flag. This is a major breaki
Usage: env-cmd [options] -- <command> [...args]

Options:
-v, --version output the version number
-e, --environments [env1,env2,...] The rc file environment(s) to use
-f, --file [path] Custom env file path (default path: ./.env)
--fallback Fallback to default env file path, if custom env file path not found
--no-override Do not override existing environment variables
-r, --rc-file [path] Custom rc file path (default path: ./.env-cmdrc(|.js|.json)
--silent Ignore any env-cmd errors and only fail on executed program failure.
--use-shell Execute the command in a new shell with the given environment
--verbose Print helpful debugging information
-x, --expand-envs Replace $var in args and command with environment variables
-h, --help output usage information
-v, --version output the version number
-e, --environments [envs...] The rc file environment(s) to use
-f, --file [path] Custom env file path or .rc file path if '-e' used (default path: ./.env or
./.env-cmdrc.(js|cjs|mjs|json))
-x, --expand-envs Replace $var in args and command with environment variables
--fallback Fallback to default env file path, if custom env file path not found
--no-override Do not override existing environment variables
--silent Ignore any env-cmd errors and only fail on executed program failure.
--use-shell Execute the command in a new shell with the given environment
--verbose Print helpful debugging information
-h, --help display help for command
```

## 🔬 Advanced Usage
Expand Down
13 changes: 8 additions & 5 deletions dist/parse-args.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '@commander-js/extra-typings';
import { Command, Option, CommanderError } from '@commander-js/extra-typings';
import { parseArgList } from './utils.js';
import packageJson from '../package.json' with { type: 'json' };
/**
Expand Down Expand Up @@ -43,9 +43,9 @@ export function parseArgs(args) {
rc = {
environments: parsedCmdOptions.environments,
// if we get a boolean value assume not defined
filePath: parsedCmdOptions.rcFile === true ?
filePath: parsedCmdOptions.file === true ?
undefined :
parsedCmdOptions.rcFile,
parsedCmdOptions.file,
};
}
let envFile;
Expand Down Expand Up @@ -82,14 +82,17 @@ export function parseArgsUsingCommander(args) {
.version(packageJson.version, '-v, --version')
.usage('[options] -- <command> [...args]')
.option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList)
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc.(js|cjs|mjs|json)')
.option('-f, --file [path]', 'Custom env file path or .rc file path if \'-e\' used (default path: ./.env or ./.env-cmdrc.(js|cjs|mjs|json))')
.option('-x, --expand-envs', 'Replace $var in args and command with environment variables')
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('--no-override', 'Do not override existing environment variables')
.option('--silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.option('--verbose', 'Print helpful debugging information')
// TODO: Remove -r deprecation error on version >= v12
.addOption(new Option('-r, --rc-file [path]', 'Deprecated Option')
.hideHelp()
.argParser(() => { throw new CommanderError(1, 'deprecated-option', 'The -r flag has been deprecated, use the -f flag instead.'); }))
.allowUnknownOption(true)
.allowExcessArguments(true)
.parse(['_', '_', ...args], { from: 'node' });
Expand Down
1 change: 0 additions & 1 deletion dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type CommanderOptions = Command<[], {
fallback?: boolean;
file?: true | string;
override?: boolean;
rcFile?: true | string;
silent?: boolean;
useShell?: boolean;
verbose?: boolean;
Expand Down
14 changes: 9 additions & 5 deletions src/parse-args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '@commander-js/extra-typings'
import { Command, Option, CommanderError } from '@commander-js/extra-typings'
import type { EnvCmdOptions, CommanderOptions, EnvFileOptions, RCFileOptions } from './types.ts'
import { parseArgList } from './utils.js'
import packageJson from '../package.json' with { type: 'json' }
Expand Down Expand Up @@ -51,9 +51,9 @@ export function parseArgs(args: string[]): EnvCmdOptions {
rc = {
environments: parsedCmdOptions.environments,
// if we get a boolean value assume not defined
filePath: parsedCmdOptions.rcFile === true ?
filePath: parsedCmdOptions.file === true ?
undefined :
parsedCmdOptions.rcFile,
parsedCmdOptions.file,
}
}

Expand Down Expand Up @@ -88,19 +88,23 @@ export function parseArgs(args: string[]): EnvCmdOptions {
}

export function parseArgsUsingCommander(args: string[]): CommanderOptions {

return new Command('env-cmd')
.description('CLI for executing commands using an environment from an env file.')
.version(packageJson.version, '-v, --version')
.usage('[options] -- <command> [...args]')
.option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList)
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc.(js|cjs|mjs|json)')
.option('-f, --file [path]', 'Custom env file path or .rc file path if \'-e\' used (default path: ./.env or ./.env-cmdrc.(js|cjs|mjs|json))')
.option('-x, --expand-envs', 'Replace $var in args and command with environment variables')
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('--no-override', 'Do not override existing environment variables')
.option('--silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.option('--verbose', 'Print helpful debugging information')
// TODO: Remove -r deprecation error on version >= v12
.addOption(new Option('-r, --rc-file [path]', 'Deprecated Option')
.hideHelp()
.argParser(() => { throw new CommanderError(1, 'deprecated-option', 'The -r flag has been deprecated, use the -f flag instead.') }))
.allowUnknownOption(true)
.allowExcessArguments(true)
.parse(['_', '_', ...args], { from: 'node' })
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export type CommanderOptions = Command<[], {
fallback?: boolean // Default false
file?: true | string
override?: boolean // Default: false
rcFile?: true | string
silent?: boolean // Default: false
useShell?: boolean // Default: false
verbose?: boolean // Default: false
Expand Down
2 changes: 1 addition & 1 deletion test/parse-args.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('parseArgs', (): void => {
})

it('should parse rc file path', (): void => {
const res = parseArgs(['-e', environments[0], '-r', rcFilePath, '--', command, ...commandArgs])
const res = parseArgs(['-e', environments[0], '-f', rcFilePath, '--', command, ...commandArgs])
assert.exists(res.rc)
assert.equal(res.rc.filePath, rcFilePath)
})
Expand Down
Loading