mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 21:01:43 +03:00
fix: CLI harden update restart imports and fix nested bundle version resolution
This commit is contained in:
+46
-18
@@ -1,29 +1,41 @@
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
declare const __OPENCLAW_VERSION__: string | undefined;
|
||||
const CORE_PACKAGE_NAME = "openclaw";
|
||||
|
||||
function readVersionFromPackageJson(): string | null {
|
||||
try {
|
||||
const require = createRequire(import.meta.url);
|
||||
const pkg = require("../package.json") as { version?: string };
|
||||
return pkg.version ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
const PACKAGE_JSON_CANDIDATES = [
|
||||
"../package.json",
|
||||
"../../package.json",
|
||||
"../../../package.json",
|
||||
"./package.json",
|
||||
] as const;
|
||||
|
||||
function readVersionFromBuildInfo(): string | null {
|
||||
const BUILD_INFO_CANDIDATES = [
|
||||
"../build-info.json",
|
||||
"../../build-info.json",
|
||||
"./build-info.json",
|
||||
] as const;
|
||||
|
||||
function readVersionFromJsonCandidates(
|
||||
moduleUrl: string,
|
||||
candidates: readonly string[],
|
||||
opts: { requirePackageName?: boolean } = {},
|
||||
): string | null {
|
||||
try {
|
||||
const require = createRequire(import.meta.url);
|
||||
const candidates = ["../build-info.json", "./build-info.json"];
|
||||
const require = createRequire(moduleUrl);
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
const info = require(candidate) as { version?: string };
|
||||
if (info.version) {
|
||||
return info.version;
|
||||
const parsed = require(candidate) as { name?: string; version?: string };
|
||||
const version = parsed.version?.trim();
|
||||
if (!version) {
|
||||
continue;
|
||||
}
|
||||
if (opts.requirePackageName && parsed.name !== CORE_PACKAGE_NAME) {
|
||||
continue;
|
||||
}
|
||||
return version;
|
||||
} catch {
|
||||
// ignore missing candidate
|
||||
// ignore missing or unreadable candidate
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -32,12 +44,28 @@ function readVersionFromBuildInfo(): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
export function readVersionFromPackageJsonForModuleUrl(moduleUrl: string): string | null {
|
||||
return readVersionFromJsonCandidates(moduleUrl, PACKAGE_JSON_CANDIDATES, {
|
||||
requirePackageName: true,
|
||||
});
|
||||
}
|
||||
|
||||
export function readVersionFromBuildInfoForModuleUrl(moduleUrl: string): string | null {
|
||||
return readVersionFromJsonCandidates(moduleUrl, BUILD_INFO_CANDIDATES);
|
||||
}
|
||||
|
||||
export function resolveVersionFromModuleUrl(moduleUrl: string): string | null {
|
||||
return (
|
||||
readVersionFromPackageJsonForModuleUrl(moduleUrl) ||
|
||||
readVersionFromBuildInfoForModuleUrl(moduleUrl)
|
||||
);
|
||||
}
|
||||
|
||||
// Single source of truth for the current OpenClaw version.
|
||||
// - Embedded/bundled builds: injected define or env var.
|
||||
// - Dev/npm builds: package.json.
|
||||
export const VERSION =
|
||||
(typeof __OPENCLAW_VERSION__ === "string" && __OPENCLAW_VERSION__) ||
|
||||
process.env.OPENCLAW_BUNDLED_VERSION ||
|
||||
readVersionFromPackageJson() ||
|
||||
readVersionFromBuildInfo() ||
|
||||
resolveVersionFromModuleUrl(import.meta.url) ||
|
||||
"0.0.0";
|
||||
|
||||
Reference in New Issue
Block a user