diff options
author | Winter <winter@winter.cafe> | 2022-10-03 04:37:59 +0300 |
---|---|---|
committer | Gar <wraithgar@github.com> | 2022-10-05 21:32:08 +0300 |
commit | e4e8ae20aef9e27e57282e87e8757d5b364abb39 (patch) | |
tree | 97e7dbb5b0ee782c55872a680927c7483aa85844 | |
parent | 92e94e3e891e602b8cbac2b39bedaf297a84f102 (diff) |
fix(libnpmpack): obey foregroundScripts
-rw-r--r-- | DEPENDENCIES.md | 1 | ||||
-rw-r--r-- | workspaces/libnpmpack/lib/index.js | 6 | ||||
-rw-r--r-- | workspaces/libnpmpack/test/fixtures/tspawk.js | 15 | ||||
-rw-r--r-- | workspaces/libnpmpack/test/index.js | 47 |
4 files changed, 67 insertions, 2 deletions
diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index 52525aa03..c945f9e18 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -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; diff --git a/workspaces/libnpmpack/lib/index.js b/workspaces/libnpmpack/lib/index.js index 93428b37c..baccadc33 100644 --- a/workspaces/libnpmpack/lib/index.js +++ b/workspaces/libnpmpack/lib/index.js @@ -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, }) @@ -52,7 +54,7 @@ async function pack (spec = 'file:.', opts = {}) { ...opts, event: 'postpack', path: spec.fetchSpec, - stdio: 'inherit', + stdio, pkg: manifest, banner, env: { diff --git a/workspaces/libnpmpack/test/fixtures/tspawk.js b/workspaces/libnpmpack/test/fixtures/tspawk.js new file mode 100644 index 000000000..6c1b481c8 --- /dev/null +++ b/workspaces/libnpmpack/test/fixtures/tspawk.js @@ -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 +} diff --git a/workspaces/libnpmpack/test/index.js b/workspaces/libnpmpack/test/index.js index d9ec1d12a..acbdfd947 100644 --- a/workspaces/libnpmpack/test/index.js +++ b/workspaces/libnpmpack/test/index.js @@ -1,10 +1,15 @@ '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/', @@ -12,6 +17,11 @@ const OPTS = { 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({ @@ -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) + }) +}) |