refactor: prune legacy plugin dependency debris on postinstall

This commit is contained in:
Peter Steinberger
2026-05-01 22:08:24 +01:00
parent 4def4073d4
commit 01c384cbf9
3 changed files with 84 additions and 88 deletions

View File

@@ -164,12 +164,6 @@ function assertSafeInstalledDistPath(relativePath, params) {
return candidatePath;
}
function isStagedRuntimeDependencyPath(relativePath) {
return /^dist\/extensions\/[^/]+\/(?:node_modules|\.openclaw-install-stage(?:-[^/]+)?)(?:\/|$)/u.test(
normalizeRelativePath(relativePath),
);
}
function listInstalledDistFiles(params = {}) {
const readDir = params.readdirSync ?? readdirSync;
const distRoot = resolveInstalledDistRoot(params);
@@ -184,10 +178,6 @@ function listInstalledDistFiles(params = {}) {
if (!currentDir) {
continue;
}
const relativeCurrentDir = normalizeRelativePath(relative(packageRoot, currentDir));
if (isStagedRuntimeDependencyPath(relativeCurrentDir)) {
continue;
}
for (const entry of readDir(currentDir, { withFileTypes: true })) {
const entryPath = join(currentDir, entry.name);
if (entry.isSymbolicLink()) {
@@ -223,10 +213,6 @@ function pruneEmptyDistDirectories(params = {}) {
const pathLstat = params.lstatSync ?? lstatSync;
function prune(currentDir) {
const relativeCurrentDir = normalizeRelativePath(relative(packageRoot, currentDir));
if (isStagedRuntimeDependencyPath(relativeCurrentDir)) {
return;
}
for (const entry of readDir(currentDir, { withFileTypes: true })) {
if (entry.isSymbolicLink()) {
throw new Error(
@@ -261,6 +247,57 @@ function pruneEmptyDistDirectories(params = {}) {
prune(distRoot.distDir);
}
function isLegacyInstalledPluginDependencyDirName(name) {
return name === "node_modules" || /^\.openclaw-install-stage(?:-[^/]+)?$/iu.test(name);
}
function pruneLegacyInstalledPluginDependencyDirs(params) {
const readDir = params.readdirSync ?? readdirSync;
const removePath = params.rmSync ?? rmSync;
const packageRoot = params.packageRoot ?? DEFAULT_PACKAGE_ROOT;
const extensionsDir = join(packageRoot, "dist", "extensions");
const removed = [];
let pluginEntries;
try {
pluginEntries = readDir(extensionsDir, { withFileTypes: true });
} catch {
return removed;
}
for (const pluginEntry of pluginEntries) {
if (!pluginEntry.isDirectory() || pluginEntry.isSymbolicLink()) {
continue;
}
const pluginDir = join(extensionsDir, pluginEntry.name);
let pluginChildren;
try {
pluginChildren = readDir(pluginDir, { withFileTypes: true });
} catch {
continue;
}
for (const childEntry of pluginChildren) {
if (!isLegacyInstalledPluginDependencyDirName(childEntry.name)) {
continue;
}
const safePluginDir = assertSafeInstalledDistPath(
normalizeRelativePath(relative(packageRoot, pluginDir)),
{
packageRoot,
distDirReal: params.distDirReal,
realpathSync: params.realpathSync,
},
);
const relativePath = normalizeRelativePath(
relative(packageRoot, join(pluginDir, childEntry.name)),
);
removePath(join(safePluginDir, childEntry.name), { recursive: true, force: true });
removed.push(relativePath);
}
}
return removed;
}
const JS_DIST_FILE_RE = /^dist\/.*\.(?:cjs|js|mjs)$/u;
function stripSpecifierSuffix(value) {
@@ -400,6 +437,13 @@ export function pruneInstalledPackageDist(params = {}) {
if (distRoot === null) {
return [];
}
const removedLegacyDependencyDirs = pruneLegacyInstalledPluginDependencyDirs({
packageRoot,
distDirReal: distRoot.distDirReal,
realpathSync: params.realpathSync,
readdirSync: params.readdirSync,
rmSync: params.rmSync,
});
let expectedFiles = params.expectedFiles ?? null;
if (expectedFiles === null) {
try {
@@ -444,6 +488,11 @@ export function pruneInstalledPackageDist(params = {}) {
if (removed.length > 0) {
log.log(`[postinstall] pruned stale dist files: ${removed.join(", ")}`);
}
if (removedLegacyDependencyDirs.length > 0) {
log.log(
`[postinstall] pruned legacy plugin dependency dirs: ${removedLegacyDependencyDirs.join(", ")}`,
);
}
return removed;
}