mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
Merge pull request #409 from toddbluhm/feat-combine-f-r-flags
Remove `-r` flag and use only `-f` flag
This commit is contained in:
commit
660f3e0a06
22
README.md
22
README.md
@ -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
|
||||
|
||||
13
dist/parse-args.js
vendored
13
dist/parse-args.js
vendored
@ -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' };
|
||||
/**
|
||||
@ -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;
|
||||
@ -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' });
|
||||
|
||||
1
dist/types.d.ts
vendored
1
dist/types.d.ts
vendored
@ -7,7 +7,6 @@ export type CommanderOptions = Command<[], {
|
||||
fallback?: boolean;
|
||||
file?: true | string;
|
||||
override?: boolean;
|
||||
rcFile?: true | string;
|
||||
silent?: boolean;
|
||||
useShell?: boolean;
|
||||
verbose?: boolean;
|
||||
|
||||
@ -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' }
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@ -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' })
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user