From ef687f545b177d0496ce74faacf1bf738978355a Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 8 Feb 2021 15:16:01 -0800 Subject: [PATCH] fix(env): Do not clobber defined 'env' script If an env script is already defined, run that instead of the default. PR-URL: https://github.com/npm/cli/pull/2655 Credit: @isaacs Close: #2655 Reviewed-by: @ljharb --- lib/run-script.js | 2 +- package-lock.json | 2 -- package.json | 1 - test/lib/run-script.js | 67 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/lib/run-script.js b/lib/run-script.js index c095e6decd403..7aab07f5daebd 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -41,7 +41,7 @@ const runScript = async (args) => { if (event === 'restart' && !scripts.restart) scripts.restart = 'npm stop --if-present && npm start' - else if (event === 'env') + else if (event === 'env' && !scripts.env) scripts.env = isWindowsShell ? 'SET' : 'env' pkg.scripts = scripts diff --git a/package-lock.json b/package-lock.json index 3f5aec15eafa2..fe81b05f395f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -81,7 +81,6 @@ "@eslint/eslintrc", "@npmcli/disparity-colors", "@npmcli/git", - "@npmcli/installed-package-contents", "@npmcli/map-workspaces", "@npmcli/metavuln-calculator", "@npmcli/move-file", @@ -361,7 +360,6 @@ "@npmcli/arborist": "^2.2.1", "@npmcli/ci-detect": "^1.2.0", "@npmcli/config": "^1.2.9", - "@npmcli/installed-package-contents": "^1.0.7", "@npmcli/run-script": "^1.8.2", "abbrev": "~1.1.1", "ansicolors": "~0.3.2", diff --git a/package.json b/package.json index 8f88726ad1e04..20c719bc68104 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "@npmcli/arborist": "^2.2.1", "@npmcli/ci-detect": "^1.2.0", "@npmcli/config": "^1.2.9", - "@npmcli/installed-package-contents": "^1.0.7", "@npmcli/run-script": "^1.8.2", "abbrev": "~1.1.1", "ansicolors": "~0.3.2", diff --git a/test/lib/run-script.js b/test/lib/run-script.js index bad8a63c0d778..070f766b44d80 100644 --- a/test/lib/run-script.js +++ b/test/lib/run-script.js @@ -126,12 +126,14 @@ t.test('default env, start, and restart scripts', async t => { scriptShell: undefined, stdio: 'inherit', stdioString: true, - pkg: { name: 'x', + pkg: { + name: 'x', version: '1.2.3', _id: 'x@1.2.3', scripts: { env: 'env', - } }, + }, + }, event: 'env', }, ]) @@ -185,6 +187,67 @@ t.test('default env, start, and restart scripts', async t => { RUN_SCRIPTS.length = 0 }) +t.test('non-default env script', async t => { + npm.localPrefix = t.testdir({ + 'package.json': JSON.stringify({ + name: 'x', + version: '1.2.3', + scripts: { + env: 'hello', + }, + }), + }) + + await runScript(['env'], er => { + if (er) + throw er + + t.match(RUN_SCRIPTS, [ + { + path: npm.localPrefix, + args: [], + scriptShell: undefined, + stdio: 'inherit', + stdioString: true, + pkg: { + name: 'x', + version: '1.2.3', + _id: 'x@1.2.3', + scripts: { + env: 'hello', + }, + }, + event: 'env', + }, + ]) + }) + RUN_SCRIPTS.length = 0 + + await runScriptWin(['env'], er => { + if (er) + throw er + + t.match(RUN_SCRIPTS, [ + { + path: npm.localPrefix, + args: [], + scriptShell: undefined, + stdio: 'inherit', + stdioString: true, + pkg: { name: 'x', + version: '1.2.3', + _id: 'x@1.2.3', + scripts: { + env: 'hello', + }, + }, + event: 'env', + }, + ]) + }) + RUN_SCRIPTS.length = 0 +}) + t.test('try to run missing script', t => { npm.localPrefix = t.testdir({ 'package.json': JSON.stringify({