Skip to content

Commit

Permalink
fix(pack): default foreground-scripts to true
Browse files Browse the repository at this point in the history
Fixes #6816
  • Loading branch information
ljharb committed Jan 18, 2024
1 parent 162c82e commit 4188dcb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Pack extends BaseCommand {
static workspaces = true
static ignoreImplicitWorkspace = false

constructor (...args) {
super(...args)
this.npm.config.set('foreground-scripts', true, 'default')
}

async exec (args) {
if (args.length === 0) {
args = ['.']
Expand Down
21 changes: 21 additions & 0 deletions tap-snapshots/test/lib/commands/pack.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ Array [
]
`

exports[`test/lib/commands/pack.js TAP foreground-scripts defaults to true > logs pack contents 1`] = `
Array [
undefined,
"package: test-fg-scripts@0.0.0",
undefined,
"110B package.json",
undefined,
String(
name: test-fg-scripts
version: 0.0.0
filename: test-fg-scripts-0.0.0.tgz
package size: {size}
unpacked size: 110 B
shasum: {sha}
integrity: {integrity}
total files: 1
),
"",
]
`

exports[`test/lib/commands/pack.js TAP should log output as valid json > logs pack contents 1`] = `
Array []
`
Expand Down
42 changes: 42 additions & 0 deletions test/lib/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,48 @@ t.test('dry run', async t => {
t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
})

t.test('foreground-scripts defaults to true', async t => {
const { npm, outputs, logs } = await loadMockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'test-fg-scripts',
version: '0.0.0',
scripts: {
prepack: 'echo prepack!',
postpack: 'echo postpack!',
},
}
),
},
config: { 'dry-run': true },
})

/* eslint no-console: 0 */
// TODO: replace this with `const results = t.intercept(console, 'log')`
const log = console.log
t.teardown(() => {
console.log = log
})
const caughtLogs = []
console.log = (...args) => {
caughtLogs.push(args)
}
// end TODO

await npm.exec('pack', [])
const filename = 'test-fg-scripts-0.0.0.tgz'
t.same(
caughtLogs,
[
['\n> test-fg-scripts@0.0.0 prepack\n> echo prepack!\n'],
['\n> test-fg-scripts@0.0.0 postpack\n> echo postpack!\n'],
],
'prepack and postpack log to stdout')
t.strictSame(outputs, [[filename]])
t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
})

t.test('invalid packument', async t => {
const { npm, outputs } = await loadMockNpm(t, {
prefixDir: {
Expand Down

0 comments on commit 4188dcb

Please sign in to comment.