Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

winterqt/libnpmpack foreground scripts #5645

Merged
merged 3 commits into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions DEPENDENCIES.md
Expand Up @@ -403,6 +403,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
4 changes: 3 additions & 1 deletion package-lock.json
Expand Up @@ -10111,8 +10111,9 @@
},
"node_modules/spawk": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/spawk/-/spawk-1.7.1.tgz",
"integrity": "sha512-qkPqVdPp5ICEeSYKB/qCkwIBB0IWQuouEvYenQvpTq15fqSQgutpH453NjEImrpCWTwQwj2bQjGp8YGHapEiWw==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=12.0.0"
}
Expand Down Expand Up @@ -14020,6 +14021,7 @@
"@npmcli/eslint-config": "^3.1.0",
"@npmcli/template-oss": "4.4.5",
"nock": "^13.0.7",
"spawk": "^1.7.1",
"tap": "^16.0.1"
},
"engines": {
Expand Down
6 changes: 4 additions & 2 deletions workspaces/libnpmpack/lib/index.js
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
1 change: 1 addition & 0 deletions workspaces/libnpmpack/package.json
Expand Up @@ -25,6 +25,7 @@
"@npmcli/eslint-config": "^3.1.0",
"@npmcli/template-oss": "4.4.5",
"nock": "^13.0.7",
"spawk": "^1.7.1",
"tap": "^16.0.1"
},
"repository": {
Expand Down
16 changes: 16 additions & 0 deletions workspaces/libnpmpack/test/fixtures/tspawk.js
@@ -0,0 +1,16 @@
'use strict'

const spawk = require('spawk')

module.exports = tspawk

function tspawk (t) {
spawk.preventUnmatched()
t.teardown(function () {
spawk.unload()
})
t.afterEach(function () {
spawk.done()
})
return spawk
}
44 changes: 44 additions & 0 deletions workspaces/libnpmpack/test/index.js
@@ -1,6 +1,9 @@
'use strict'

const t = require('tap')

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

const fs = require('fs')
const path = require('path')
const pack = require('../lib/index.js')
Expand All @@ -12,6 +15,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({
Expand Down Expand Up @@ -128,3 +136,39 @@ 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 spawk = tspawk(t)

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

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

const [scriptShell, scriptArgs] = makeSpawnArgs({
event: 'prepack',
path: testDir,
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)
})
})