fix: Support workspaces in CLIs
Signed-off-by: Gabriel Dugny <gabriel.dugny@believe.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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}`,
|
||||
|
||||
Reference in New Issue
Block a user