diff --git a/README.md b/README.md index ec17c65..52b148e 100644 --- a/README.md +++ b/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] -- [...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 diff --git a/dist/parse-args.js b/dist/parse-args.js index c269122..792001f 100644 --- a/dist/parse-args.js +++ b/dist/parse-args.js @@ -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] -- [...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' }); diff --git a/dist/types.d.ts b/dist/types.d.ts index 483442f..797988d 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -7,7 +7,6 @@ export type CommanderOptions = Command<[], { fallback?: boolean; file?: true | string; override?: boolean; - rcFile?: true | string; silent?: boolean; useShell?: boolean; verbose?: boolean; diff --git a/src/parse-args.ts b/src/parse-args.ts index ff39601..74dcdd7 100644 --- a/src/parse-args.ts +++ b/src/parse-args.ts @@ -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] -- [...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' }) diff --git a/src/types.ts b/src/types.ts index dfad531..c9fc0c0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 diff --git a/test/parse-args.spec.ts b/test/parse-args.spec.ts index 364502d..30e19ac 100644 --- a/test/parse-args.spec.ts +++ b/test/parse-args.spec.ts @@ -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) })