Skip to content

Commit

Permalink
feat(cli): return type of workspace along with monorepo root
Browse files Browse the repository at this point in the history
  • Loading branch information
laine-hallot committed Oct 29, 2024
1 parent 8971f4d commit 05c66b4
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions cli/src/util/monorepotools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import { join, dirname, relative } from 'node:path';
/**
* Finds the monorepo root from the given path.
* @param currentPath - The current path to start searching from.
* @returns The path to the monorepo root.
* @return an object describing the monorepo root and the type of file where the workspace config was detected.
* @throws An error if the monorepo root is not found.
*/
export function findMonorepoRoot(currentPath: string): string {
export function findMonorepoRoot(currentPath: string): {
/**
* The type of file where the workspace config was detected (ex. json for npm/yarn, yaml for pnpm).
*/
fileType: 'json' | 'yaml';
path: string;
} {
const packageJsonPath = join(currentPath, 'package.json');
const pnpmWorkspacePath = join(currentPath, 'pnpm-workspace.yaml');
if (
existsSync(pnpmWorkspacePath) ||
(existsSync(packageJsonPath) && JSON.parse(readFileSync(packageJsonPath, 'utf-8')).workspaces)
) {
return currentPath;
if (existsSync(pnpmWorkspacePath)) {
return { fileType: 'yaml', path: currentPath };
} else if (existsSync(packageJsonPath) && JSON.parse(readFileSync(packageJsonPath, 'utf-8')).workspaces) {
return { fileType: 'json', path: currentPath };
}
const parentPath = dirname(currentPath);
if (parentPath === currentPath) {
Expand Down Expand Up @@ -73,7 +78,7 @@ export function findPackagePath(
* @returns The relative path to the package, or null if not found.
*/
export function findPackageRelativePathInMonorepo(packageName: string, currentPath: string): string | null {
const monorepoRoot = findMonorepoRoot(currentPath);
const { path: monorepoRoot } = findMonorepoRoot(currentPath);
const packagePath = findPackagePath(packageName, currentPath, monorepoRoot);
if (packagePath) {
return relative(currentPath, packagePath);
Expand Down

0 comments on commit 05c66b4

Please sign in to comment.