|
2 | 2 | import fs from "node:fs"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { safePathSegmentHashed } from "../infra/install-safe-path.js"; |
| 5 | +import { resolveDefaultPluginNpmDir, resolvePluginNpmProjectsDir } from "./install-paths.js"; |
| 6 | +import { listManagedPluginNpmProjectRootsSync } from "./npm-project-roots.js"; |
5 | 7 |
|
6 | 8 | export const RETAINED_MANAGED_NPM_INSTALL_MARKER = ".openclaw-retained-npm-install.json"; |
7 | 9 | const RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR = ".openclaw-retained-npm-installs"; |
@@ -109,3 +111,55 @@ export async function markRetainedManagedNpmInstall(params: { |
109 | 111 | ); |
110 | 112 | return true; |
111 | 113 | } |
| 114 | + |
| 115 | +function isPathEqualOrInside(parentPath: string, childPath: string): boolean { |
| 116 | + const relative = path.relative(path.resolve(parentPath), path.resolve(childPath)); |
| 117 | + return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`)); |
| 118 | +} |
| 119 | + |
| 120 | +export async function cleanupRetainedManagedNpmInstallGenerations( |
| 121 | + params: { |
| 122 | + activeInstallPaths?: Iterable<string>; |
| 123 | + env?: NodeJS.ProcessEnv; |
| 124 | + npmDir?: string; |
| 125 | + onError?: (error: unknown, projectRoot: string) => void; |
| 126 | + } = {}, |
| 127 | +): Promise<number> { |
| 128 | + // Callers run this after the previous gateway server has closed and before |
| 129 | + // the next one loads plugins, so retired module graphs no longer need these trees. |
| 130 | + const npmDir = params.npmDir ?? resolveDefaultPluginNpmDir(params.env); |
| 131 | + const projectsDir = resolvePluginNpmProjectsDir(npmDir); |
| 132 | + const activeInstallPaths = Array.from(params.activeInstallPaths ?? [], (installPath) => |
| 133 | + path.resolve(installPath), |
| 134 | + ); |
| 135 | + let removed = 0; |
| 136 | + for (const projectRoot of listManagedPluginNpmProjectRootsSync(npmDir)) { |
| 137 | + const markerDir = path.join(projectRoot, RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR); |
| 138 | + let markerEntries: fs.Dirent[]; |
| 139 | + try { |
| 140 | + markerEntries = fs |
| 141 | + .readdirSync(markerDir, { withFileTypes: true }) |
| 142 | + .filter((entry) => entry.isFile()); |
| 143 | + } catch (error) { |
| 144 | + if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 145 | + continue; |
| 146 | + } |
| 147 | + params.onError?.(error, projectRoot); |
| 148 | + continue; |
| 149 | + } |
| 150 | + if ( |
| 151 | + markerEntries.length === 0 || |
| 152 | + !isPathEqualOrInside(projectsDir, projectRoot) || |
| 153 | + activeInstallPaths.some((installPath) => isPathEqualOrInside(projectRoot, installPath)) |
| 154 | + ) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + try { |
| 158 | + await fs.promises.rm(projectRoot, { recursive: true, force: true }); |
| 159 | + removed += 1; |
| 160 | + } catch (error) { |
| 161 | + params.onError?.(error, projectRoot); |
| 162 | + } |
| 163 | + } |
| 164 | + return removed; |
| 165 | +} |
0 commit comments