Skip to content

Commit 1c6da12

Browse files
committed
feat(publish): add workspace support
Errors will make things stop altogether, dunno if we want to bikeshed that here or not
1 parent 659751f commit 1c6da12

File tree

3 files changed

+251
-28
lines changed

3 files changed

+251
-28
lines changed

lib/publish.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const npmFetch = require('npm-registry-fetch')
1111
const flatten = require('./utils/config/flatten.js')
1212
const otplease = require('./utils/otplease.js')
1313
const { getContents, logTar } = require('./utils/tar.js')
14+
const getWorkspaces = require('./workspaces/get-workspaces.js')
1415

1516
// this is the only case in the CLI where we use the old full slow
1617
// 'read-package-json' module, because we want to pull in all the
@@ -44,6 +45,10 @@ class Publish extends BaseCommand {
4445
this.publish(args).then(() => cb()).catch(cb)
4546
}
4647

48+
execWorkspaces (args, filters, cb) {
49+
this.publishWorkspaces(args, filters).then(() => cb()).catch(cb)
50+
}
51+
4752
async publish (args) {
4853
if (args.length === 0)
4954
args = ['.']
@@ -56,6 +61,7 @@ class Publish extends BaseCommand {
5661
const dryRun = this.npm.config.get('dry-run')
5762
const json = this.npm.config.get('json')
5863
const defaultTag = this.npm.config.get('tag')
64+
const silent = log.level === 'silent'
5965

6066
if (semver.validRange(defaultTag))
6167
throw new Error('Tag name must not be a valid SemVer range: ' + defaultTag.trim())
@@ -77,7 +83,7 @@ class Publish extends BaseCommand {
7783
path: spec.fetchSpec,
7884
stdio: 'inherit',
7985
pkg: manifest,
80-
banner: log.level !== 'silent',
86+
banner: !silent,
8187
})
8288
}
8389

@@ -114,27 +120,51 @@ class Publish extends BaseCommand {
114120
path: spec.fetchSpec,
115121
stdio: 'inherit',
116122
pkg: manifest,
117-
banner: log.level !== 'silent',
123+
banner: !silent,
118124
})
119125

120126
await runScript({
121127
event: 'postpublish',
122128
path: spec.fetchSpec,
123129
stdio: 'inherit',
124130
pkg: manifest,
125-
banner: log.level !== 'silent',
131+
banner: !silent,
126132
})
127133
}
128134

129-
const silent = log.level === 'silent'
130-
if (!silent && json)
131-
this.npm.output(JSON.stringify(pkgContents, null, 2))
132-
else if (!silent)
133-
this.npm.output(`+ ${pkgContents.id}`)
135+
if (!this.workspaces) {
136+
if (!silent && json)
137+
this.npm.output(JSON.stringify(pkgContents, null, 2))
138+
else if (!silent)
139+
this.npm.output(`+ ${pkgContents.id}`)
140+
}
134141

135142
return pkgContents
136143
}
137144

145+
async publishWorkspaces (args, filters) {
146+
// Suppresses JSON output in publish() so we can handle it here
147+
this.workspaces = true
148+
149+
const results = {}
150+
const json = this.npm.config.get('json')
151+
const silent = log.level === 'silent'
152+
const workspaces =
153+
await getWorkspaces(filters, { path: this.npm.localPrefix })
154+
for (const [name, workspace] of workspaces.entries()) {
155+
const pkgContents = await this.publish([workspace])
156+
// This needs to be in-line w/ the rest of the output that non-JSON
157+
// publish generates
158+
if (!silent && !json)
159+
this.npm.output(`+ ${pkgContents.id}`)
160+
else
161+
results[name] = pkgContents
162+
}
163+
164+
if (!silent && json)
165+
this.npm.output(JSON.stringify(results, null, 2))
166+
}
167+
138168
// if it's a directory, read it from the file system
139169
// otherwise, get the full metadata from whatever it is
140170
getManifest (spec, opts) {

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,104 @@ Options:
1818
1919
Run "npm help publish" for more info
2020
`
21+
22+
exports[`test/lib/publish.js TAP workspaces all workspaces > should output all publishes 1`] = `
23+
Array [
24+
"+ workspace-a@1.2.3-a",
25+
"+ workspace-b@1.2.3-n",
26+
]
27+
`
28+
29+
exports[`test/lib/publish.js TAP workspaces all workspaces > should publish all workspaces 1`] = `
30+
Array [
31+
Object {
32+
"_id": "workspace-a@1.2.3-a",
33+
"name": "workspace-a",
34+
"readme": "ERROR: No README data found!",
35+
"repository": Object {
36+
"type": "git",
37+
"url": "http://repo.workspace-a/",
38+
},
39+
"version": "1.2.3-a",
40+
},
41+
Object {
42+
"_id": "workspace-b@1.2.3-n",
43+
"bugs": Object {
44+
"url": "https://github.com/npm/workspace-b/issues",
45+
},
46+
"homepage": "https://github.com/npm/workspace-b#readme",
47+
"name": "workspace-b",
48+
"readme": "ERROR: No README data found!",
49+
"repository": Object {
50+
"type": "git",
51+
"url": "git+https://github.com/npm/workspace-b.git",
52+
},
53+
"version": "1.2.3-n",
54+
},
55+
]
56+
`
57+
58+
exports[`test/lib/publish.js TAP workspaces json > should output all publishes as json 1`] = `
59+
Array [
60+
String(
61+
{
62+
"workspace-a": {
63+
"id": "workspace-a@1.2.3-a"
64+
},
65+
"workspace-b": {
66+
"id": "workspace-b@1.2.3-n"
67+
}
68+
}
69+
),
70+
]
71+
`
72+
73+
exports[`test/lib/publish.js TAP workspaces json > should publish all workspaces 1`] = `
74+
Array [
75+
Object {
76+
"_id": "workspace-a@1.2.3-a",
77+
"name": "workspace-a",
78+
"readme": "ERROR: No README data found!",
79+
"repository": Object {
80+
"type": "git",
81+
"url": "http://repo.workspace-a/",
82+
},
83+
"version": "1.2.3-a",
84+
},
85+
Object {
86+
"_id": "workspace-b@1.2.3-n",
87+
"bugs": Object {
88+
"url": "https://github.com/npm/workspace-b/issues",
89+
},
90+
"homepage": "https://github.com/npm/workspace-b#readme",
91+
"name": "workspace-b",
92+
"readme": "ERROR: No README data found!",
93+
"repository": Object {
94+
"type": "git",
95+
"url": "git+https://github.com/npm/workspace-b.git",
96+
},
97+
"version": "1.2.3-n",
98+
},
99+
]
100+
`
101+
102+
exports[`test/lib/publish.js TAP workspaces one workspace > should output one publish 1`] = `
103+
Array [
104+
"+ workspace-a@1.2.3-a",
105+
]
106+
`
107+
108+
exports[`test/lib/publish.js TAP workspaces one workspace > should publish given workspace 1`] = `
109+
Array [
110+
Object {
111+
"_id": "workspace-a@1.2.3-a",
112+
"name": "workspace-a",
113+
"readme": "ERROR: No README data found!",
114+
"repository": Object {
115+
"type": "git",
116+
"url": "http://repo.workspace-a/",
117+
},
118+
"version": "1.2.3-a",
119+
},
120+
]
121+
`

0 commit comments

Comments
 (0)