Skip to content

Commit

Permalink
fix: resolve workspace paths from cwd when possible (#4265)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Jan 20, 2022
1 parent c99c215 commit 14a3d95
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/base-command.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Base class for npm commands

const { relative } = require('path')

const usageUtil = require('./utils/usage.js')
const ConfigDefinitions = require('./utils/config/definitions.js')
const getWorkspaces = require('./workspaces/get-workspaces.js')
Expand Down Expand Up @@ -78,9 +81,14 @@ class BaseCommand {
this.includeWorkspaceRoot = false
}

const relativeFrom = relative(this.npm.localPrefix, process.cwd()).startsWith('..')
? this.npm.localPrefix
: process.cwd()

const ws = await getWorkspaces(filters, {
path: this.npm.localPrefix,
includeWorkspaceRoot: this.includeWorkspaceRoot,
relativeFrom,
})
this.workspaces = ws
this.workspaceNames = [...ws.keys()]
Expand Down
6 changes: 3 additions & 3 deletions lib/workspaces/get-workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rpj = require('read-package-json-fast')

// Returns an Map of paths to workspaces indexed by workspace name
// { foo => '/path/to/foo' }
const getWorkspaces = async (filters, { path, includeWorkspaceRoot }) => {
const getWorkspaces = async (filters, { path, includeWorkspaceRoot, relativeFrom }) => {
// TODO we need a better error to be bubbled up here if this rpj call fails
const pkg = await rpj(resolve(path, 'package.json'))
const workspaces = await mapWorkspaces({ cwd: path, pkg })
Expand All @@ -21,8 +21,8 @@ const getWorkspaces = async (filters, { path, includeWorkspaceRoot }) => {
for (const filterArg of filters) {
for (const [workspaceName, workspacePath] of workspaces.entries()) {
if (filterArg === workspaceName
|| resolve(path, filterArg) === workspacePath
|| minimatch(workspacePath, `${resolve(path, filterArg)}/*`)) {
|| resolve(relativeFrom || path, filterArg) === workspacePath
|| minimatch(workspacePath, `${resolve(relativeFrom || path, filterArg)}/*`)) {
res.set(workspaceName, workspacePath)
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/lib/arborist-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ t.test('handle getWorkspaces raising an error', async t => {
})
class TestCmd extends ArboristCmd {}
const cmd = new TestCmd()
cmd.npm = {}
cmd.npm = { localPrefix: t.testdir() }

await t.rejects(
cmd.execWorkspaces(['foo'], ['a']),
Expand Down

0 comments on commit 14a3d95

Please sign in to comment.