refactor(cli): extract hook pack update flow

This commit is contained in:
Peter Steinberger
2026-03-22 11:46:22 -07:00
parent 5696e24c3f
commit e3151af6bc
6 changed files with 300 additions and 255 deletions

View File

@@ -1,7 +1,8 @@
import fsSync from "node:fs";
import path from "node:path";
import type { OpenClawConfig } from "../config/config.js";
import { openBoundaryFileSync } from "../infra/boundary-file-read.js";
import {
expectedIntegrityForUpdate,
readInstalledPackageVersion,
} from "../infra/package-update-utils.js";
import type { UpdateChannel } from "../infra/update-channels.js";
import { resolveUserPath } from "../utils.js";
import { resolveBundledPluginSources } from "./bundled-sources.js";
@@ -103,49 +104,6 @@ type InstallIntegrityDrift = {
};
};
function expectedIntegrityForUpdate(
spec: string | undefined,
integrity: string | undefined,
): string | undefined {
if (!integrity || !spec) {
return undefined;
}
const value = spec.trim();
if (!value) {
return undefined;
}
const at = value.lastIndexOf("@");
if (at <= 0 || at >= value.length - 1) {
return undefined;
}
const version = value.slice(at + 1).trim();
if (!/^v?\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(version)) {
return undefined;
}
return integrity;
}
async function readInstalledPackageVersion(dir: string): Promise<string | undefined> {
const manifestPath = path.join(dir, "package.json");
const opened = openBoundaryFileSync({
absolutePath: manifestPath,
rootPath: dir,
boundaryLabel: "installed plugin directory",
});
if (!opened.ok) {
return undefined;
}
try {
const raw = fsSync.readFileSync(opened.fd, "utf-8");
const parsed = JSON.parse(raw) as { version?: unknown };
return typeof parsed.version === "string" ? parsed.version : undefined;
} catch {
return undefined;
} finally {
fsSync.closeSync(opened.fd);
}
}
function pathsEqual(
left: string | undefined,
right: string | undefined,