Skip to content

Commit 3a63ecb

Browse files
ruyadornoisaacs
authored andcommitted
feat: add ability to skip pre/post hooks
Implement support to `ignoreScripts` config option in `npm run-script` allowing for skipping pre and post hooks when used as discussed in RFC-0029. ref: https://github.com/npm/rfcs/blob/ed67c2bc41873d8f1954f8ce4a8f00437f5f14eb/accepted/0029-add-ability-to-skip-hooks.md PR-URL: #1718 Credit: @ruyadorno Close: #1718 Reviewed-by: @isaacs
1 parent 50f9740 commit 3a63ecb

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

lib/run-script.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ const runScript = async (args) => {
5656

5757
// positional args only added to the main event, not pre/post
5858
const events = [[event, args]]
59-
if (scripts[`pre${event}`]) {
60-
events.unshift([`pre${event}`, []])
61-
}
62-
if (scripts[`post${event}`]) {
63-
events.push([`post${event}`, []])
59+
if (!npm.flatOptions.ignoreScripts) {
60+
if (scripts[`pre${event}`]) {
61+
events.unshift([`pre${event}`, []])
62+
}
63+
if (scripts[`post${event}`]) {
64+
events.push([`post${event}`, []])
65+
}
6466
}
6567

6668
const opts = {

test/lib/run-script.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,45 @@ t.test('run pre/post hooks', async t => {
223223
RUN_SCRIPTS.length = 0
224224
})
225225

226+
t.test('skip pre/post hooks when using ignoreScripts', async t => {
227+
npm.flatOptions.ignoreScripts = true
228+
229+
npm.localPrefix = t.testdir({
230+
'package.json': JSON.stringify({
231+
name: 'x',
232+
version: '1.2.3',
233+
scripts: {
234+
preenv: 'echo before the env',
235+
postenv: 'echo after the env'
236+
}
237+
})
238+
})
239+
240+
await runScript(['env'], er => {
241+
if (er) {
242+
throw er
243+
}
244+
t.deepEqual(RUN_SCRIPTS, [
245+
{
246+
path: npm.localPrefix,
247+
args: [],
248+
scriptShell: undefined,
249+
stdio: 'inherit',
250+
stdioString: true,
251+
pkg: { name: 'x', version: '1.2.3', _id: '[email protected]', scripts: {
252+
preenv: 'echo before the env',
253+
postenv: 'echo after the env',
254+
env: 'env'
255+
} },
256+
event: 'env'
257+
}
258+
])
259+
260+
delete npm.flatOptions.ignoreScripts
261+
})
262+
RUN_SCRIPTS.length = 0
263+
})
264+
226265
t.test('run silent', async t => {
227266
npmlog.level = 'silent'
228267
t.teardown(() => { npmlog.level = 'warn' })

0 commit comments

Comments
 (0)