Skip to content

Commit

Permalink
fix(libnpmpack): obey foregroundScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
winterqt authored and wraithgar committed Oct 5, 2022
1 parent 92e94e3 commit e4e8ae2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ graph LR;
libnpmpack-->npmcli-run-script["@npmcli/run-script"];
libnpmpack-->npmcli-template-oss["@npmcli/template-oss"];
libnpmpack-->pacote;
libnpmpack-->spawk;
libnpmpack-->tap;
libnpmpublish-->libnpmpack;
libnpmpublish-->lodash.clonedeep;
Expand Down
6 changes: 4 additions & 2 deletions workspaces/libnpmpack/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ async function pack (spec = 'file:.', opts = {}) {
// mode
const banner = !opts.silent

const stdio = opts.foregroundScripts ? 'inherit' : 'pipe'

if (spec.type === 'directory') {
// prepack
await runScript({
...opts,
event: 'prepack',
path: spec.fetchSpec,
stdio: 'inherit',
stdio,
pkg: manifest,
banner,
})
Expand All @@ -52,7 +54,7 @@ async function pack (spec = 'file:.', opts = {}) {
...opts,
event: 'postpack',
path: spec.fetchSpec,
stdio: 'inherit',
stdio,
pkg: manifest,
banner,
env: {
Expand Down
15 changes: 15 additions & 0 deletions workspaces/libnpmpack/test/fixtures/tspawk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const spawk = require('spawk')

module.exports = tspawk

function tspawk (t) {
t.teardown(function () {
spawk.unload()
})
t.afterEach(function () {
spawk.done()
})
return spawk
}
47 changes: 47 additions & 0 deletions workspaces/libnpmpack/test/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
'use strict'

const t = require('tap')

const tspawk = require('./fixtures/tspawk.js')
const spawk = tspawk(t)

const fs = require('fs')
const path = require('path')
const pack = require('../lib/index.js')
const tnock = require('./fixtures/tnock.js')
const { load: loadMockNpm } = require('../../../test/fixtures/mock-npm.js')

const OPTS = {
registry: 'https://mock.reg/',
}

const REG = OPTS.registry

// TODO this ... smells. npm "script-shell" config mentions defaults but those
// are handled by run-script, not npm. So for now we have to tie tests to some
// pretty specific internals of runScript
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')

t.test('packs from local directory', async t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
Expand Down Expand Up @@ -128,3 +138,40 @@ t.test('packs from registry spec', async t => {
const tarball = await pack(spec, { ...OPTS })
t.ok(tarball)
})

t.test('runs scripts in foreground when foregroundScripts === true', async t => {
const { npm } = await loadMockNpm(t, {})

const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
version: '1.0.0',
scripts: {
prepack: 'touch prepack',
postpack: 'touch postpack',
},
}, null, 2),
})

const cwd = process.cwd()
process.chdir(testDir)

const [scriptShell, scriptArgs] = makeSpawnArgs({
event: 'prepack',
path: npm.prefix,
cmd: 'touch prepack',
})

const prepack = spawk.spawn(scriptShell, scriptArgs)

await pack('file:.', {
packDestination: testDir,
foregroundScripts: true,
})

t.ok(prepack.called)

t.teardown(async () => {
process.chdir(cwd)
})
})

0 comments on commit e4e8ae2

Please sign in to comment.