openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
69 lines (68 loc) • 2.69 kB
JavaScript
import { t as canAccess } from "./path-existence-Co7-GyVr.mjs";
import { n as readPackageManagerSpec } from "./package-json-DXb4lz-W.mjs";
import path from "node:path";
import fs from "node:fs/promises";
import os from "node:os";
//#region src/infra/detect-package-manager.ts
/** Resolves Bun's global project from its installed owner rather than unrelated caller settings. */
function resolveBunGlobalInstallOwner(e, t = process.env) {
const n = t.BUN_INSTALL?.trim();
const r = t.BUN_INSTALL_GLOBAL_DIR?.trim();
if (e == null) {
const e = n || path.join(os.homedir(), ".bun");
const t = path.resolve(r || path.join(e, "install", "global"));
return {
globalRoot: path.join(t, "node_modules"),
globalProjectRoot: t,
...!r || n ? { bunInstall: path.resolve(e) } : {}
};
}
const i = e.trim();
if (!i) return null;
let a = path.dirname(path.resolve(i));
if (path.basename(a).startsWith("@")) a = path.dirname(a);
if (path.basename(a) !== "node_modules") return null;
const o = path.dirname(a);
const s = path.dirname(o);
const c = path.basename(o) === "global" && path.basename(s) === "install";
const l = r !== void 0 && path.resolve(r) === o;
if (!c && !l) return null;
const u = c ? path.dirname(s) : n;
return {
globalRoot: a,
globalProjectRoot: o,
...u ? { bunInstall: path.resolve(u) } : {}
};
}
function resolvePnpmNodeModulesRoot(e) {
const t = path.resolve(e);
const n = t.split(path.sep);
const r = n.lastIndexOf(".pnpm");
if (r > 0) {
const e = n.slice(0, r).join(path.sep) || path.sep;
return path.basename(e) === "node_modules" ? e : path.join(e, "node_modules");
}
const i = path.dirname(t);
return path.basename(i) === "node_modules" ? i : null;
}
async function isPnpmOwnedPackageRoot(e) {
const t = resolvePnpmNodeModulesRoot(e);
return t !== null && await canAccess(path.join(t, ".modules.yaml"));
}
/** Detects the package manager that owns a package root from manifests, locks, and install layout. */
async function detectPackageManager(e) {
const t = (await readPackageManagerSpec(e))?.split("@")[0]?.trim();
const n = await fs.readdir(e).catch(() => []);
const r = n.includes("npm-shrinkwrap.json");
const i = n.includes("pnpm-lock.yaml");
const a = n.includes("bun.lock") || n.includes("bun.lockb");
if (resolveBunGlobalInstallOwner(e)) return "bun";
if (r && !(t === "pnpm" && (i || await isPnpmOwnedPackageRoot(e))) && !(t === "bun" && a)) return "npm";
if (t === "pnpm" || t === "bun" || t === "npm") return t;
if (i) return "pnpm";
if (a) return "bun";
if (n.includes("package-lock.json")) return "npm";
return null;
}
//#endregion
export { resolveBunGlobalInstallOwner as n, detectPackageManager as t };