UNPKG

@visulima/package

Version:

One Package to rule them all, finds your root-dir, monorepo, or package manager.

99 lines (96 loc) 3.51 kB
import { existsSync, readFileSync } from 'node:fs'; import { findUpSync, readJsonSync, findUp, readJson } from '@visulima/fs'; import { NotFoundError } from '@visulima/fs/error'; import { dirname, join } from '@visulima/path'; import { findPackageManagerSync, findPackageManager } from './package-manager.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const findMonorepoRoot = /* @__PURE__ */ __name(async (cwd) => { const workspaceFilePath = await findUp(["lerna.json", "turbo.json"], { type: "file", ...cwd && { cwd } }); if (workspaceFilePath?.endsWith("lerna.json")) { const lerna = await readJson(workspaceFilePath); if (lerna.useWorkspaces || lerna.packages) { return { path: dirname(workspaceFilePath), strategy: "lerna" }; } } const isTurbo = workspaceFilePath?.endsWith("turbo.json"); try { const { packageManager, path } = await findPackageManager(cwd); if (["npm", "yarn"].includes(packageManager)) { const packageJsonFilePath = join(path, "package.json"); if (existsSync(packageJsonFilePath)) { const packageJson = readFileSync(join(path, "package.json"), "utf8"); if (packageJson.includes("workspaces")) { return { path, strategy: isTurbo ? "turbo" : packageManager }; } } } else if (packageManager === "pnpm") { const pnpmWorkspacesFilePath = join(path, "pnpm-workspace.yaml"); if (existsSync(pnpmWorkspacesFilePath)) { return { path, strategy: isTurbo ? "turbo" : "pnpm" }; } } } catch (error) { if (!(error instanceof NotFoundError)) { throw error; } } throw new Error(`No monorepo root could be found upwards from the directory ${cwd} using lerna, yarn, pnpm, or npm as indicators.`); }, "findMonorepoRoot"); const findMonorepoRootSync = /* @__PURE__ */ __name((cwd) => { const workspaceFilePath = findUpSync(["lerna.json", "turbo.json"], { type: "file", ...cwd && { cwd } }); if (workspaceFilePath?.endsWith("lerna.json")) { const lerna = readJsonSync(workspaceFilePath); if (lerna.useWorkspaces || lerna.packages) { return { path: dirname(workspaceFilePath), strategy: "lerna" }; } } const isTurbo = workspaceFilePath?.endsWith("turbo.json"); try { const { packageManager, path } = findPackageManagerSync(cwd); if (["npm", "yarn"].includes(packageManager)) { const packageJsonFilePath = join(path, "package.json"); if (existsSync(packageJsonFilePath)) { const packageJson = readFileSync(join(path, "package.json"), "utf8"); if (packageJson.includes("workspaces")) { return { path, strategy: isTurbo ? "turbo" : packageManager }; } } } else if (packageManager === "pnpm") { const pnpmWorkspacesFilePath = join(path, "pnpm-workspace.yaml"); if (existsSync(pnpmWorkspacesFilePath)) { return { path, strategy: isTurbo ? "turbo" : "pnpm" }; } } } catch (error) { if (!(error instanceof NotFoundError)) { throw error; } } throw new Error(`No monorepo root could be found upwards from the directory ${cwd} using lerna, yarn, pnpm, or npm as indicators.`); }, "findMonorepoRootSync"); export { findMonorepoRoot, findMonorepoRootSync };