Skip to content

Commit e41071f

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

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
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(/\//, '-'))

test/lib/pack.js

+43-3
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 pkgDirPack = 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,45 @@ 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+
}, null, 2),
168+
})
169+
170+
const Pack = t.mock('../../lib/pack.js', {
171+
libnpmpack: () => pkgDirPack(testDir),
172+
npmlog: {
173+
notice: () => {},
174+
showProgress: () => {},
175+
clearProgress: () => {},
176+
},
177+
})
178+
const npm = mockNpm({
179+
config: {
180+
unicode: true,
181+
json: true,
182+
'dry-run': true,
183+
},
184+
output,
185+
})
186+
const pack = new Pack(npm)
187+
188+
pack.exec([testDir], err => {
189+
t.error(err, { bail: true })
190+
191+
t.similar(JSON.parse(OUTPUT), [{
192+
filename: 'my-cool-pkg-1.0.0.tgz',
193+
files: [{path: 'package.json'}],
194+
entryCount: 1,
195+
}], 'pack details output as valid json')
196+
197+
t.end()
198+
})
199+
})
200+
161201
t.test('invalid packument', (t) => {
162202
const mockPacote = {
163203
manifest: () => {
@@ -176,7 +216,7 @@ t.test('invalid packument', (t) => {
176216
const npm = mockNpm({
177217
config: {
178218
unicode: true,
179-
json: true,
219+
json: false,
180220
'dry-run': true,
181221
},
182222
output,

0 commit comments

Comments
 (0)