Skip to content

Commit 63a7635

Browse files
mrmlncisaacs
authored andcommitted
fix: restore '--json' argument to 'npm pack' command
PR-URL: #3217 Credit: @mrmlnc Close: #3217 Reviewed-by: @isaacs, @wraithgar, @ruyadorno
1 parent 83df366 commit 63a7635

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

docs/content/commands/npm-pack.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Create a tarball from a package
77
### Synopsis
88

99
```bash
10-
npm pack [[<@scope>/]<pkg>...] [--dry-run]
10+
npm pack [[<@scope>/]<pkg>...] [--dry-run] [--json]
1111
```
1212

1313
### Configuration

lib/pack.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Pack extends BaseCommand {
2424

2525
/* istanbul ignore next - see test/lib/load-all-commands.js */
2626
static get params () {
27-
return ['dry-run', 'workspace', 'workspaces']
27+
return ['dry-run', 'json', 'workspace', 'workspaces']
2828
}
2929

3030
/* istanbul ignore next - see test/lib/load-all-commands.js */
@@ -46,6 +46,7 @@ class Pack extends BaseCommand {
4646

4747
const unicode = this.npm.config.get('unicode')
4848
const dryRun = this.npm.config.get('dry-run')
49+
const json = this.npm.config.get('json')
4950

5051
// Get the manifests and filenames first so we can bail early on manifest
5152
// errors before making any tarballs
@@ -74,6 +75,11 @@ class Pack extends BaseCommand {
7475
tarballs.push(pkgContents)
7576
}
7677

78+
if (json) {
79+
this.npm.output(JSON.stringify(tarballs, null, 2))
80+
return
81+
}
82+
7783
for (const tar of tarballs) {
7884
logTar(tar, { log, unicode })
7985
this.npm.output(tar.filename.replace(/^@/, '').replace(/\//, '-'))

tap-snapshots/test/lib/load-all-commands.js.test.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ Usage:
653653
npm pack [[<@scope>/]<pkg>...]
654654
655655
Options:
656-
[--dry-run]
656+
[--dry-run] [--json]
657657
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
658658
[-ws|--workspaces]
659659

tap-snapshots/test/lib/utils/npm-usage.js.test.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ All commands:
740740
npm pack [[<@scope>/]<pkg>...]
741741
742742
Options:
743-
[--dry-run]
743+
[--dry-run] [--json]
744744
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
745745
[-ws|--workspaces]
746746

test/lib/pack.js

+90-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ t.test('should pack given directory', (t) => {
7373
const npm = mockNpm({
7474
config: {
7575
unicode: true,
76-
json: true,
76+
json: false,
7777
'dry-run': true,
7878
},
7979
output,
@@ -108,7 +108,7 @@ t.test('should pack given directory for scoped package', (t) => {
108108
const npm = mockNpm({
109109
config: {
110110
unicode: true,
111-
json: true,
111+
json: false,
112112
'dry-run': true,
113113
},
114114
output,
@@ -158,6 +158,93 @@ t.test('should log pack contents', (t) => {
158158
})
159159
})
160160

161+
t.test('should log output as valid json', (t) => {
162+
const testDir = t.testdir({
163+
'package.json': JSON.stringify({
164+
name: 'my-cool-pkg',
165+
version: '1.0.0',
166+
main: './index.js',
167+
}, null, 2),
168+
'README.md': 'text',
169+
'index.js': 'void',
170+
})
171+
172+
const Pack = t.mock('../../lib/pack.js', {
173+
libnpmpack,
174+
'../../lib/utils/tar.js': {
175+
getContents: async () => ({
176+
id: '@ruyadorno/[email protected]',
177+
name: '@ruyadorno/redact',
178+
version: '1.0.0',
179+
size: 2450,
180+
unpackedSize: 4911,
181+
shasum: '044c7574639b923076069d6e801e2d1866430f17',
182+
// mocks exactly how ssri Integrity works:
183+
integrity: {
184+
sha512: [
185+
{
186+
source: 'sha512-JSdyskeR2qonBUaQ4vdlU/vQGSfgCxSq5O+vH+d2yVWRqzso4O3gUzd6QX/V7OWV//zU7kA5o63Zf433jUnOtQ==',
187+
digest: 'JSdyskeR2qonBUaQ4vdlU/vQGSfgCxSq5O+vH+d2yVWRqzso4O3gUzd6QX/V7OWV//zU7kA5o63Zf433jUnOtQ==',
188+
algorithm: 'sha512',
189+
options: [],
190+
},
191+
],
192+
toJSON () {
193+
return 'sha512-JSdyskeR2qonBUaQ4vdlU/vQGSfgCxSq5O+vH+d2yVWRqzso4O3gUzd6QX/V7OWV//zU7kA5o63Zf433jUnOtQ=='
194+
},
195+
},
196+
filename: '@ruyadorno/redact-1.0.0.tgz',
197+
files: [
198+
{ path: 'LICENSE', size: 1113, mode: 420 },
199+
{ path: 'README.md', size: 2639, mode: 420 },
200+
{ path: 'index.js', size: 719, mode: 493 },
201+
{ path: 'package.json', size: 440, mode: 420 },
202+
],
203+
entryCount: 4,
204+
bundled: [],
205+
}),
206+
},
207+
npmlog: {
208+
notice: () => {},
209+
showProgress: () => {},
210+
clearProgress: () => {},
211+
},
212+
})
213+
const npm = mockNpm({
214+
config: {
215+
unicode: true,
216+
json: true,
217+
'dry-run': true,
218+
},
219+
output,
220+
})
221+
const pack = new Pack(npm)
222+
223+
pack.exec([testDir], err => {
224+
t.error(err, { bail: true })
225+
226+
t.match(JSON.parse(OUTPUT), [{
227+
id: '@ruyadorno/[email protected]',
228+
name: '@ruyadorno/redact',
229+
version: '1.0.0',
230+
size: 2450,
231+
unpackedSize: 4911,
232+
shasum: '044c7574639b923076069d6e801e2d1866430f17',
233+
integrity: 'sha512-JSdyskeR2qonBUaQ4vdlU/vQGSfgCxSq5O+vH+d2yVWRqzso4O3gUzd6QX/V7OWV//zU7kA5o63Zf433jUnOtQ==',
234+
filename: '@ruyadorno/redact-1.0.0.tgz',
235+
files: [
236+
{ path: 'LICENSE' },
237+
{ path: 'README.md' },
238+
{ path: 'index.js' },
239+
{ path: 'package.json' },
240+
],
241+
entryCount: 4,
242+
}], 'pack details output as valid json')
243+
244+
t.end()
245+
})
246+
})
247+
161248
t.test('invalid packument', (t) => {
162249
const mockPacote = {
163250
manifest: () => {
@@ -176,7 +263,7 @@ t.test('invalid packument', (t) => {
176263
const npm = mockNpm({
177264
config: {
178265
unicode: true,
179-
json: true,
266+
json: false,
180267
'dry-run': true,
181268
},
182269
output,

0 commit comments

Comments
 (0)