This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
feat: automatically detect workspace roots #28
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ const walkUp = require('walk-up-path') | |
const ini = require('ini') | ||
const nopt = require('nopt') | ||
const mkdirp = require('mkdirp-infer-owner') | ||
const mapWorkspaces = require('@npmcli/map-workspaces') | ||
const rpj = require('read-package-json-fast') | ||
|
||
/* istanbul ignore next */ | ||
const myUid = process.getuid && process.getuid() | ||
|
@@ -543,23 +545,65 @@ class Config { | |
return | ||
} | ||
|
||
const cliWorkspaces = this[_get]('workspaces', 'cli') | ||
|
||
for (const p of walkUp(this.cwd)) { | ||
// walk up until we have a nm dir or a pj file | ||
const hasAny = (await Promise.all([ | ||
stat(resolve(p, 'node_modules')) | ||
.then(st => st.isDirectory()) | ||
.catch(() => false), | ||
stat(resolve(p, 'package.json')) | ||
.then(st => st.isFile()) | ||
.catch(() => false), | ||
])).some(is => is) | ||
if (hasAny) { | ||
const hasNodeModules = await stat(resolve(p, 'node_modules')) | ||
.then((st) => st.isDirectory()) | ||
.catch(() => false) | ||
|
||
const hasPackageJson = await stat(resolve(p, 'package.json')) | ||
.then((st) => st.isFile()) | ||
.catch(() => false) | ||
|
||
if (!this.localPrefix && (hasNodeModules || hasPackageJson)) { | ||
this.localPrefix = p | ||
return | ||
|
||
// if workspaces are disabled, return now | ||
if (cliWorkspaces === false) { | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this inside the for loop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh it's so that we set localPrefix to the very first walkUp we find no matter what, fencepost issue which I'll back away from slowly cause I'm very bad at them. |
||
} | ||
|
||
// otherwise, continue the loop | ||
continue | ||
} | ||
|
||
if (this.localPrefix && hasPackageJson) { | ||
// if we already set localPrefix but this dir has a package.json | ||
// then we need to see if `p` is a workspace root by reading its package.json | ||
// however, if reading it fails then we should just move on | ||
const pkg = await rpj(resolve(p, 'package.json')).catch(() => false) | ||
if (!pkg) { | ||
continue | ||
} | ||
|
||
const workspaces = await mapWorkspaces({ cwd: p, pkg }) | ||
for (const w of workspaces.values()) { | ||
if (w === this.localPrefix) { | ||
// see if there's a .npmrc file in the workspace, if so log a warning | ||
const hasNpmrc = await stat(resolve(this.localPrefix, '.npmrc')) | ||
.then((st) => st.isFile()) | ||
.catch(() => false) | ||
|
||
if (hasNpmrc) { | ||
this.log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`) | ||
ruyadorno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// set the workspace in the default layer, which allows it to be overridden easily | ||
const { data } = this.data.get('default') | ||
data.workspace = [this.localPrefix] | ||
this.localPrefix = p | ||
this.log.info(`found workspace root at ${this.localPrefix}`) | ||
ruyadorno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// we found a root, so we return now | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
this.localPrefix = this.cwd | ||
if (!this.localPrefix) { | ||
this.localPrefix = this.cwd | ||
} | ||
} | ||
|
||
loadUserConfig () { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about handling root links? tbh I'm not sure what the supported usages are but I'm aware that arborist does support some form of root link node:
ref: https://github.com/npm/cli/blob/latest/workspaces/arborist/lib/arborist/build-ideal-tree.js#L384
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we use
stat
here instead oflstat
we'll resolve symlinks during the lookups and require that thepackage.json
ornode_modules
correctly resolve to a file or directory, respectively. there's no change in behavior there so i think this should be just fine