mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:01:23 +00:00
fix(plugins): sanitize workspace deps before plugin install
Co-authored-by: guanyu-zhang <guanyu-zhang@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,53 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { runCommandWithTimeout } from "../process/exec.js";
|
||||
import { fileExists } from "./archive.js";
|
||||
|
||||
function isObjectRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
async function sanitizeManifestForNpmInstall(targetDir: string): Promise<void> {
|
||||
const manifestPath = path.join(targetDir, "package.json");
|
||||
let manifestRaw = "";
|
||||
try {
|
||||
manifestRaw = await fs.readFile(manifestPath, "utf-8");
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
let manifest: Record<string, unknown>;
|
||||
try {
|
||||
const parsed = JSON.parse(manifestRaw) as unknown;
|
||||
if (!isObjectRecord(parsed)) {
|
||||
return;
|
||||
}
|
||||
manifest = parsed;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
const devDependencies = manifest.devDependencies;
|
||||
if (!isObjectRecord(devDependencies)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const filteredEntries = Object.entries(devDependencies).filter(([, rawSpec]) => {
|
||||
const spec = typeof rawSpec === "string" ? rawSpec.trim() : "";
|
||||
return !spec.startsWith("workspace:");
|
||||
});
|
||||
if (filteredEntries.length === Object.keys(devDependencies).length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (filteredEntries.length === 0) {
|
||||
delete manifest.devDependencies;
|
||||
} else {
|
||||
manifest.devDependencies = Object.fromEntries(filteredEntries);
|
||||
}
|
||||
await fs.writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, "utf-8");
|
||||
}
|
||||
|
||||
export async function installPackageDir(params: {
|
||||
sourceDir: string;
|
||||
targetDir: string;
|
||||
@@ -43,6 +89,7 @@ export async function installPackageDir(params: {
|
||||
}
|
||||
|
||||
if (params.hasDeps) {
|
||||
await sanitizeManifestForNpmInstall(params.targetDir);
|
||||
params.logger?.info?.(params.depsLogMessage);
|
||||
const npmRes = await runCommandWithTimeout(
|
||||
["npm", "install", "--omit=dev", "--silent", "--ignore-scripts"],
|
||||
|
||||
Reference in New Issue
Block a user