Skip to content

Commit 86044e8

Browse files
committed
Enhance monorepo root detection
1 parent f5cbb18 commit 86044e8

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/index.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ const builtins = {
9595
const workspaceRootFiles = new Set([
9696
'pnpm-workspace.yaml', // pnpm
9797
'lerna.json', // Lerna
98-
'workspace.jsonc', // Bit
99-
'nx.json', // Nx
100-
'rush.json', // Rush
98+
// Note: is there any interest in the following?
99+
// 'workspace.jsonc', // Bit
100+
// 'nx.json', // Nx
101+
// 'rush.json', // Rush
101102
])
102103

103104
// Our defaults
@@ -126,16 +127,16 @@ const isString = (str: unknown): str is string =>
126127
*/
127128
function externals(options: ExternalsOptions = {}): Plugin {
128129

129-
// This will store all eventual warnings/errors until we can display them.
130+
// This will store all eventual errors/warnings until we can display them.
130131
const messages: string[] = []
131132

132133
// Consolidate options
133134
const config: Config = Object.assign(Object.create(null), defaults, options)
134135

135136
// Map the include and exclude options to arrays of regexes.
136-
const [ include, exclude ] = [ 'include', 'exclude' ].map(option =>
137+
const [ include, exclude ] = ([ 'include', 'exclude' ] as const).map(option =>
137138
([] as (string | RegExp)[])
138-
.concat(config[ option as 'include' | 'exclude' ])
139+
.concat(config[option])
139140
.reduce((result, entry, index) => {
140141
if (entry instanceof RegExp)
141142
result.push(entry)
@@ -148,7 +149,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
148149
}, [] as RegExp[])
149150
)
150151

151-
// Prepare npm dependencies lists.
152+
// Prepare npm dependencies list.
152153
if (config.deps || config.devDeps || config.peerDeps || config.optDeps) {
153154

154155
const packagePaths: string[] = Array.isArray(config.packagePath)
@@ -158,8 +159,8 @@ function externals(options: ExternalsOptions = {}): Plugin {
158159
: []
159160

160161
if (packagePaths.length === 0) {
161-
// Get all package.json files from cwd up to the root of the git repo,
162-
// the root of the monorepo, or the root of the volume, whichever comes first.
162+
// Get all package.json files from cwd up to the root of the monorepo,
163+
// the root of the git epo, or the root of the volume, whichever comes first.
163164
for (
164165
let current = process.cwd(), previous: string | null = null;
165166
previous !== current;
@@ -170,6 +171,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
170171
if (entries.some(entry => entry.name === 'package.json' && entry.isFile()))
171172
packagePaths.push(path.join(current, 'package.json'))
172173

174+
// Break early if there is a pnpm/lerna workspace config file.
173175
if (entries.some(entry =>
174176
(workspaceRootFiles.has(entry.name) && entry.isFile()) ||
175177
(entry.name === '.git' && entry.isDirectory())
@@ -192,16 +194,18 @@ function externals(options: ExternalsOptions = {}): Plugin {
192194
config.optDeps && pkg.optionalDependencies
193195
)
194196

195-
// Stop here if this is a npm/yarn workspace root
196-
if (pkg.workspaces || pkg.packages)
197+
// Break early if this is a npm/yarn workspace root
198+
if ('workspaces' in pkg)
197199
break
198200
}
199201
catch {
202+
messages.push(pkg
203+
? `File ${JSON.stringify(packagePath)} does not look like a valid package.json file.`
204+
: `Cannot read file ${JSON.stringify(packagePath)}`
205+
)
206+
200207
config.invalid = true
201-
if (pkg)
202-
messages.push(`File ${JSON.stringify(packagePath)} does not look like a valid package.json.`)
203-
else if (config.packagePath.length) // string or array
204-
messages.push(`Cannot read file ${JSON.stringify(packagePath)}`)
208+
break
205209
}
206210
}
207211

0 commit comments

Comments
 (0)