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;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { runCommandWithTimeout } from "../process/exec.js";
|
||||
import { fetchWithTimeout } from "../utils/fetch-timeout.js";
|
||||
import { detectPackageManager as detectPackageManagerImpl } from "./detect-package-manager.js";
|
||||
import { parseSemver } from "./runtime-guard.js";
|
||||
import { channelToNpmTag, type UpdateChannel } from "./update-channels.js";
|
||||
|
||||
@@ -58,28 +59,7 @@ async function exists(p: string): Promise<boolean> {
|
||||
}
|
||||
|
||||
async function detectPackageManager(root: string): Promise<PackageManager> {
|
||||
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 "unknown";
|
||||
return (await detectPackageManagerImpl(root)) ?? "unknown";
|
||||
}
|
||||
|
||||
async function detectGitRoot(root: string): Promise<string | null> {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
resolveControlUiDistIndexHealth,
|
||||
resolveControlUiDistIndexPathForRoot,
|
||||
} from "./control-ui-assets.js";
|
||||
import { detectPackageManager as detectPackageManagerImpl } from "./detect-package-manager.js";
|
||||
import { trimLogTail } from "./restart-sentinel.js";
|
||||
import {
|
||||
channelToNpmTag,
|
||||
@@ -254,28 +255,7 @@ async function findPackageRoot(candidates: string[]) {
|
||||
}
|
||||
|
||||
async function detectPackageManager(root: string) {
|
||||
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 "npm";
|
||||
return (await detectPackageManagerImpl(root)) ?? "npm";
|
||||
}
|
||||
|
||||
type RunStepOptions = {
|
||||
|
||||
Reference in New Issue
Block a user