Skip to content

Commit

Permalink
fix(env): Do not clobber defined 'env' script
Browse files Browse the repository at this point in the history
If an env script is already defined, run that
instead of the default.

PR-URL: #2655
Credit: @isaacs
Close: #2655
Reviewed-by: @ljharb
  • Loading branch information
isaacs authored and wraithgar committed Feb 11, 2021
1 parent e815cd4 commit ef687f5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/run-script.js
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
67 changes: 65 additions & 2 deletions test/lib/run-script.js
Expand Up @@ -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',
},
])
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit ef687f5

Please sign in to comment.