Skip to content

Commit e4e8ae2

Browse files
winterqtwraithgar
authored andcommitted
fix(libnpmpack): obey foregroundScripts
1 parent 92e94e3 commit e4e8ae2

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

DEPENDENCIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ graph LR;
575575
libnpmpack-->npmcli-run-script["@npmcli/run-script"];
576576
libnpmpack-->npmcli-template-oss["@npmcli/template-oss"];
577577
libnpmpack-->pacote;
578+
libnpmpack-->spawk;
578579
libnpmpack-->tap;
579580
libnpmpublish-->libnpmpack;
580581
libnpmpublish-->lodash.clonedeep;

workspaces/libnpmpack/lib/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ async function pack (spec = 'file:.', opts = {}) {
1919
// mode
2020
const banner = !opts.silent
2121

22+
const stdio = opts.foregroundScripts ? 'inherit' : 'pipe'
23+
2224
if (spec.type === 'directory') {
2325
// prepack
2426
await runScript({
2527
...opts,
2628
event: 'prepack',
2729
path: spec.fetchSpec,
28-
stdio: 'inherit',
30+
stdio,
2931
pkg: manifest,
3032
banner,
3133
})
@@ -52,7 +54,7 @@ async function pack (spec = 'file:.', opts = {}) {
5254
...opts,
5355
event: 'postpack',
5456
path: spec.fetchSpec,
55-
stdio: 'inherit',
57+
stdio,
5658
pkg: manifest,
5759
banner,
5860
env: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const spawk = require('spawk')
4+
5+
module.exports = tspawk
6+
7+
function tspawk (t) {
8+
t.teardown(function () {
9+
spawk.unload()
10+
})
11+
t.afterEach(function () {
12+
spawk.done()
13+
})
14+
return spawk
15+
}

workspaces/libnpmpack/test/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
'use strict'
22

33
const t = require('tap')
4+
5+
const tspawk = require('./fixtures/tspawk.js')
6+
const spawk = tspawk(t)
7+
48
const fs = require('fs')
59
const path = require('path')
610
const pack = require('../lib/index.js')
711
const tnock = require('./fixtures/tnock.js')
12+
const { load: loadMockNpm } = require('../../../test/fixtures/mock-npm.js')
813

914
const OPTS = {
1015
registry: 'https://mock.reg/',
1116
}
1217

1318
const REG = OPTS.registry
1419

20+
// TODO this ... smells. npm "script-shell" config mentions defaults but those
21+
// are handled by run-script, not npm. So for now we have to tie tests to some
22+
// pretty specific internals of runScript
23+
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')
24+
1525
t.test('packs from local directory', async t => {
1626
const testDir = t.testdir({
1727
'package.json': JSON.stringify({
@@ -128,3 +138,40 @@ t.test('packs from registry spec', async t => {
128138
const tarball = await pack(spec, { ...OPTS })
129139
t.ok(tarball)
130140
})
141+
142+
t.test('runs scripts in foreground when foregroundScripts === true', async t => {
143+
const { npm } = await loadMockNpm(t, {})
144+
145+
const testDir = t.testdir({
146+
'package.json': JSON.stringify({
147+
name: 'my-cool-pkg',
148+
version: '1.0.0',
149+
scripts: {
150+
prepack: 'touch prepack',
151+
postpack: 'touch postpack',
152+
},
153+
}, null, 2),
154+
})
155+
156+
const cwd = process.cwd()
157+
process.chdir(testDir)
158+
159+
const [scriptShell, scriptArgs] = makeSpawnArgs({
160+
event: 'prepack',
161+
path: npm.prefix,
162+
cmd: 'touch prepack',
163+
})
164+
165+
const prepack = spawk.spawn(scriptShell, scriptArgs)
166+
167+
await pack('file:.', {
168+
packDestination: testDir,
169+
foregroundScripts: true,
170+
})
171+
172+
t.ok(prepack.called)
173+
174+
t.teardown(async () => {
175+
process.chdir(cwd)
176+
})
177+
})

0 commit comments

Comments
 (0)