Skip to content

Commit 7fdd1e5

Browse files
committed
fix(refactor): use output.buffer for easier json output in publish
1 parent 1bdb860 commit 7fdd1e5

File tree

2 files changed

+31
-55
lines changed

2 files changed

+31
-55
lines changed

lib/commands/pack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Pack extends BaseCommand {
5757
tarballs.push(pkgContents)
5858
}
5959

60+
// XXX(BREAKING_CHANGE): publish outputs a json object with package
61+
// names as keys. Pack should do the same here instead of an array
6062
if (json) {
6163
output.standard(JSON.stringify(tarballs, null, 2))
6264
return

lib/commands/publish.js

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,34 @@ class Publish extends BaseCommand {
4343
throw this.usageError()
4444
}
4545

46+
await this.#publish(args)
47+
}
48+
49+
async execWorkspaces () {
50+
await this.setWorkspaces()
51+
52+
for (const [name, workspace] of this.workspaces.entries()) {
53+
try {
54+
await this.#publish([workspace], { workspace: name })
55+
} catch (err) {
56+
if (err.code !== 'EPRIVATE') {
57+
throw err
58+
}
59+
// eslint-disable-next-line max-len
60+
log.warn('publish', `Skipping workspace ${this.npm.chalk.cyan(name)}, marked as ${this.npm.chalk.bold('private')}`)
61+
}
62+
}
63+
}
64+
65+
async #publish (args, { workspace } = {}) {
4666
log.verbose('publish', replaceInfo(args))
4767

68+
const { silent } = this.npm
4869
const unicode = this.npm.config.get('unicode')
4970
const dryRun = this.npm.config.get('dry-run')
5071
const json = this.npm.config.get('json')
5172
const defaultTag = this.npm.config.get('tag')
5273
const ignoreScripts = this.npm.config.get('ignore-scripts')
53-
const { silent } = this.npm
5474

5575
if (semver.validRange(defaultTag)) {
5676
throw new Error('Tag name must not be a valid SemVer range: ' + defaultTag.trim())
@@ -61,7 +81,7 @@ class Publish extends BaseCommand {
6181
// you can publish name@version, ./foo.tgz, etc.
6282
// even though the default is the 'file:.' cwd.
6383
const spec = npa(args[0])
64-
let manifest = await this.getManifest(spec, opts)
84+
let manifest = await this.#getManifest(spec, opts)
6585

6686
// only run scripts for directory type publishes
6787
if (spec.type === 'directory' && !ignoreScripts) {
@@ -88,10 +108,11 @@ class Publish extends BaseCommand {
88108
// The purpose of re-reading the manifest is in case it changed,
89109
// so that we send the latest and greatest thing to the registry
90110
// note that publishConfig might have changed as well!
91-
manifest = await this.getManifest(spec, opts, true)
111+
manifest = await this.#getManifest(spec, opts, true)
92112

93-
// JSON already has the package contents
94-
if (!json) {
113+
if (json) {
114+
output.buffer(workspace ? { [workspace]: pkgContents } : pkgContents)
115+
} else {
95116
logTar(pkgContents, { unicode })
96117
}
97118

@@ -142,62 +163,15 @@ class Publish extends BaseCommand {
142163
})
143164
}
144165

145-
if (!this.suppressOutput) {
146-
if (!silent && json) {
147-
output.standard(JSON.stringify(pkgContents, null, 2))
148-
} else if (!silent) {
149-
output.standard(`+ ${pkgContents.id}`)
150-
}
151-
}
152-
153-
return pkgContents
154-
}
155-
156-
async execWorkspaces () {
157-
// Suppresses JSON output in publish() so we can handle it here
158-
this.suppressOutput = true
159-
160-
const results = {}
161-
const json = this.npm.config.get('json')
162-
const { silent } = this.npm
163-
await this.setWorkspaces()
164-
165-
for (const [name, workspace] of this.workspaces.entries()) {
166-
let pkgContents
167-
try {
168-
pkgContents = await this.exec([workspace])
169-
} catch (err) {
170-
if (err.code === 'EPRIVATE') {
171-
log.warn(
172-
'publish',
173-
`Skipping workspace ${
174-
this.npm.chalk.cyan(name)
175-
}, marked as ${
176-
this.npm.chalk.bold('private')
177-
}`
178-
)
179-
continue
180-
}
181-
throw err
182-
}
183-
// This needs to be in-line w/ the rest of the output that non-JSON
184-
// publish generates
185-
if (!silent && !json) {
186-
output.standard(`+ ${pkgContents.id}`)
187-
} else {
188-
results[name] = pkgContents
189-
}
190-
}
191-
192-
if (!silent && json) {
193-
output.standard(JSON.stringify(results, null, 2))
166+
if (!json && !silent) {
167+
output.standard(`+ ${pkgContents.id}`)
194168
}
195169
}
196170

197171
// if it's a directory, read it from the file system
198172
// otherwise, get the full metadata from whatever it is
199173
// XXX can't pacote read the manifest from a directory?
200-
async getManifest (spec, opts, logWarnings = false) {
174+
async #getManifest (spec, opts, logWarnings = false) {
201175
let manifest
202176
if (spec.type === 'directory') {
203177
const changes = []

0 commit comments

Comments
 (0)