Skip to content

Commit ef687f5

Browse files
isaacswraithgar
authored andcommitted
fix(env): Do not clobber defined 'env' script
If an env script is already defined, run that instead of the default. PR-URL: #2655 Credit: @isaacs Close: #2655 Reviewed-by: @ljharb
1 parent e815cd4 commit ef687f5

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

lib/run-script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const runScript = async (args) => {
4141

4242
if (event === 'restart' && !scripts.restart)
4343
scripts.restart = 'npm stop --if-present && npm start'
44-
else if (event === 'env')
44+
else if (event === 'env' && !scripts.env)
4545
scripts.env = isWindowsShell ? 'SET' : 'env'
4646

4747
pkg.scripts = scripts

package-lock.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"@npmcli/arborist": "^2.2.1",
4646
"@npmcli/ci-detect": "^1.2.0",
4747
"@npmcli/config": "^1.2.9",
48-
"@npmcli/installed-package-contents": "^1.0.7",
4948
"@npmcli/run-script": "^1.8.2",
5049
"abbrev": "~1.1.1",
5150
"ansicolors": "~0.3.2",

test/lib/run-script.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,14 @@ t.test('default env, start, and restart scripts', async t => {
126126
scriptShell: undefined,
127127
stdio: 'inherit',
128128
stdioString: true,
129-
pkg: { name: 'x',
129+
pkg: {
130+
name: 'x',
130131
version: '1.2.3',
131132
_id: 'x@1.2.3',
132133
scripts: {
133134
env: 'env',
134-
} },
135+
},
136+
},
135137
event: 'env',
136138
},
137139
])
@@ -185,6 +187,67 @@ t.test('default env, start, and restart scripts', async t => {
185187
RUN_SCRIPTS.length = 0
186188
})
187189

190+
t.test('non-default env script', async t => {
191+
npm.localPrefix = t.testdir({
192+
'package.json': JSON.stringify({
193+
name: 'x',
194+
version: '1.2.3',
195+
scripts: {
196+
env: 'hello',
197+
},
198+
}),
199+
})
200+
201+
await runScript(['env'], er => {
202+
if (er)
203+
throw er
204+
205+
t.match(RUN_SCRIPTS, [
206+
{
207+
path: npm.localPrefix,
208+
args: [],
209+
scriptShell: undefined,
210+
stdio: 'inherit',
211+
stdioString: true,
212+
pkg: {
213+
name: 'x',
214+
version: '1.2.3',
215+
_id: 'x@1.2.3',
216+
scripts: {
217+
env: 'hello',
218+
},
219+
},
220+
event: 'env',
221+
},
222+
])
223+
})
224+
RUN_SCRIPTS.length = 0
225+
226+
await runScriptWin(['env'], er => {
227+
if (er)
228+
throw er
229+
230+
t.match(RUN_SCRIPTS, [
231+
{
232+
path: npm.localPrefix,
233+
args: [],
234+
scriptShell: undefined,
235+
stdio: 'inherit',
236+
stdioString: true,
237+
pkg: { name: 'x',
238+
version: '1.2.3',
239+
_id: 'x@1.2.3',
240+
scripts: {
241+
env: 'hello',
242+
},
243+
},
244+
event: 'env',
245+
},
246+
])
247+
})
248+
RUN_SCRIPTS.length = 0
249+
})
250+
188251
t.test('try to run missing script', t => {
189252
npm.localPrefix = t.testdir({
190253
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)