Skip to content

Commit b50ce9f

Browse files
authored
Merge pull request #400 from toddbluhm/dependabot/npm_and_yarn/commander-13.1.0
chore(deps): bump commander from 12.1.0 to 13.1.0
2 parents 97ecb90 + 1e71026 commit b50ce9f

File tree

7 files changed

+58
-24
lines changed

7 files changed

+58
-24
lines changed

dist/env-cmd.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ export async function EnvCmd({ command, commandArgs, envFile, rc, options = {},
3333
command = expandEnvs(command, env);
3434
commandArgs = commandArgs.map(arg => expandEnvs(arg, env));
3535
}
36+
if (!command) {
37+
throw new Error('env-cmd cannot be used as a standalone command. ' +
38+
'Refer to the documentation for usage examples: https://npm.im/env-cmd');
39+
}
3640
// Execute the command with the given environment variables
3741
const proc = spawn(command, commandArgs, {
3842
stdio: 'inherit',
3943
shell: options.useShell,
40-
env: env,
44+
env,
4145
});
4246
// Handle any termination signals for parent and child proceses
4347
const signals = new TermSignals({ verbose: options.verbose });

dist/parse-args.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ export function parseArgsUsingCommander(args) {
9494
.option('--use-shell', 'Execute the command in a new shell with the given environment')
9595
.option('--verbose', 'Print helpful debugging information')
9696
.allowUnknownOption(true)
97+
.allowExcessArguments(true)
9798
.parse(['_', '_', ...args], { from: 'node' });
9899
}

dist/parse-env-file.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ export declare function stripComments(envString: string): string;
1919
* Strips out newlines from env file string
2020
*/
2121
export declare function stripEmptyLines(envString: string): string;
22+
/**
23+
* If we load data from a file like .js, the user
24+
* might export something which is not an object.
25+
*
26+
* This function ensures that the input is valid,
27+
* and converts the object's values to strings, for
28+
* consistincy. See issue #125 for details.
29+
*/
30+
export declare function normalizeEnvObject(input: unknown, absolutePath: string): Environment;

dist/parse-env-file.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ export async function getEnvFileVars(envFilePath) {
1414
}
1515
// Get the file extension
1616
const ext = extname(absolutePath).toLowerCase();
17-
let env = {};
17+
let env;
1818
if (IMPORT_HOOK_EXTENSIONS.includes(ext)) {
1919
// For some reason in ES Modules, only JSON file types need to be specifically delinated when importing them
2020
let attributeTypes = {};
2121
if (ext === '.json') {
2222
attributeTypes = { [importAttributesKeyword]: { type: 'json' } };
2323
}
2424
const res = await import(pathToFileURL(absolutePath).href, attributeTypes);
25-
if ('default' in res) {
25+
if (typeof res === 'object' && res && 'default' in res) {
2626
env = res.default;
2727
}
2828
else {
@@ -32,12 +32,14 @@ export async function getEnvFileVars(envFilePath) {
3232
if (isPromise(env)) {
3333
env = await env;
3434
}
35+
return normalizeEnvObject(env, absolutePath);
3536
}
36-
else {
37-
const file = readFileSync(absolutePath, { encoding: 'utf8' });
38-
env = parseEnvString(file);
37+
const file = readFileSync(absolutePath, { encoding: 'utf8' });
38+
switch (ext) {
39+
// other loaders can be added here
40+
default:
41+
return parseEnvString(file);
3942
}
40-
return env;
4143
}
4244
/**
4345
* Parse out all env vars from a given env file string and return an object
@@ -61,23 +63,18 @@ export function parseEnvVars(envString) {
6163
// Note: match[1] is the full env=var line
6264
const key = match[2].trim();
6365
let value = match[3].trim();
64-
// remove any surrounding quotes
65-
value = value
66-
.replace(/(^['"]|['"]$)/g, '')
67-
.replace(/\\n/g, '\n');
68-
// Convert string to JS type if appropriate
69-
if (value !== '' && !isNaN(+value)) {
70-
matches[key] = +value;
71-
}
72-
else if (value === 'true') {
73-
matches[key] = true;
74-
}
75-
else if (value === 'false') {
76-
matches[key] = false;
66+
// if the string is quoted, remove everything after the final
67+
// quote. This implicitly removes inline comments.
68+
if (value.startsWith("'") || value.startsWith('"')) {
69+
value = value.slice(1, value.lastIndexOf(value[0]));
7770
}
7871
else {
79-
matches[key] = value;
72+
// if the string is not quoted, we need to explicitly remove
73+
// inline comments.
74+
value = value.split('#')[0].trim();
8075
}
76+
value = value.replace(/\\n/g, '\n');
77+
matches[key] = value;
8178
}
8279
return JSON.parse(JSON.stringify(matches));
8380
}
@@ -95,3 +92,25 @@ export function stripEmptyLines(envString) {
9592
const emptyLinesRegex = /(^\n)/gim;
9693
return envString.replace(emptyLinesRegex, '');
9794
}
95+
/**
96+
* If we load data from a file like .js, the user
97+
* might export something which is not an object.
98+
*
99+
* This function ensures that the input is valid,
100+
* and converts the object's values to strings, for
101+
* consistincy. See issue #125 for details.
102+
*/
103+
export function normalizeEnvObject(input, absolutePath) {
104+
if (typeof input !== 'object' || !input) {
105+
throw new Error(`env-cmd cannot load “${absolutePath}” because it does not export an object.`);
106+
}
107+
const env = {};
108+
for (const [key, value] of Object.entries(input)) {
109+
// we're intentionally stringifying the value here, to
110+
// match what `child_process.spawn` does when loading
111+
// env variables.
112+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
113+
env[key] = `${value}`;
114+
}
115+
return env;
116+
}

dist/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command } from '@commander-js/extra-typings';
2-
export type Environment = Partial<Record<string, string | number | boolean>>;
2+
export type Environment = Partial<Record<string, string>>;
33
export type RCEnvironment = Partial<Record<string, Environment>>;
44
export type CommanderOptions = Command<[], {
55
environments?: true | string[];

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
},
5050
"homepage": "https://github.com/toddbluhm/env-cmd#readme",
5151
"dependencies": {
52-
"@commander-js/extra-typings": "^12.1.0",
53-
"commander": "^12.1.0",
52+
"@commander-js/extra-typings": "^13.1.0",
53+
"commander": "^13.1.0",
5454
"cross-spawn": "^7.0.6"
5555
},
5656
"devDependencies": {

src/parse-args.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ export function parseArgsUsingCommander(args: string[]): CommanderOptions {
108108
.option('--use-shell', 'Execute the command in a new shell with the given environment')
109109
.option('--verbose', 'Print helpful debugging information')
110110
.allowUnknownOption(true)
111+
.allowExcessArguments(true)
111112
.parse(['_', '_', ...args], { from: 'node' })
112113
}

0 commit comments

Comments
 (0)