fix: Support workspaces in CLIs

Signed-off-by: Gabriel Dugny <gabriel.dugny@believe.com>
This commit is contained in:
Gabriel Dugny
2026-01-29 10:24:31 +01:00
parent ebd4630702
commit a9d23c4a32
13 changed files with 79 additions and 62 deletions
+3
View File
@@ -23,6 +23,9 @@ export class ExitCodeError extends CustomErrorBase {
// @public
export function findPaths(searchDir: string): Paths;
// @public
export function getWorkspacesPatterns(pkgJson: any): string[];
// @public
export function isChildPath(base: string, path: string): boolean;
+1 -1
View File
@@ -20,7 +20,7 @@
* @packageDocumentation
*/
export { findPaths, BACKSTAGE_JSON } from './paths';
export { findPaths, getWorkspacesPatterns, BACKSTAGE_JSON } from './paths';
export { isChildPath } from './isChildPath';
export type { Paths, ResolveFunc } from './paths';
export { bootstrapEnvProxyAgents } from './proxyBootstrap';
+20 -1
View File
@@ -107,6 +107,25 @@ export function findOwnRootDir(ownDir: string) {
return resolvePath(ownDir, '../..');
}
/**
* Gets the workspaces pattern from a package.json object.
* @param pkgJson - The package.json object to get the workspaces pattern from.
* @returns The workspaces patterns (glob not resolved).
* @public
*/
export function getWorkspacesPatterns(pkgJson: any): string[] {
if (Array.isArray(pkgJson.workspaces)) {
return pkgJson.workspaces;
} else if (
typeof pkgJson.workspaces === 'object' &&
pkgJson.workspaces !== null &&
Array.isArray(pkgJson.workspaces.packages)
) {
return pkgJson.workspaces.packages;
}
return [];
}
/**
* Find paths related to a package and its execution context.
*
@@ -141,7 +160,7 @@ export function findPaths(searchDir: string): Paths {
try {
const content = fs.readFileSync(path, 'utf8');
const data = JSON.parse(content);
return Boolean(data.workspaces);
return getWorkspacesPatterns(data).length > 0;
} catch (error) {
throw new Error(
`Failed to parse package.json file while searching for root, ${error}`,