Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not clobber defined 'env' script with default #2655

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

2 changes: 1 addition & 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 Expand Up @@ -115,6 +114,7 @@
"@npmcli/arborist",
"@npmcli/ci-detect",
"@npmcli/config",
"@npmcli/installed-package-contents",
"@npmcli/run-script",
"abbrev",
"ansicolors",
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