diff --git a/README.md b/README.md index 5a45fed..b7b3fef 100644 --- a/README.md +++ b/README.md @@ -57,17 +57,18 @@ 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) - -x, --expand-envs Replace $var and ${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 - --recursive Replace $var and ${var} in env file with the referenced environment variable - -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 and ${var} in args and command with environment variables + --recursive Replace $var and ${var} in env file with the referenced environment variable + --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/expand-envs.d.ts b/dist/expand-envs.d.ts index 420523e..c77ac26 100644 --- a/dist/expand-envs.d.ts +++ b/dist/expand-envs.d.ts @@ -1,6 +1,6 @@ import type { Environment } from './types.ts'; /** * expandEnvs Replaces $var and ${var} in args and command with environment variables - * the environment variable doesn't exist, it leaves it as is. + * if the environment variable doesn't exist, it leaves it as is. */ export declare function expandEnvs(str: string, envs: Environment): string; diff --git a/dist/expand-envs.js b/dist/expand-envs.js index a46c937..7fb5fd5 100644 --- a/dist/expand-envs.js +++ b/dist/expand-envs.js @@ -1,9 +1,9 @@ /** * expandEnvs Replaces $var and ${var} in args and command with environment variables - * the environment variable doesn't exist, it leaves it as is. + * if the environment variable doesn't exist, it leaves it as is. */ export function expandEnvs(str, envs) { - return str.replace(/(? { + return str.replace(/(? { const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)]; return varValue ?? varName; }); diff --git a/dist/parse-args.js b/dist/parse-args.js index 5d51ec7..58a5ed9 100644 --- a/dist/parse-args.js +++ b/dist/parse-args.js @@ -88,18 +88,17 @@ export function parseArgsUsingCommander(args) { .usage('[options] -- [...args]') .option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList) .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 and $\\{var\\} in args and command with environment variables') + .option('-x, --expand-envs', 'Replace $var and ${var} in args and command with environment variables') + .option('--recursive', 'Replace $var and ${var} in env file with the referenced environment variable') .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') - .option('--recursive', 'Replace $var and $\\{var\\} in env file with the referenced environment variable') // 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.'); })) - // ENDTODO .allowUnknownOption(true) .allowExcessArguments(true) .parse(['_', '_', ...args], { from: 'node' }); diff --git a/src/expand-envs.ts b/src/expand-envs.ts index 565665b..958405d 100644 --- a/src/expand-envs.ts +++ b/src/expand-envs.ts @@ -2,10 +2,10 @@ import type { Environment } from './types.ts' /** * expandEnvs Replaces $var and ${var} in args and command with environment variables - * the environment variable doesn't exist, it leaves it as is. + * if the environment variable doesn't exist, it leaves it as is. */ export function expandEnvs (str: string, envs: Environment): string { - return str.replace(/(? { + return str.replace(/(? { const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)] return varValue ?? varName }) diff --git a/src/parse-args.ts b/src/parse-args.ts index de4adcf..03bdb25 100644 --- a/src/parse-args.ts +++ b/src/parse-args.ts @@ -100,8 +100,8 @@ export function parseArgsUsingCommander(args: string[]): CommanderOptions { .usage('[options] -- [...args]') .option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList) .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('--recursive', 'Replace $var and $\\{var\\} in env file with the referenced environment variable') + .option('-x, --expand-envs', 'Replace $var and ${var} in args and command with environment variables') + .option('--recursive', 'Replace $var and ${var} in env file with the referenced environment variable') .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.') diff --git a/test/expand-envs.spec.ts b/test/expand-envs.spec.ts index 1438de6..ca348e8 100644 --- a/test/expand-envs.spec.ts +++ b/test/expand-envs.spec.ts @@ -15,12 +15,14 @@ describe('expandEnvs', (): void => { const args = [ 'notvar', '$dollar', '\\$notvar', '-4', '$PING', '$IP1', '\\$IP1', '$NONEXIST', - '${PING}', '${NONEXIST}' + '${PING}', '${NONEXIST}', '\\${PING}', + '$PING}', '${PING2' ] const argsExpanded = [ 'notvar', 'money', '\\$notvar', '-4', 'PONG', '127.0.0.1', '\\$IP1', '$NONEXIST', - 'PONG', '${NONEXIST}' + 'PONG', '${NONEXIST}', '\\${PING}', + 'PONG}', '${PING2' ] it('should replace environment variables in args', (): void => {