30 lines
826 B
TypeScript
30 lines
826 B
TypeScript
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;
|
|
}
|