openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
103 lines (102 loc) • 3.83 kB
JavaScript
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { p as statIfExists } from "./path-existence-Co7-GyVr.mjs";
import { l as tryReadJson } from "./json-files-Cb1h8pYs.mjs";
import { t as isPackageDependencyName } from "./package-json-DXb4lz-W.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/plugins/installed-package-runtime-graph.ts
function runtimeDependencies(e, t) {
const n = /* @__PURE__ */ new Map();
const r = asNullableRecord(e.peerDependenciesMeta);
if (e.peerDependenciesMeta !== void 0 && !r) t("Invalid peerDependenciesMeta map; expected an object.");
for (const i of [
"dependencies",
"optionalDependencies",
"peerDependencies"
]) {
if (e[i] === void 0) continue;
const a = asNullableRecord(e[i]);
if (!a) {
t(`Invalid ${i} map; expected an object.`);
continue;
}
for (const [e, o] of Object.entries(a)) {
if (i === "peerDependencies" && e === "openclaw") continue;
if (!isPackageDependencyName(e)) {
t(`Invalid ${i} package name ${JSON.stringify(e)}.`);
continue;
}
if (typeof o !== "string") t(`Invalid dependency specifier for ${JSON.stringify(e)}; expected a string.`);
const a = r?.[e];
const s = asNullableRecord(a);
if (i === "peerDependencies" && a !== void 0 && (!s || s.optional !== void 0 && typeof s.optional !== "boolean")) t(`Invalid peerDependenciesMeta entry for ${JSON.stringify(e)}.`);
const c = i === "optionalDependencies" || i === "peerDependencies" && s?.optional === true;
if (i !== "peerDependencies" || !n.has(e) || !c) n.set(e, c);
}
}
return [...n].map(([e, t]) => ({
name: e,
optional: t
})).toSorted((e, t) => e.name.localeCompare(t.name));
}
/** Resolve manifest-owned runtime dependencies without importing package code. */
async function collectInstalledRuntimePackageGraph(e) {
const t = e.lookupRootDir;
const n = t ? await fs.realpath(t).catch(() => path.resolve(t)) : void 0;
const r = [];
for (const t of [e.packageDir, ...e.additionalPackageDirs ?? []]) {
const n = {
packageDir: t,
realPath: await fs.realpath(t).catch(() => path.resolve(t))
};
if (e.admitPackage?.(n) !== false) r.push(n);
}
const i = /* @__PURE__ */ new Set();
const a = [];
const o = /* @__PURE__ */ new Map();
async function resolveDependency(t, r) {
let i = t;
while (true) {
if (path.basename(i) !== "node_modules") {
const t = path.join(i, "node_modules", r);
if ((await statIfExists(t))?.isDirectory()) {
const n = {
packageDir: t,
realPath: await fs.realpath(t).catch(() => path.resolve(t))
};
return e.admitPackage?.({
...n,
dependencyName: r
}) === false ? "excluded" : n;
}
}
if (i === n || path.dirname(i) === i) return;
i = path.dirname(i);
}
}
for (const t of r) {
if (i.has(t.realPath)) continue;
i.add(t.realPath);
if (i.size > (e.maxPackages ?? 1e4)) throw new Error(`installed dependency scan exceeded max packages (${e.maxPackages ?? 1e4}) under ${e.lookupRootDir ?? e.packageDir}`);
a.push(t.packageDir);
const n = asNullableRecord(await tryReadJson(path.join(t.packageDir, "package.json")));
if (!n) continue;
const reportIncomplete = (e) => {
const n = o.get(t.packageDir) ?? [];
n.push(e);
o.set(t.packageDir, n);
};
for (const { name: e, optional: a } of runtimeDependencies(n, reportIncomplete)) {
const n = await resolveDependency(t.realPath, e);
if (n === "excluded") continue;
if (n && !i.has(n.realPath)) r.push(n);
else if (!n && !a && e !== "openclaw") reportIncomplete(`Missing required runtime dependency ${JSON.stringify(e)}; install package dependencies before qualification.`);
}
}
return {
packageDirs: a,
incomplete: o
};
}
//#endregion
export { collectInstalledRuntimePackageGraph as t };