UNPKG

@visulima/package

Version:

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

63 lines (60 loc) 1.83 kB
import { existsSync } from 'node:fs'; import { findUpSync, readJsonSync, findUp } from '@visulima/fs'; import { dirname, join } from '@visulima/path'; import { findLockFileSync, findLockFile } from './package-manager.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const packageJsonMatcher = /* @__PURE__ */ __name((directory) => { if (existsSync(join(directory, "package.json"))) { const packageJson = readJsonSync(join(directory, "package.json")); if (packageJson.name && packageJson.private !== true) { return "package.json"; } } return void 0; }, "packageJsonMatcher"); const findPackageRoot = /* @__PURE__ */ __name(async (cwd) => { try { const lockFile = await findLockFile(cwd); return dirname(lockFile); } catch { } const gitConfig = await findUp(".git/config", { ...cwd && { cwd }, type: "file" }); if (gitConfig) { return dirname(dirname(gitConfig)); } const filePath = await findUp(packageJsonMatcher, { ...cwd && { cwd }, type: "file" }); if (filePath) { return dirname(filePath); } throw new Error("Could not find root directory"); }, "findPackageRoot"); const findPackageRootSync = /* @__PURE__ */ __name((cwd) => { try { const lockFile = findLockFileSync(cwd); return dirname(lockFile); } catch { } const gitConfig = findUpSync(".git/config", { ...cwd && { cwd }, type: "file" }); if (gitConfig) { return dirname(dirname(gitConfig)); } const filePath = findUpSync(packageJsonMatcher, { ...cwd && { cwd }, type: "file" }); if (filePath) { return dirname(filePath); } throw new Error("Could not find root directory"); }, "findPackageRootSync"); export { findPackageRoot, findPackageRootSync };