workspace-root.mjs 673 Bytes
import fs from "node:fs";
import path from "node:path";
import process from "node:process";

export function findWorkspaceRoot(startPath = process.cwd()) {
	let current = path.resolve(startPath);

	while (true) {
		if (fs.existsSync(path.join(current, "pnpm-workspace.yaml"))) {
			return current;
		}

		const parent = path.dirname(current);
		if (parent === current) {
			return "";
		}

		current = parent;
	}
}

export function requireWorkspaceRoot(startPath = process.cwd()) {
	const workspaceRoot = findWorkspaceRoot(startPath);
	if (!workspaceRoot) {
		throw new Error(`Unable to locate workspace root from: ${path.resolve(startPath)}`);
	}
	return workspaceRoot;
}