Skip to content

Commit

Permalink
fix: revert '--json' argument to 'npm pack' command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed May 10, 2021
1 parent 049166b commit 48a349c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/content/commands/npm-pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Create a tarball from a package
### Synopsis

```bash
npm pack [[<@scope>/]<pkg>...] [--dry-run]
npm pack [[<@scope>/]<pkg>...] [--dry-run] [--json]
```

### Configuration
Expand Down
8 changes: 7 additions & 1 deletion lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Pack extends BaseCommand {

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

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

const unicode = this.npm.config.get('unicode')
const dryRun = this.npm.config.get('dry-run')
const json = this.npm.config.get('json')

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

if (json) {
this.npm.output(JSON.stringify(tarballs, null, 2))
return
}

for (const tar of tarballs) {
logTar(tar, { log, unicode })
this.npm.output(tar.filename.replace(/^@/, '').replace(/\//, '-'))
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/lib/utils/npm-usage.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ All commands:
npm pack [[<@scope>/]<pkg>...]
Options:
[--dry-run]
[--dry-run] [--json]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
Expand Down
49 changes: 46 additions & 3 deletions test/lib/pack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const t = require('tap')
const mockNpm = require('../fixtures/mock-npm')
const libpack = require('libnpmpack')
const pacote = require('pacote')

const OUTPUT = []
Expand Down Expand Up @@ -73,7 +74,7 @@ t.test('should pack given directory', (t) => {
const npm = mockNpm({
config: {
unicode: true,
json: true,
json: false,
'dry-run': true,
},
output,
Expand Down Expand Up @@ -108,7 +109,7 @@ t.test('should pack given directory for scoped package', (t) => {
const npm = mockNpm({
config: {
unicode: true,
json: true,
json: false,
'dry-run': true,
},
output,
Expand Down Expand Up @@ -158,6 +159,48 @@ t.test('should log pack contents', (t) => {
})
})

t.test('should log output as valid json', (t) => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
version: '1.0.0',
main: './index.js',
}, null, 2),
'README.md': 'text',
'index.js': 'void',
})

const Pack = t.mock('../../lib/pack.js', {
libnpmpack: () => libpack(testDir),
npmlog: {
notice: () => {},
showProgress: () => {},
clearProgress: () => {},
},
})
const npm = mockNpm({
config: {
unicode: true,
json: true,
'dry-run': true,
},
output,
})
const pack = new Pack(npm)

pack.exec([testDir], err => {
t.error(err, { bail: true })

t.similar(JSON.parse(OUTPUT), [{
filename: 'my-cool-pkg-1.0.0.tgz',
files: [{ path: 'README.md' }, { path: 'index.js' }, { path: 'package.json' }],
entryCount: 3,
}], 'pack details output as valid json')

t.end()
})
})

t.test('invalid packument', (t) => {
const mockPacote = {
manifest: () => {
Expand All @@ -176,7 +219,7 @@ t.test('invalid packument', (t) => {
const npm = mockNpm({
config: {
unicode: true,
json: true,
json: false,
'dry-run': true,
},
output,
Expand Down

0 comments on commit 48a349c

Please sign in to comment.