Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornlf <quitlahok@gmail.com>2022-01-20 23:37:57 +0300
committerGitHub <noreply@github.com>2022-01-20 23:37:57 +0300
commit14a3d95000f1cba937f3309d198a363ae65cf01f (patch)
treea1bd9458b339bd86e61e3253d2af2975954cee4b /lib
parentc99c2151a868672c017f64ff0ecb12149a2fb095 (diff)
fix: resolve workspace paths from cwd when possible (#4265)
Diffstat (limited to 'lib')
-rw-r--r--lib/base-command.js8
-rw-r--r--lib/workspaces/get-workspaces.js6
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/base-command.js b/lib/base-command.js
index ad261b514..f67f99f36 100644
--- a/lib/base-command.js
+++ b/lib/base-command.js
@@ -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')
@@ -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()]
diff --git a/lib/workspaces/get-workspaces.js b/lib/workspaces/get-workspaces.js
index a59b5a6c5..0cddae2a4 100644
--- a/lib/workspaces/get-workspaces.js
+++ b/lib/workspaces/get-workspaces.js
@@ -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 })
@@ -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)
}
}