Skip to content

Commit

Permalink
Fix: Better npm_config_argv emulation (#4479)
Browse files Browse the repository at this point in the history
**Summary**

Fixes #2226. Better emulates `npm_config_argv` by passing
`process.argv.slice(2)` as the `original` portion and both the
command name and the script name in `cooked` portion.

**Test case**

Added integration tests.
  • Loading branch information
BYK authored Sep 18, 2017
1 parent 3124c91 commit d64512c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
try {
const argv = JSON.parse(process.env['npm_config_argv']);

console.log(`##${argv.cooked[0]}##`);
} catch (err) {
console.log(`##${err.toString()}##`);
}
console.log(process.env['npm_config_argv']);
8 changes: 4 additions & 4 deletions __tests__/lifecycle-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ test.concurrent(
execCommand('test', 'npm_config_argv_env_vars', env),
]);

expect(stdouts[0]).toContain('##install##');
expect(stdouts[1]).toContain('##install##');
expect(stdouts[2]).toContain('##test##');
expect(stdouts[3]).toContain('##test##');
expect(stdouts[0]).toContain('"install"');
expect(stdouts[1]).toContain('"install"');
expect(stdouts[2]).toContain('"run","test"');
expect(stdouts[3]).toContain('"run","test"');
},
);

Expand Down
11 changes: 4 additions & 7 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,17 @@ export function main({
setHelpMode();
}

let command;
if (!commandName) {
commandName = 'install';
isKnownCommand = true;
}

if (isKnownCommand) {
command = commands[commandName];
} else {
if (!isKnownCommand) {
// if command is not recognized, then set default to `run`
args.unshift(commandName);
command = commands.run;
commandName = 'run';
}
const command = commands[commandName];

let warnAboutRunDashDash = false;
// we are using "yarn <script> -abc" or "yarn run <script> -abc", we want -abc to be script options, not yarn options
Expand Down Expand Up @@ -448,6 +446,7 @@ export function main({
config
.init({
cwd,
commandName,

binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
Expand All @@ -469,8 +468,6 @@ export function main({
networkTimeout: commander.networkTimeout,
nonInteractive: commander.nonInteractive,
scriptsPrependNodePath: commander.scriptsPrependNodePath,

commandName: commandName === 'run' ? commander.args[0] : commandName,
})
.then(() => {
// option "no-progress" stored in yarn config
Expand Down
4 changes: 2 additions & 2 deletions src/util/execute-lifecycle-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export async function makeEnv(
// parser used by npm. Since we use other parser, we just roughly emulate it's output. (See: #684)
env.npm_config_argv = JSON.stringify({
remain: [],
cooked: [config.commandName],
original: [config.commandName],
cooked: config.commandName === 'run' ? [config.commandName, stage] : [config.commandName],
original: process.argv.slice(2),
});

const manifest = await config.maybeReadManifest(cwd);
Expand Down

0 comments on commit d64512c

Please sign in to comment.