Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve workspace paths from cwd when possible #4265

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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