Skip to content

Commit 05c66b4

Browse files
committed
feat(cli): return type of workspace along with monorepo root
1 parent 8971f4d commit 05c66b4

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

cli/src/util/monorepotools.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ import { join, dirname, relative } from 'node:path';
44
/**
55
* Finds the monorepo root from the given path.
66
* @param currentPath - The current path to start searching from.
7-
* @returns The path to the monorepo root.
7+
* @return an object describing the monorepo root and the type of file where the workspace config was detected.
88
* @throws An error if the monorepo root is not found.
99
*/
10-
export function findMonorepoRoot(currentPath: string): string {
10+
export function findMonorepoRoot(currentPath: string): {
11+
/**
12+
* The type of file where the workspace config was detected (ex. json for npm/yarn, yaml for pnpm).
13+
*/
14+
fileType: 'json' | 'yaml';
15+
path: string;
16+
} {
1117
const packageJsonPath = join(currentPath, 'package.json');
1218
const pnpmWorkspacePath = join(currentPath, 'pnpm-workspace.yaml');
13-
if (
14-
existsSync(pnpmWorkspacePath) ||
15-
(existsSync(packageJsonPath) && JSON.parse(readFileSync(packageJsonPath, 'utf-8')).workspaces)
16-
) {
17-
return currentPath;
19+
if (existsSync(pnpmWorkspacePath)) {
20+
return { fileType: 'yaml', path: currentPath };
21+
} else if (existsSync(packageJsonPath) && JSON.parse(readFileSync(packageJsonPath, 'utf-8')).workspaces) {
22+
return { fileType: 'json', path: currentPath };
1823
}
1924
const parentPath = dirname(currentPath);
2025
if (parentPath === currentPath) {
@@ -73,7 +78,7 @@ export function findPackagePath(
7378
* @returns The relative path to the package, or null if not found.
7479
*/
7580
export function findPackageRelativePathInMonorepo(packageName: string, currentPath: string): string | null {
76-
const monorepoRoot = findMonorepoRoot(currentPath);
81+
const { path: monorepoRoot } = findMonorepoRoot(currentPath);
7782
const packagePath = findPackagePath(packageName, currentPath, monorepoRoot);
7883
if (packagePath) {
7984
return relative(currentPath, packagePath);

0 commit comments

Comments
 (0)