From d64512c632e1ed12f5bffe8a1f50d235c18f0196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Yi=C4=9Fit=20Kaya?= Date: Mon, 18 Sep 2017 22:07:28 +0100 Subject: [PATCH] Fix: Better npm_config_argv emulation (#4479) **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. --- .../npm_config_argv_env_vars/log-command.js | 8 +------- __tests__/lifecycle-scripts.js | 8 ++++---- src/cli/index.js | 11 ++++------- src/util/execute-lifecycle-script.js | 4 ++-- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/__tests__/fixtures/lifecycle-scripts/npm_config_argv_env_vars/log-command.js b/__tests__/fixtures/lifecycle-scripts/npm_config_argv_env_vars/log-command.js index 725005575a..85e27480a9 100644 --- a/__tests__/fixtures/lifecycle-scripts/npm_config_argv_env_vars/log-command.js +++ b/__tests__/fixtures/lifecycle-scripts/npm_config_argv_env_vars/log-command.js @@ -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']); diff --git a/__tests__/lifecycle-scripts.js b/__tests__/lifecycle-scripts.js index d059622162..1132a70fe1 100644 --- a/__tests__/lifecycle-scripts.js +++ b/__tests__/lifecycle-scripts.js @@ -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"'); }, ); diff --git a/src/cli/index.js b/src/cli/index.js index 2c5cb74ffc..7db71539fc 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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