fix(recursive): cleaned up after rebase

This commit is contained in:
Todd Bluhm 2025-07-02 23:02:12 -08:00
parent f47b9f3c63
commit 18e8a28148
No known key found for this signature in database
GPG Key ID: 9CF312607477B8AB
11 changed files with 34 additions and 19 deletions

View File

@ -1,9 +1,10 @@
# Changelog
## 10.1.1 - In Development
## Landed in master
- **Upgrade**: Upgraded dependency `commander` to `5.x`
- **Upgrade**: Upgraded devDependencies `ts-standard`, `sinon`
- **Upgrade**: Upgraded dependency `commander` to `13.x`
- **Upgrade**: Upgraded dependency `cross-spawn` to `7.x`
- **Upgrade**: Upgraded all devDependencies `ts-standard`, `sinon`
- **Feature**: support both `$var` and `${var}` when expanding vars
- **Feature**: Added support for nested env variables with the `--recursive` flag

4
dist/env-cmd.js vendored
View File

@ -31,7 +31,9 @@ export async function EnvCmd({ command, commandArgs, envFile, rc, options = {},
}
if (options.recursive === true) {
for (const key of Object.keys(env)) {
env[key] = expand_envs_1.expandEnvs(env[key], env);
if (env[key] !== undefined) {
env[key] = expandEnvs(env[key], env);
}
}
}
if (options.expandEnvs === true) {

View File

@ -1,6 +1,6 @@
import type { Environment } from './types.ts';
/**
* expandEnvs Replaces $var in args and command with environment variables
* if the environment variable doesn't exist, it leaves it as is.
* expandEnvs Replaces $var and ${var} in args and command with environment variables
* the environment variable doesn't exist, it leaves it as is.
*/
export declare function expandEnvs(str: string, envs: Environment): string;

9
dist/expand-envs.js vendored
View File

@ -1,11 +1,10 @@
/**
* expandEnvs Replaces $var in args and command with environment variables
* if the environment variable doesn't exist, it leaves it as is.
* expandEnvs Replaces $var and ${var} in args and command with environment variables
* 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) => {
const varValue = envs[varName.slice(1)];
// const test = 42;
return str.replace(/(?<!\\)\$\{?[a-zA-Z0-9_]+\}?/g, (varName) => {
const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)];
return varValue ?? varName;
});
}

2
dist/parse-args.js vendored
View File

@ -29,7 +29,7 @@ export function parseArgs(args) {
expandEnvs = true;
}
let recursive = false;
if (program.recursive === true) {
if (parsedCmdOptions.recursive === true) {
recursive = true;
}
let verbose = false;

1
dist/types.d.ts vendored
View File

@ -4,6 +4,7 @@ export type RCEnvironment = Partial<Record<string, Environment>>;
export type CommanderOptions = Command<[], {
environments?: true | string[];
expandEnvs?: boolean;
recursive?: boolean;
fallback?: boolean;
file?: true | string;
override?: boolean;

View File

@ -42,7 +42,9 @@ export async function EnvCmd(
if (options.recursive === true) {
for (const key of Object.keys(env)) {
env[key] = expandEnvs(env[key], env)
if (env[key] !== undefined) {
env[key] = expandEnvs(env[key], env)
}
}
}

View File

@ -34,7 +34,7 @@ export function parseArgs(args: string[]): EnvCmdOptions {
expandEnvs = true
}
let recursive = false
if (program.recursive === true) {
if (parsedCmdOptions.recursive === true) {
recursive = true
}
let verbose = false

View File

@ -8,6 +8,7 @@ export type RCEnvironment = Partial<Record<string, Environment>>
export type CommanderOptions = Command<[], {
environments?: true | string[]
expandEnvs?: boolean // Default: false
recursive?: boolean // Default: false
fallback?: boolean // Default false
file?: true | string
override?: boolean // Default: false

View File

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

View File

@ -8,11 +8,20 @@ describe('expandEnvs', (): void => {
dollar: 'money',
PING: 'PONG',
IP1: '127.0.0.1',
THANKSFORALLTHEFISH: 42,
BRINGATOWEL: true,
THANKSFORALLTHEFISH: '42',
BRINGATOWEL: 'true',
}
const args = ['notvar', '$dollar', '\\$notvar', '-4', '$PING', '$IP1', '\\$IP1', '$NONEXIST', '${PING}', '${NONEXIST}'] /* eslint-disable-line */
const argsExpanded = ['notvar', 'money', '\\$notvar', '-4', 'PONG', '127.0.0.1', '\\$IP1', '$NONEXIST', 'PONG', '${NONEXIST}'] /* eslint-disable-line */
const args = [
'notvar', '$dollar', '\\$notvar', '-4',
'$PING', '$IP1', '\\$IP1', '$NONEXIST',
'${PING}', '${NONEXIST}'
]
const argsExpanded = [
'notvar', 'money', '\\$notvar', '-4',
'PONG', '127.0.0.1', '\\$IP1', '$NONEXIST',
'PONG', '${NONEXIST}'
]
it('should replace environment variables in args', (): void => {
const res = args.map(arg => expandEnvs(arg, envs))