Skip to content

Commit 22d9858

Browse files
fix(cli): remove usage of deprecated npm env var from arg parser (#3486)
This removes some code from our argument parsing implementation which would look for an environment variable (`npm_config_argv`) set by npm and, if present, merge CLI flags found their into the ones already present on `process.argv`. We want to remove this for two reasons: - `npm_config_argv` is deprecated in `npm` v7+, so in newer versions of the package manager it is no longer set and our code referencing it is effectively doing nothing - `yarn` v1.x still sets it (presumably for compatibility with `npm` that hasn't been removed yet, which causes an inconsistency between `npm` and `yarn` where certain `package.json` scripts will run without issue in `npm` but fail in `yarn` (see #3482) Accordingly, this commit deletes the offending function from `src/cli/parse-flags.ts` and makes a few related changes at call sites, etc which are necessary due to that change. This commit also refactors the spec file for `parse-flags.ts` to remove redundant tests. Because all of the supported CLI arguments (i.e. those added to `knownArgs`) are defined in `ReadonlyArray<string>` variables we can do a lot of exhaustive testing of all the arguments, set in the various possible permutations, etc, so we don't need to define a bunch of individual tests. Accordingly, the test file is refactored to remove redundant tests, add a few more test cases for the exhaustive tests, organize things a bit more with `describe` blocks, and ensure that we have good tests around all off the 'edge-case-ey' things.
1 parent bbdebf4 commit 22d9858

File tree

5 files changed

+169
-539
lines changed

5 files changed

+169
-539
lines changed

src/cli/parse-flags.ts

+4-30
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import {
1919
/**
2020
* Parse command line arguments into a structured `ConfigFlags` object
2121
*
22-
* @param args an array of config flags
23-
* @param sys an optional compiler system
22+
* @param args an array of CLI flags
23+
* @param _sys an optional compiler system
2424
* @returns a structured ConfigFlags object
2525
*/
26-
export const parseFlags = (args: string[], sys?: CompilerSystem): ConfigFlags => {
26+
export const parseFlags = (args: string[], _sys?: CompilerSystem): ConfigFlags => {
27+
// TODO(STENCIL-509): remove the _sys parameter here ^^ (for v3)
2728
const flags: ConfigFlags = createConfigFlags();
2829

2930
// cmd line has more priority over npm scripts cmd
@@ -33,17 +34,6 @@ export const parseFlags = (args: string[], sys?: CompilerSystem): ConfigFlags =>
3334
}
3435
parseArgs(flags, flags.args);
3536

36-
if (sys && sys.name === 'node') {
37-
const envArgs = getNpmConfigEnvArgs(sys);
38-
parseArgs(flags, envArgs);
39-
40-
envArgs.forEach((envArg) => {
41-
if (!flags.args.includes(envArg)) {
42-
flags.args.push(envArg);
43-
}
44-
});
45-
}
46-
4737
if (flags.task != null) {
4838
const i = flags.args.indexOf(flags.task);
4939
if (i > -1) {
@@ -344,19 +334,3 @@ const isLogLevel = (maybeLogLevel: string): maybeLogLevel is LogLevel =>
344334
//
345335
// see microsoft/TypeScript#31018 for some discussion of this
346336
LOG_LEVELS.includes(maybeLogLevel as any);
347-
348-
const getNpmConfigEnvArgs = (sys: CompilerSystem) => {
349-
// process.env.npm_config_argv
350-
// {"remain":["4444"],"cooked":["run","serve","--port","4444"],"original":["run","serve","--port","4444"]}
351-
let args: string[] = [];
352-
try {
353-
const npmConfigArgs = sys.getEnvironmentVar('npm_config_argv');
354-
if (npmConfigArgs) {
355-
args = JSON.parse(npmConfigArgs).original as string[];
356-
if (args[0] === 'run') {
357-
args = args.slice(2);
358-
}
359-
}
360-
} catch (e) {}
361-
return args;
362-
};

src/cli/public.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export declare function run(init: CliInitOptions): Promise<void>;
1515
*/
1616
export declare function runTask(coreCompiler: any, config: Config, task: TaskCommand): Promise<void>;
1717

18-
export declare function parseFlags(args: string[], sys?: CompilerSystem): ConfigFlags;
18+
// TODO(STENCIL-509): remove the _sys parameter here (for v3)
19+
export declare function parseFlags(args: string[], _sys?: CompilerSystem): ConfigFlags;
1920

2021
export { CompilerSystem, Config, ConfigFlags, Logger, TaskCommand };

src/cli/run.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const run = async (init: d.CliInitOptions) => {
2323
const { args, logger, sys } = init;
2424

2525
try {
26-
const flags = parseFlags(args, sys);
26+
const flags = parseFlags(args);
2727
const task = flags.task;
2828

2929
if (flags.debug || flags.verbose) {

0 commit comments

Comments
 (0)