mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(update): share package.json readers
This commit is contained in:
@@ -5,6 +5,7 @@ import path from "node:path";
|
||||
import type { UpdateStepProgress, UpdateStepResult } from "../../infra/update-runner.js";
|
||||
import { resolveStateDir } from "../../config/paths.js";
|
||||
import { resolveOpenClawPackageRoot } from "../../infra/openclaw-root.js";
|
||||
import { readPackageName, readPackageVersion } from "../../infra/package-json.js";
|
||||
import { trimLogTail } from "../../infra/restart-sentinel.js";
|
||||
import { parseSemver } from "../../infra/runtime-guard.js";
|
||||
import { fetchNpmTagVersion } from "../../infra/update-check.js";
|
||||
@@ -68,15 +69,7 @@ export function normalizeVersionTag(tag: string): string | null {
|
||||
return parseSemver(cleaned) ? cleaned : null;
|
||||
}
|
||||
|
||||
export async function readPackageVersion(root: string): Promise<string | null> {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { version?: string };
|
||||
return typeof parsed.version === "string" ? parsed.version : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
export { readPackageName, readPackageVersion };
|
||||
|
||||
export async function resolveTargetVersion(
|
||||
tag: string,
|
||||
@@ -99,17 +92,6 @@ export async function isGitCheckout(root: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function readPackageName(root: string): Promise<string | null> {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { name?: string };
|
||||
const name = parsed?.name?.trim();
|
||||
return name ? name : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function isCorePackage(root: string): Promise<boolean> {
|
||||
const name = await readPackageName(root);
|
||||
return Boolean(name && CORE_PACKAGE_NAMES.has(name));
|
||||
|
||||
23
src/infra/package-json.ts
Normal file
23
src/infra/package-json.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
export async function readPackageVersion(root: string): Promise<string | null> {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { version?: string };
|
||||
return typeof parsed?.version === "string" ? parsed.version : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function readPackageName(root: string): Promise<string | null> {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { name?: string };
|
||||
const name = parsed?.name?.trim();
|
||||
return name ? name : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
resolveControlUiDistIndexPathForRoot,
|
||||
} from "./control-ui-assets.js";
|
||||
import { detectPackageManager as detectPackageManagerImpl } from "./detect-package-manager.js";
|
||||
import { readPackageName, readPackageVersion } from "./package-json.js";
|
||||
import { trimLogTail } from "./restart-sentinel.js";
|
||||
import {
|
||||
channelToNpmTag,
|
||||
@@ -131,27 +132,6 @@ function buildStartDirs(opts: UpdateRunnerOptions): string[] {
|
||||
return Array.from(new Set(dirs));
|
||||
}
|
||||
|
||||
async function readPackageVersion(root: string) {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { version?: string };
|
||||
return typeof parsed?.version === "string" ? parsed.version : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function readPackageName(root: string) {
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(root, "package.json"), "utf-8");
|
||||
const parsed = JSON.parse(raw) as { name?: string };
|
||||
const name = parsed?.name?.trim();
|
||||
return name ? name : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function readBranchName(
|
||||
runCommand: CommandRunner,
|
||||
root: string,
|
||||
|
||||
Reference in New Issue
Block a user