mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
fix(expand-envs): test more edge cases
This commit is contained in:
parent
18e8a28148
commit
0846e5d6a5
@ -58,16 +58,17 @@ 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)
|
||||
-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
|
||||
--recursive Replace $var and ${var} in env file with the referenced environment variable
|
||||
-h, --help output usage information
|
||||
-h, --help display help for command
|
||||
|
||||
```
|
||||
|
||||
## 🔬 Advanced Usage
|
||||
|
||||
2
dist/expand-envs.d.ts
vendored
2
dist/expand-envs.d.ts
vendored
@ -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;
|
||||
|
||||
4
dist/expand-envs.js
vendored
4
dist/expand-envs.js
vendored
@ -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(/(?<!\\)\$\{?[a-zA-Z0-9_]+\}?/g, (varName) => {
|
||||
return str.replace(/(?<!\\)\$(\{\w+\}|\w+)?/g, (varName) => {
|
||||
const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)];
|
||||
return varValue ?? varName;
|
||||
});
|
||||
|
||||
5
dist/parse-args.js
vendored
5
dist/parse-args.js
vendored
@ -88,18 +88,17 @@ export function parseArgsUsingCommander(args) {
|
||||
.usage('[options] -- <command> [...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' });
|
||||
|
||||
@ -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(/(?<!\\)\$\{?[a-zA-Z0-9_]+\}?/g, (varName) => {
|
||||
return str.replace(/(?<!\\)\$(\{\w+\}|\w+)?/g, (varName) => {
|
||||
const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)]
|
||||
return varValue ?? varName
|
||||
})
|
||||
|
||||
@ -100,8 +100,8 @@ export function parseArgsUsingCommander(args: string[]): CommanderOptions {
|
||||
.usage('[options] -- <command> [...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.')
|
||||
|
||||
@ -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 => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user