Skip to content

Commit 5995b95

Browse files
committed
fix(pack, publish): default foreground-scripts to true
Fixes #6816
1 parent a530215 commit 5995b95

File tree

8 files changed

+305
-1
lines changed

8 files changed

+305
-1
lines changed

lib/commands/pack.js

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class Pack extends BaseCommand {
4747
for (const { arg, manifest } of manifests) {
4848
const tarballData = await libpack(arg, {
4949
...this.npm.flatOptions,
50+
foregroundScripts: this.npm.config.isDefault('foreground-scripts')
51+
? true
52+
: this.npm.config.get('foreground-scripts'),
5053
prefix: this.npm.localPrefix,
5154
workspaces: this.workspacePaths,
5255
})

lib/commands/publish.js

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class Publish extends BaseCommand {
8080
// we pass dryRun: true to libnpmpack so it doesn't write the file to disk
8181
const tarballData = await pack(spec, {
8282
...opts,
83+
foregroundScripts: this.npm.config.isDefault('foreground-scripts')
84+
? true
85+
: this.npm.config.get('foreground-scripts'),
8386
dryRun: true,
8487
prefix: this.npm.localPrefix,
8588
workspaces: this.workspacePaths,

tap-snapshots/test/lib/commands/pack.js.test.cjs

+42
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,48 @@ Array [
2626
]
2727
`
2828

29+
exports[`test/lib/commands/pack.js TAP foreground-scripts can still be set to false > logs pack contents 1`] = `
30+
Array [
31+
undefined,
32+
"package: test-fg-scripts@0.0.0",
33+
undefined,
34+
"110B package.json",
35+
undefined,
36+
String(
37+
name: test-fg-scripts
38+
version: 0.0.0
39+
filename: test-fg-scripts-0.0.0.tgz
40+
package size: {size}
41+
unpacked size: 110 B
42+
shasum: {sha}
43+
integrity: {integrity}
44+
total files: 1
45+
),
46+
"",
47+
]
48+
`
49+
50+
exports[`test/lib/commands/pack.js TAP foreground-scripts defaults to true > logs pack contents 1`] = `
51+
Array [
52+
undefined,
53+
"package: test-fg-scripts@0.0.0",
54+
undefined,
55+
"110B package.json",
56+
undefined,
57+
String(
58+
name: test-fg-scripts
59+
version: 0.0.0
60+
filename: test-fg-scripts-0.0.0.tgz
61+
package size: {size}
62+
unpacked size: 110 B
63+
shasum: {sha}
64+
integrity: {integrity}
65+
total files: 1
66+
),
67+
"",
68+
]
69+
`
70+
2971
exports[`test/lib/commands/pack.js TAP should log output as valid json > logs pack contents 1`] = `
3072
Array []
3173
`

tap-snapshots/test/lib/commands/publish.js.test.cjs

+86
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,92 @@ Array [
5656
]
5757
`
5858

59+
exports[`test/lib/commands/publish.js TAP foreground-scripts can still be set to false > must match snapshot 1`] = `
60+
Array [
61+
Array [
62+
"",
63+
],
64+
Array [
65+
"",
66+
"package: test-fg-scripts@0.0.0",
67+
],
68+
Array [
69+
"=== Tarball Contents ===",
70+
],
71+
Array [
72+
"",
73+
"110B package.json",
74+
],
75+
Array [
76+
"=== Tarball Details ===",
77+
],
78+
Array [
79+
"",
80+
String(
81+
name: test-fg-scripts
82+
version: 0.0.0
83+
filename: test-fg-scripts-0.0.0.tgz
84+
package size: {size}
85+
unpacked size: 110 B
86+
shasum: {sha}
87+
integrity: {integrity}
88+
total files: 1
89+
),
90+
],
91+
Array [
92+
"",
93+
"",
94+
],
95+
Array [
96+
"",
97+
"Publishing to https://registry.npmjs.org/ with tag latest and default access (dry-run)",
98+
],
99+
]
100+
`
101+
102+
exports[`test/lib/commands/publish.js TAP foreground-scripts defaults to true > must match snapshot 1`] = `
103+
Array [
104+
Array [
105+
"",
106+
],
107+
Array [
108+
"",
109+
"package: test-fg-scripts@0.0.0",
110+
],
111+
Array [
112+
"=== Tarball Contents ===",
113+
],
114+
Array [
115+
"",
116+
"110B package.json",
117+
],
118+
Array [
119+
"=== Tarball Details ===",
120+
],
121+
Array [
122+
"",
123+
String(
124+
name: test-fg-scripts
125+
version: 0.0.0
126+
filename: test-fg-scripts-0.0.0.tgz
127+
package size: {size}
128+
unpacked size: 110 B
129+
shasum: {sha}
130+
integrity: {integrity}
131+
total files: 1
132+
),
133+
],
134+
Array [
135+
"",
136+
"",
137+
],
138+
Array [
139+
"",
140+
"Publishing to https://registry.npmjs.org/ with tag latest and default access (dry-run)",
141+
],
142+
]
143+
`
144+
59145
exports[`test/lib/commands/publish.js TAP has mTLS auth for scope configured registry > new package version 1`] = `
60146
+ @npm/test-package@1.0.0
61147
`

tap-snapshots/test/lib/docs.js.test.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ recommended that you do not use this option!
637637
638638
#### \`foreground-scripts\`
639639
640-
* Default: false
640+
* Default: \`false\` unless when using \`npm pack\` or \`npm publish\` where it
641+
defaults to \`true\`
641642
* Type: Boolean
642643
643644
Run all build scripts (ie, \`preinstall\`, \`install\`, and \`postinstall\`)

test/lib/commands/pack.js

+81
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,87 @@ t.test('dry run', async t => {
105105
t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
106106
})
107107

108+
t.test('foreground-scripts defaults to true', async t => {
109+
const { npm, outputs, logs } = await loadMockNpm(t, {
110+
prefixDir: {
111+
'package.json': JSON.stringify({
112+
name: 'test-fg-scripts',
113+
version: '0.0.0',
114+
scripts: {
115+
prepack: 'echo prepack!',
116+
postpack: 'echo postpack!',
117+
},
118+
}
119+
),
120+
},
121+
config: { 'dry-run': true },
122+
})
123+
124+
/* eslint no-console: 0 */
125+
// TODO: replace this with `const results = t.intercept(console, 'log')`
126+
const log = console.log
127+
t.teardown(() => {
128+
console.log = log
129+
})
130+
const caughtLogs = []
131+
console.log = (...args) => {
132+
caughtLogs.push(args)
133+
}
134+
// end TODO
135+
136+
await npm.exec('pack', [])
137+
const filename = 'test-fg-scripts-0.0.0.tgz'
138+
t.same(
139+
caughtLogs,
140+
[
141+
['\n> test-fg-scripts@0.0.0 prepack\n> echo prepack!\n'],
142+
['\n> test-fg-scripts@0.0.0 postpack\n> echo postpack!\n'],
143+
],
144+
'prepack and postpack log to stdout')
145+
t.strictSame(outputs, [[filename]])
146+
t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
147+
t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
148+
})
149+
150+
t.test('foreground-scripts can still be set to false', async t => {
151+
const { npm, outputs, logs } = await loadMockNpm(t, {
152+
prefixDir: {
153+
'package.json': JSON.stringify({
154+
name: 'test-fg-scripts',
155+
version: '0.0.0',
156+
scripts: {
157+
prepack: 'echo prepack!',
158+
postpack: 'echo postpack!',
159+
},
160+
}
161+
),
162+
},
163+
config: { 'dry-run': true, 'foreground-scripts': false },
164+
})
165+
166+
/* eslint no-console: 0 */
167+
// TODO: replace this with `const results = t.intercept(console, 'log')`
168+
const log = console.log
169+
t.teardown(() => {
170+
console.log = log
171+
})
172+
const caughtLogs = []
173+
console.log = (...args) => {
174+
caughtLogs.push(args)
175+
}
176+
// end TODO
177+
178+
await npm.exec('pack', [])
179+
const filename = 'test-fg-scripts-0.0.0.tgz'
180+
t.same(
181+
caughtLogs,
182+
[],
183+
'prepack and postpack do not log to stdout')
184+
t.strictSame(outputs, [[filename]])
185+
t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
186+
t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
187+
})
188+
108189
t.test('invalid packument', async t => {
109190
const { npm, outputs } = await loadMockNpm(t, {
110191
prefixDir: {

test/lib/commands/publish.js

+86
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,92 @@ t.test('dry-run', async t => {
167167
t.matchSnapshot(logs.notice)
168168
})
169169

170+
t.test('foreground-scripts defaults to true', async t => {
171+
const { joinedOutput, npm, logs } = await loadMockNpm(t, {
172+
config: {
173+
'dry-run': true,
174+
...auth,
175+
},
176+
prefixDir: {
177+
'package.json': JSON.stringify({
178+
name: 'test-fg-scripts',
179+
version: '0.0.0',
180+
scripts: {
181+
prepack: 'echo prepack!',
182+
postpack: 'echo postpack!',
183+
},
184+
}
185+
),
186+
},
187+
})
188+
189+
/* eslint no-console: 0 */
190+
// TODO: replace this with `const results = t.intercept(console, 'log')`
191+
const log = console.log
192+
t.teardown(() => {
193+
console.log = log
194+
})
195+
const caughtLogs = []
196+
console.log = (...args) => {
197+
caughtLogs.push(args)
198+
}
199+
// end TODO
200+
201+
await npm.exec('publish', [])
202+
t.equal(joinedOutput(), `+ test-fg-scripts@0.0.0`)
203+
t.matchSnapshot(logs.notice)
204+
205+
t.same(
206+
caughtLogs,
207+
[
208+
['\n> test-fg-scripts@0.0.0 prepack\n> echo prepack!\n'],
209+
['\n> test-fg-scripts@0.0.0 postpack\n> echo postpack!\n'],
210+
],
211+
'prepack and postpack log to stdout')
212+
})
213+
214+
t.test('foreground-scripts can still be set to false', async t => {
215+
const { joinedOutput, npm, logs } = await loadMockNpm(t, {
216+
config: {
217+
'dry-run': true,
218+
'foreground-scripts': false,
219+
...auth,
220+
},
221+
prefixDir: {
222+
'package.json': JSON.stringify({
223+
name: 'test-fg-scripts',
224+
version: '0.0.0',
225+
scripts: {
226+
prepack: 'echo prepack!',
227+
postpack: 'echo postpack!',
228+
},
229+
}
230+
),
231+
},
232+
})
233+
234+
/* eslint no-console: 0 */
235+
// TODO: replace this with `const results = t.intercept(console, 'log')`
236+
const log = console.log
237+
t.teardown(() => {
238+
console.log = log
239+
})
240+
const caughtLogs = []
241+
console.log = (...args) => {
242+
caughtLogs.push(args)
243+
}
244+
// end TODO
245+
246+
await npm.exec('publish', [])
247+
t.equal(joinedOutput(), `+ test-fg-scripts@0.0.0`)
248+
t.matchSnapshot(logs.notice)
249+
250+
t.same(
251+
caughtLogs,
252+
[],
253+
'prepack and postpack do not log to stdout')
254+
})
255+
170256
t.test('shows usage with wrong set of arguments', async t => {
171257
const { publish } = await loadMockNpm(t, { command: 'publish' })
172258
await t.rejects(publish.exec(['a', 'b', 'c']), publish.usage)

workspaces/config/lib/definitions/definitions.js

+2
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ define('force', {
786786

787787
define('foreground-scripts', {
788788
default: false,
789+
defaultDescription: `\`false\` unless when using \`npm pack\` or \`npm publish\` where it
790+
defaults to \`true\``,
789791
type: Boolean,
790792
description: `
791793
Run all build scripts (ie, \`preinstall\`, \`install\`, and

0 commit comments

Comments
 (0)