Skip to content

Commit 2e9b26e

Browse files
authored
fix: sync json output of pack and publish (npm#9247)
BREAKING CHANGE: the --json output of `npm pack` and `npm publish` have changed. They are now always consistent, and in the same format. Previously, `npm pack` would output an array of entries and `npm publish` an object. The `npm publish` object also changed forms depending on if workspaces were being published. Now, the output is always an object with the package name as the top level index. fixes npm/statusboard#1073
1 parent 738be10 commit 2e9b26e

6 files changed

Lines changed: 31 additions & 29 deletions

File tree

lib/commands/pack.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ class Pack extends BaseCommand {
6262
tarballs.push(await getContents(manifest, tarballData))
6363
}
6464

65-
for (const [index, tar] of Object.entries(tarballs)) {
66-
// XXX(BREAKING_CHANGE): publish outputs a json object with package names as keys.
67-
// Pack should do the same here instead of an array
68-
logTar(tar, { unicode, json, key: index })
65+
for (const tar of tarballs) {
66+
logTar(tar, { unicode, json, key: tar.name })
6967
if (!json) {
7068
output.standard(tar.filename.replace(/^@/, '').replace(/\//, '-'))
7169
}

lib/commands/publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Publish extends BaseCommand {
106106
workspaces: this.workspacePaths,
107107
})
108108
const pkgContents = await getContents(manifest, tarballData)
109-
const logPkg = () => logTar(pkgContents, { unicode, json, key: workspace })
109+
const logPkg = () => logTar(pkgContents, { unicode, json, key: pkgContents.name })
110110

111111
// The purpose of re-reading the manifest is in case it changed, so that we send the latest and greatest thing to the registry note that publishConfig might have changed as well!
112112
manifest = await this.#getManifest(spec, opts, true)

lib/utils/display.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const getArrayOrObject = (items) => {
9090
return foundNonObject
9191
}
9292
// We use objects with 0,1,2,etc keys to merge array
93+
// We don't currently use this but want to allow for it again
94+
// istanbul ignore next
9395
if (items.every((o, i) => Object.hasOwn(o, i))) {
9496
return Object.assign([], ...items)
9597
}

lib/utils/tar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const localeCompare = require('@isaacs/string-locale-compare')('en', {
99

1010
const logTar = (tarball, { unicode = false, json, key } = {}) => {
1111
if (json) {
12-
output.buffer(key == null ? tarball : { [key]: tarball })
12+
output.buffer({ [key]: tarball })
1313
return
1414
}
1515
log.notice('')

tap-snapshots/test/lib/commands/pack.js.test.cjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ Array []
6262

6363
exports[`test/lib/commands/pack.js TAP should log output as valid json > outputs as json 1`] = `
6464
Array [
65-
Array [
66-
Object {
65+
Object {
66+
"test-package": Object {
6767
"bundled": Array [],
6868
"entryCount": 1,
6969
"filename": "test-package-1.0.0.tgz",
@@ -82,7 +82,7 @@ Array [
8282
"unpackedSize": 41,
8383
"version": "1.0.0",
8484
},
85-
],
85+
},
8686
]
8787
`
8888

@@ -92,8 +92,8 @@ Array []
9292

9393
exports[`test/lib/commands/pack.js TAP should log scoped package output as valid json > outputs as json 1`] = `
9494
Array [
95-
Array [
96-
Object {
95+
Object {
96+
"@myscope/test-package": Object {
9797
"bundled": Array [],
9898
"entryCount": 1,
9999
"filename": "myscope-test-package-1.0.0.tgz",
@@ -112,7 +112,7 @@ Array [
112112
"unpackedSize": 88,
113113
"version": "1.0.0",
114114
},
115-
],
115+
},
116116
]
117117
`
118118

tap-snapshots/test/lib/commands/publish.js.test.cjs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,25 @@ Array [
8787

8888
exports[`test/lib/commands/publish.js TAP json > new package json 1`] = `
8989
{
90-
"id": "@npmcli/test-package@1.0.0",
91-
"name": "@npmcli/test-package",
92-
"version": "1.0.0",
93-
"size": "{size}",
94-
"unpackedSize": 95,
95-
"shasum": "{sha}",
96-
"integrity": "{integrity}",
97-
"filename": "npmcli-test-package-1.0.0.tgz",
98-
"files": [
99-
{
100-
"path": "package.json",
101-
"size": "{size}",
102-
"mode": 420
103-
}
104-
],
105-
"entryCount": 1,
106-
"bundled": []
90+
"@npmcli/test-package": {
91+
"id": "@npmcli/test-package@1.0.0",
92+
"name": "@npmcli/test-package",
93+
"version": "1.0.0",
94+
"size": "{size}",
95+
"unpackedSize": 95,
96+
"shasum": "{sha}",
97+
"integrity": "{integrity}",
98+
"filename": "npmcli-test-package-1.0.0.tgz",
99+
"files": [
100+
{
101+
"path": "package.json",
102+
"size": "{size}",
103+
"mode": 420
104+
}
105+
],
106+
"entryCount": 1,
107+
"bundled": []
108+
}
107109
}
108110
`
109111

0 commit comments

Comments
 (0)