Skip to content

Commit 48a349c

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

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-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: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const t = require('tap')
22
const mockNpm = require('../fixtures/mock-npm')
3+
const libpack = require('libnpmpack')
34
const pacote = require('pacote')
45

56
const OUTPUT = []
@@ -73,7 +74,7 @@ t.test('should pack given directory', (t) => {
7374
const npm = mockNpm({
7475
config: {
7576
unicode: true,
76-
json: true,
77+
json: false,
7778
'dry-run': true,
7879
},
7980
output,
@@ -108,7 +109,7 @@ t.test('should pack given directory for scoped package', (t) => {
108109
const npm = mockNpm({
109110
config: {
110111
unicode: true,
111-
json: true,
112+
json: false,
112113
'dry-run': true,
113114
},
114115
output,
@@ -158,6 +159,48 @@ t.test('should log pack contents', (t) => {
158159
})
159160
})
160161

162+
t.test('should log output as valid json', (t) => {
163+
const testDir = t.testdir({
164+
'package.json': JSON.stringify({
165+
name: 'my-cool-pkg',
166+
version: '1.0.0',
167+
main: './index.js',
168+
}, null, 2),
169+
'README.md': 'text',
170+
'index.js': 'void',
171+
})
172+
173+
const Pack = t.mock('../../lib/pack.js', {
174+
libnpmpack: () => libpack(testDir),
175+
npmlog: {
176+
notice: () => {},
177+
showProgress: () => {},
178+
clearProgress: () => {},
179+
},
180+
})
181+
const npm = mockNpm({
182+
config: {
183+
unicode: true,
184+
json: true,
185+
'dry-run': true,
186+
},
187+
output,
188+
})
189+
const pack = new Pack(npm)
190+
191+
pack.exec([testDir], err => {
192+
t.error(err, { bail: true })
193+
194+
t.similar(JSON.parse(OUTPUT), [{
195+
filename: 'my-cool-pkg-1.0.0.tgz',
196+
files: [{ path: 'README.md' }, { path: 'index.js' }, { path: 'package.json' }],
197+
entryCount: 3,
198+
}], 'pack details output as valid json')
199+
200+
t.end()
201+
})
202+
})
203+
161204
t.test('invalid packument', (t) => {
162205
const mockPacote = {
163206
manifest: () => {
@@ -176,7 +219,7 @@ t.test('invalid packument', (t) => {
176219
const npm = mockNpm({
177220
config: {
178221
unicode: true,
179-
json: true,
222+
json: false,
180223
'dry-run': true,
181224
},
182225
output,

0 commit comments

Comments
 (0)