Skip to content

Commit d18c29e

Browse files
committed
fix: revert '--json' argument to 'npm pack' command
1 parent 049166b commit d18c29e

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

docs/content/commands/npm-pack.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 1 deletion
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/utils/npm-usage.js.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ All commands:
637637
npm pack [[<@scope>/]<pkg>...]
638638
639639
Options:
640-
[--dry-run]
640+
[--dry-run] [--json]
641641
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
642642
[-ws|--workspaces]
643643

test/lib/pack.js

Lines changed: 52 additions & 3 deletions
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,55 @@ 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+
filename: 'my-cool-pkg-1.0.0.tgz',
177+
files: [{ path: 'README.md' }, { path: 'index.js' }, { path: 'package.json' }],
178+
entryCount: 3,
179+
}),
180+
},
181+
npmlog: {
182+
notice: () => {},
183+
showProgress: () => {},
184+
clearProgress: () => {},
185+
},
186+
})
187+
const npm = mockNpm({
188+
config: {
189+
unicode: true,
190+
json: true,
191+
'dry-run': true,
192+
},
193+
output,
194+
})
195+
const pack = new Pack(npm)
196+
197+
pack.exec([testDir], err => {
198+
t.error(err, { bail: true })
199+
200+
t.match(JSON.parse(OUTPUT), [{
201+
filename: 'my-cool-pkg-1.0.0.tgz',
202+
files: [{ path: 'README.md' }, { path: 'index.js' }, { path: 'package.json' }],
203+
entryCount: 3,
204+
}], 'pack details output as valid json')
205+
206+
t.end()
207+
})
208+
})
209+
161210
t.test('invalid packument', (t) => {
162211
const mockPacote = {
163212
manifest: () => {
@@ -176,7 +225,7 @@ t.test('invalid packument', (t) => {
176225
const npm = mockNpm({
177226
config: {
178227
unicode: true,
179-
json: true,
228+
json: false,
180229
'dry-run': true,
181230
},
182231
output,

0 commit comments

Comments
 (0)