mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 04:01:23 +00:00
refactor(update): dedupe package manager detection
This commit is contained in:
29
src/infra/detect-package-manager.ts
Normal file
29
src/infra/detect-package-manager.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
export type DetectedPackageManager = "pnpm" | "bun" | "npm";
|
||||
|
||||
export async function detectPackageManager(root: string): Promise<DetectedPackageManager | null> {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { packageManager?: string };
|
||||
const pm = parsed?.packageManager?.split("@")[0]?.trim();
|
||||
if (pm === "pnpm" || pm === "bun" || pm === "npm") {
|
||||
return pm;
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
const files = await fs.readdir(root).catch((): string[] => []);
|
||||
if (files.includes("pnpm-lock.yaml")) {
|
||||
return "pnpm";
|
||||
}
|
||||
if (files.includes("bun.lockb")) {
|
||||
return "bun";
|
||||
}
|
||||
if (files.includes("package-lock.json")) {
|
||||
return "npm";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user