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