UNPKG

@visulima/package

Version:

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

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