Skip to content

Commit 47eb0b4

Browse files
committed
fix(workspaces): explicitly error in global mode
Also includes a preliminary refactor to consolidate workspace logic now that every command that supports workspaces has it implemented.
1 parent c6a8734 commit 47eb0b4

30 files changed

+139
-101
lines changed

lib/audit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Audit extends ArboristWorkspaceCmd {
5858
audit: true,
5959
path: this.npm.prefix,
6060
reporter,
61-
workspaces: this.workspaces,
61+
workspaces: this.workspaceNames,
6262
}
6363

6464
const arb = new Arborist(opts)

lib/base-command.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Base class for npm.commands[cmd]
22
const usageUtil = require('./utils/usage.js')
33
const ConfigDefinitions = require('./utils/config/definitions.js')
4+
const getWorkspaces = require('./workspaces/get-workspaces.js')
45

56
class BaseCommand {
67
constructor (npm) {
@@ -72,5 +73,13 @@ class BaseCommand {
7273
{ code: 'ENOWORKSPACES' }
7374
)
7475
}
76+
77+
async setWorkspaces (filters) {
78+
// TODO npm guards workspaces/global mode so we should use this.npm.prefix?
79+
const ws = await getWorkspaces(filters, { path: this.npm.localPrefix })
80+
this.workspaces = ws
81+
this.workspaceNames = [...ws.keys()]
82+
this.workspacePaths = [...ws.values()]
83+
}
7584
}
7685
module.exports = BaseCommand

lib/ci.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CI extends ArboristWorkspaceCmd {
5555
path: where,
5656
log: this.npm.log,
5757
save: false, // npm ci should never modify the lockfile or package.json
58-
workspaces: this.workspaces,
58+
workspaces: this.workspaceNames,
5959
}
6060

6161
const arb = new Arborist(opts)

lib/dedupe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Dedupe extends ArboristWorkspaceCmd {
5050
log: this.npm.log,
5151
path: where,
5252
dryRun,
53-
workspaces: this.workspaces,
53+
workspaces: this.workspaceNames,
5454
}
5555
const arb = new Arborist(opts)
5656
await arb.dedupe(opts)

lib/diff.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const npmlog = require('npmlog')
88
const pacote = require('pacote')
99
const pickManifest = require('npm-pick-manifest')
1010

11-
const getWorkspaces = require('./workspaces/get-workspaces.js')
1211
const readPackageName = require('./utils/read-package-name.js')
1312
const BaseCommand = require('./base-command.js')
1413

@@ -90,9 +89,8 @@ class Diff extends BaseCommand {
9089
}
9190

9291
async diffWorkspaces (args, filters) {
93-
const workspaces =
94-
await getWorkspaces(filters, { path: this.npm.localPrefix })
95-
for (const workspacePath of workspaces.values()) {
92+
await this.setWorkspaces(filters)
93+
for (const workspacePath of this.workspacePaths) {
9694
this.top = workspacePath
9795
this.prefix = workspacePath
9896
await this.diff(args)
@@ -104,7 +102,6 @@ class Diff extends BaseCommand {
104102
async packageName (path) {
105103
let name
106104
try {
107-
// TODO this won't work as expected in global mode
108105
name = await readPackageName(this.prefix)
109106
} catch (e) {
110107
npmlog.verbose('diff', 'could not read project dir package.json')

lib/dist-tag.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const semver = require('semver')
55

66
const otplease = require('./utils/otplease.js')
77
const readPackageName = require('./utils/read-package-name.js')
8-
const getWorkspaces = require('./workspaces/get-workspaces.js')
98
const BaseCommand = require('./base-command.js')
109

1110
class DistTag extends BaseCommand {
@@ -180,10 +179,9 @@ class DistTag extends BaseCommand {
180179
}
181180

182181
async listWorkspaces (filters) {
183-
const workspaces =
184-
await getWorkspaces(filters, { path: this.npm.localPrefix })
182+
await this.setWorkspaces(filters)
185183

186-
for (const [name] of workspaces) {
184+
for (const name of this.workspaceNames) {
187185
try {
188186
this.npm.output(`${name}:`)
189187
await this.list(npa(name), this.npm.flatOptions)

lib/docs.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const log = require('npmlog')
22
const pacote = require('pacote')
33
const openUrl = require('./utils/open-url.js')
44
const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js')
5-
const getWorkspaces = require('./workspaces/get-workspaces.js')
65

76
const BaseCommand = require('./base-command.js')
87
class Docs extends BaseCommand {
@@ -42,9 +41,8 @@ class Docs extends BaseCommand {
4241
}
4342

4443
async docsWorkspaces (args, filters) {
45-
const workspaces =
46-
await getWorkspaces(filters, { path: this.npm.localPrefix })
47-
return this.docs([...workspaces.values()])
44+
await this.setWorkspaces(filters)
45+
return this.docs(this.workspacePaths)
4846
}
4947

5048
async getDocs (pkg) {

lib/exec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const libexec = require('libnpmexec')
22
const BaseCommand = require('./base-command.js')
33
const getLocationMsg = require('./exec/get-workspace-location-msg.js')
4-
const getWorkspaces = require('./workspaces/get-workspaces.js')
54

65
// it's like this:
76
//
@@ -105,16 +104,15 @@ class Exec extends BaseCommand {
105104
}
106105

107106
async _execWorkspaces (args, filters) {
108-
const workspaces =
109-
await getWorkspaces(filters, { path: this.npm.localPrefix })
107+
await this.setWorkspaces(filters)
110108
const color = this.npm.config.get('color')
111109

112-
for (const workspacePath of workspaces.values()) {
113-
const locationMsg = await getLocationMsg({ color, path: workspacePath })
110+
for (const path of this.workspacePaths) {
111+
const locationMsg = await getLocationMsg({ color, path })
114112
await this._exec(args, {
115113
locationMsg,
116-
path: workspacePath,
117-
runPath: workspacePath,
114+
path,
115+
runPath: path,
118116
})
119117
}
120118
}

lib/explain.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Explain extends ArboristWorkspaceCmd {
4646
const arb = new Arborist({ path: this.npm.prefix, ...this.npm.flatOptions })
4747
const tree = await arb.loadActual()
4848

49-
if (this.workspaces && this.workspaces.length)
50-
this.filterSet = arb.workspaceDependencySet(tree, this.workspaces)
49+
if (this.workspaceNames && this.workspaceNames.length)
50+
this.filterSet = arb.workspaceDependencySet(tree, this.workspaceNames)
5151

5252
const nodes = new Set()
5353
for (const arg of args) {

lib/fund.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Fund extends ArboristWorkspaceCmd {
9595
const fundingInfo = getFundingInfo(tree, {
9696
...this.flatOptions,
9797
log: this.npm.log,
98-
workspaces: this.workspaces,
98+
workspaces: this.workspaceNames,
9999
})
100100

101101
if (this.npm.config.get('json'))

0 commit comments

Comments
 (0)