mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 12:41:22 +00:00
refactor: share install flows across hooks and plugins
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { MANIFEST_KEY } from "../compat/legacy-names.js";
|
||||
import { fileExists, readJsonFile, resolveArchiveKind } from "../infra/archive.js";
|
||||
import { resolveExistingInstallPath, withExtractedArchiveRoot } from "../infra/install-flow.js";
|
||||
import {
|
||||
extractArchive,
|
||||
fileExists,
|
||||
readJsonFile,
|
||||
resolveArchiveKind,
|
||||
resolvePackedRootDir,
|
||||
} from "../infra/archive.js";
|
||||
resolveInstallModeOptions,
|
||||
resolveTimedInstallModeOptions,
|
||||
} from "../infra/install-mode-options.js";
|
||||
import { installPackageDir } from "../infra/install-package-dir.js";
|
||||
import { resolveSafeInstallDir, unscopedPackageName } from "../infra/install-safe-path.js";
|
||||
import {
|
||||
type NpmIntegrityDrift,
|
||||
type NpmSpecResolution,
|
||||
resolveArchiveSourcePath,
|
||||
withTempDir,
|
||||
} from "../infra/install-source-utils.js";
|
||||
import { installFromNpmSpecArchive } from "../infra/npm-pack-install.js";
|
||||
import {
|
||||
finalizeNpmSpecArchiveInstall,
|
||||
installFromNpmSpecArchiveWithInstaller,
|
||||
} from "../infra/npm-pack-install.js";
|
||||
import { validateRegistryNpmSpec } from "../infra/npm-registry-spec.js";
|
||||
import { isPathInside, isPathInsideWithRealpath } from "../security/scan-paths.js";
|
||||
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
||||
@@ -96,30 +97,6 @@ async function ensureOpenClawHooks(manifest: HookPackageManifest) {
|
||||
return list;
|
||||
}
|
||||
|
||||
function resolveHookInstallModeOptions(params: {
|
||||
logger?: HookInstallLogger;
|
||||
mode?: "install" | "update";
|
||||
dryRun?: boolean;
|
||||
}): { logger: HookInstallLogger; mode: "install" | "update"; dryRun: boolean } {
|
||||
return {
|
||||
logger: params.logger ?? defaultLogger,
|
||||
mode: params.mode ?? "install",
|
||||
dryRun: params.dryRun ?? false,
|
||||
};
|
||||
}
|
||||
|
||||
function resolveTimedHookInstallModeOptions(params: {
|
||||
logger?: HookInstallLogger;
|
||||
timeoutMs?: number;
|
||||
mode?: "install" | "update";
|
||||
dryRun?: boolean;
|
||||
}): { logger: HookInstallLogger; timeoutMs: number; mode: "install" | "update"; dryRun: boolean } {
|
||||
return {
|
||||
...resolveHookInstallModeOptions(params),
|
||||
timeoutMs: params.timeoutMs ?? 120_000,
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveInstallTargetDir(
|
||||
id: string,
|
||||
hooksDir?: string,
|
||||
@@ -173,7 +150,7 @@ async function installHookPackageFromDir(params: {
|
||||
dryRun?: boolean;
|
||||
expectedHookPackId?: string;
|
||||
}): Promise<InstallHooksResult> {
|
||||
const { logger, timeoutMs, mode, dryRun } = resolveTimedHookInstallModeOptions(params);
|
||||
const { logger, timeoutMs, mode, dryRun } = resolveTimedInstallModeOptions(params, defaultLogger);
|
||||
|
||||
const manifestPath = path.join(params.packageDir, "package.json");
|
||||
if (!(await fileExists(manifestPath))) {
|
||||
@@ -283,7 +260,7 @@ async function installHookFromDir(params: {
|
||||
dryRun?: boolean;
|
||||
expectedHookPackId?: string;
|
||||
}): Promise<InstallHooksResult> {
|
||||
const { logger, mode, dryRun } = resolveHookInstallModeOptions(params);
|
||||
const { logger, mode, dryRun } = resolveInstallModeOptions(params, defaultLogger);
|
||||
|
||||
await validateHookDir(params.hookDir);
|
||||
const hookName = await resolveHookNameFromDir(params.hookDir);
|
||||
@@ -353,45 +330,34 @@ export async function installHooksFromArchive(params: {
|
||||
}
|
||||
const archivePath = archivePathResult.path;
|
||||
|
||||
return await withTempDir("openclaw-hook-", async (tmpDir) => {
|
||||
const extractDir = path.join(tmpDir, "extract");
|
||||
await fs.mkdir(extractDir, { recursive: true });
|
||||
return await withExtractedArchiveRoot({
|
||||
archivePath,
|
||||
tempDirPrefix: "openclaw-hook-",
|
||||
timeoutMs,
|
||||
logger,
|
||||
onExtracted: async (rootDir) => {
|
||||
const manifestPath = path.join(rootDir, "package.json");
|
||||
if (await fileExists(manifestPath)) {
|
||||
return await installHookPackageFromDir({
|
||||
packageDir: rootDir,
|
||||
hooksDir: params.hooksDir,
|
||||
timeoutMs,
|
||||
logger,
|
||||
mode: params.mode,
|
||||
dryRun: params.dryRun,
|
||||
expectedHookPackId: params.expectedHookPackId,
|
||||
});
|
||||
}
|
||||
|
||||
logger.info?.(`Extracting ${archivePath}…`);
|
||||
try {
|
||||
await extractArchive({ archivePath, destDir: extractDir, timeoutMs, logger });
|
||||
} catch (err) {
|
||||
return { ok: false, error: `failed to extract archive: ${String(err)}` };
|
||||
}
|
||||
|
||||
let rootDir = "";
|
||||
try {
|
||||
rootDir = await resolvePackedRootDir(extractDir);
|
||||
} catch (err) {
|
||||
return { ok: false, error: String(err) };
|
||||
}
|
||||
|
||||
const manifestPath = path.join(rootDir, "package.json");
|
||||
if (await fileExists(manifestPath)) {
|
||||
return await installHookPackageFromDir({
|
||||
packageDir: rootDir,
|
||||
return await installHookFromDir({
|
||||
hookDir: rootDir,
|
||||
hooksDir: params.hooksDir,
|
||||
timeoutMs,
|
||||
logger,
|
||||
mode: params.mode,
|
||||
dryRun: params.dryRun,
|
||||
expectedHookPackId: params.expectedHookPackId,
|
||||
});
|
||||
}
|
||||
|
||||
return await installHookFromDir({
|
||||
hookDir: rootDir,
|
||||
hooksDir: params.hooksDir,
|
||||
logger,
|
||||
mode: params.mode,
|
||||
dryRun: params.dryRun,
|
||||
expectedHookPackId: params.expectedHookPackId,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -406,7 +372,7 @@ export async function installHooksFromNpmSpec(params: {
|
||||
expectedIntegrity?: string;
|
||||
onIntegrityDrift?: (params: HookNpmIntegrityDriftParams) => boolean | Promise<boolean>;
|
||||
}): Promise<InstallHooksResult> {
|
||||
const { logger, timeoutMs, mode, dryRun } = resolveTimedHookInstallModeOptions(params);
|
||||
const { logger, timeoutMs, mode, dryRun } = resolveTimedInstallModeOptions(params, defaultLogger);
|
||||
const expectedHookPackId = params.expectedHookPackId;
|
||||
const spec = params.spec.trim();
|
||||
const specError = validateRegistryNpmSpec(spec);
|
||||
@@ -415,7 +381,7 @@ export async function installHooksFromNpmSpec(params: {
|
||||
}
|
||||
|
||||
logger.info?.(`Downloading ${spec}…`);
|
||||
const flowResult = await installFromNpmSpecArchive({
|
||||
const flowResult = await installFromNpmSpecArchiveWithInstaller({
|
||||
tempDirPrefix: "openclaw-hook-pack-",
|
||||
spec,
|
||||
timeoutMs,
|
||||
@@ -424,28 +390,17 @@ export async function installHooksFromNpmSpec(params: {
|
||||
warn: (message) => {
|
||||
logger.warn?.(message);
|
||||
},
|
||||
installFromArchive: async ({ archivePath }) =>
|
||||
await installHooksFromArchive({
|
||||
archivePath,
|
||||
hooksDir: params.hooksDir,
|
||||
timeoutMs,
|
||||
logger,
|
||||
mode,
|
||||
dryRun,
|
||||
expectedHookPackId,
|
||||
}),
|
||||
installFromArchive: installHooksFromArchive,
|
||||
archiveInstallParams: {
|
||||
hooksDir: params.hooksDir,
|
||||
timeoutMs,
|
||||
logger,
|
||||
mode,
|
||||
dryRun,
|
||||
expectedHookPackId,
|
||||
},
|
||||
});
|
||||
if (!flowResult.ok) {
|
||||
return flowResult;
|
||||
}
|
||||
if (!flowResult.installResult.ok) {
|
||||
return flowResult.installResult;
|
||||
}
|
||||
return {
|
||||
...flowResult.installResult,
|
||||
npmResolution: flowResult.npmResolution,
|
||||
integrityDrift: flowResult.integrityDrift,
|
||||
};
|
||||
return finalizeNpmSpecArchiveInstall(flowResult);
|
||||
}
|
||||
|
||||
export async function installHooksFromPath(params: {
|
||||
@@ -457,12 +412,12 @@ export async function installHooksFromPath(params: {
|
||||
dryRun?: boolean;
|
||||
expectedHookPackId?: string;
|
||||
}): Promise<InstallHooksResult> {
|
||||
const resolved = resolveUserPath(params.path);
|
||||
if (!(await fileExists(resolved))) {
|
||||
return { ok: false, error: `path not found: ${resolved}` };
|
||||
const pathResult = await resolveExistingInstallPath(params.path);
|
||||
if (!pathResult.ok) {
|
||||
return pathResult;
|
||||
}
|
||||
const { resolvedPath: resolved, stat } = pathResult;
|
||||
|
||||
const stat = await fs.stat(resolved);
|
||||
if (stat.isDirectory()) {
|
||||
const manifestPath = path.join(resolved, "package.json");
|
||||
if (await fileExists(manifestPath)) {
|
||||
|
||||
Reference in New Issue
Block a user