Skip to content

Commit a04b668

Browse files
committed
fix(recursive): cleaned up after rebase
1 parent 5b717e6 commit a04b668

File tree

11 files changed

+35
-24
lines changed

11 files changed

+35
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Changelog
22

3-
## 10.1.1 - In Development
3+
## Landed in master
44

5-
- **Upgrade**: Upgraded dependency `commander` to `5.x`
6-
- **Upgrade**: Upgraded devDependencies `ts-standard`, `sinon`
5+
- **Upgrade**: Upgraded dependency `commander` to `13.x`
6+
- **Upgrade**: Upgraded dependency `cross-spawn` to `7.x`
7+
- **Upgrade**: Upgraded all devDependencies `ts-standard`, `sinon`
78
- **Feature**: support both `$var` and `${var}` when expanding vars
89
- **Feature**: Added support for nested env variables with the `--recursive` flag
910

dist/env-cmd.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export async function EnvCmd({ command, commandArgs, envFile, rc, options = {},
3131
}
3232
if (options.recursive === true) {
3333
for (const key of Object.keys(env)) {
34-
env[key] = expand_envs_1.expandEnvs(env[key], env);
34+
if (env[key] !== undefined) {
35+
env[key] = expandEnvs(env[key], env);
36+
}
3537
}
3638
}
3739
if (options.expandEnvs === true) {

dist/expand-envs.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Environment } from './types.ts';
22
/**
3-
* expandEnvs Replaces $var in args and command with environment variables
4-
* if the environment variable doesn't exist, it leaves it as is.
3+
* expandEnvs Replaces $var and ${var} in args and command with environment variables
4+
* the environment variable doesn't exist, it leaves it as is.
55
*/
66
export declare function expandEnvs(str: string, envs: Environment): string;

dist/expand-envs.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
2-
* expandEnvs Replaces $var in args and command with environment variables
3-
* if the environment variable doesn't exist, it leaves it as is.
2+
* expandEnvs Replaces $var and ${var} in args and command with environment variables
3+
* the environment variable doesn't exist, it leaves it as is.
44
*/
55
export function expandEnvs(str, envs) {
6-
return str.replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, (varName) => {
7-
const varValue = envs[varName.slice(1)];
8-
// const test = 42;
6+
return str.replace(/(?<!\\)\$\{?[a-zA-Z0-9_]+\}?/g, (varName) => {
7+
const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)];
98
return varValue === undefined ? varName : varValue.toString();
109
});
1110
}

dist/parse-args.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function parseArgs(args) {
2929
expandEnvs = true;
3030
}
3131
let recursive = false;
32-
if (program.recursive === true) {
32+
if (parsedCmdOptions.recursive === true) {
3333
recursive = true;
3434
}
3535
let verbose = false;
@@ -90,16 +90,12 @@ export function parseArgsUsingCommander(args) {
9090
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
9191
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc.(js|cjs|mjs|json)')
9292
.option('-x, --expand-envs', 'Replace $var in args and command with environment variables')
93+
.option('--recursive', 'Replace $var and $\\{var\\} in env file with the referenced environment variable')
9394
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
9495
.option('--no-override', 'Do not override existing environment variables')
9596
.option('--silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
9697
.option('--use-shell', 'Execute the command in a new shell with the given environment')
9798
.option('--verbose', 'Print helpful debugging information')
98-
<<<<<<< HEAD
99-
=======
100-
.option('-x, --expand-envs', 'Replace $var and $\\{var\\} in args and command with environment variables')
101-
.option('--recursive', 'Replace $var and $\\{var\\} in env file with the referenced environment variable')
102-
>>>>>>> 65d2198 (feat: recursive embedding of env vars in env vars)
10399
.allowUnknownOption(true)
104100
.allowExcessArguments(true)
105101
.parse(['_', '_', ...args], { from: 'node' });

dist/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type RCEnvironment = Partial<Record<string, Environment>>;
44
export type CommanderOptions = Command<[], {
55
environments?: true | string[];
66
expandEnvs?: boolean;
7+
recursive?: boolean;
78
fallback?: boolean;
89
file?: true | string;
910
override?: boolean;

src/env-cmd.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export async function EnvCmd(
4242

4343
if (options.recursive === true) {
4444
for (const key of Object.keys(env)) {
45-
env[key] = expandEnvs(env[key], env)
45+
if (env[key] !== undefined) {
46+
env[key] = expandEnvs(env[key], env)
47+
}
4648
}
4749
}
4850

src/parse-args.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function parseArgs(args: string[]): EnvCmdOptions {
3434
expandEnvs = true
3535
}
3636
let recursive = false
37-
if (program.recursive === true) {
37+
if (parsedCmdOptions.recursive === true) {
3838
recursive = true
3939
}
4040
let verbose = false

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type RCEnvironment = Partial<Record<string, Environment>>
88
export type CommanderOptions = Command<[], {
99
environments?: true | string[]
1010
expandEnvs?: boolean // Default: false
11+
recursive?: boolean // Default: false
1112
fallback?: boolean // Default false
1213
file?: true | string
1314
override?: boolean // Default: false

test/env-cmd.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('EnvCmd', (): void => {
171171

172172
it('should spawn process with args expanded if recursive option is true',
173173
async (): Promise<void> => {
174-
getEnvVarsStub.returns({ PING: 'PONG', recursive: 'PING ${PING}' }) /* eslint-disable-line */
174+
getEnvVarsStub.returns({ PING: 'PONG', recursive: 'PING ${PING}' })
175175
await envCmdLib.EnvCmd({
176176
command: 'node',
177177
commandArgs: [],

0 commit comments

Comments
 (0)