@@ -3,7 +3,7 @@ import fs from "node:fs";
33import path from "node:path" ;
44import { safePathSegmentHashed } from "../infra/install-safe-path.js" ;
55import { resolveDefaultPluginNpmDir , resolvePluginNpmProjectsDir } from "./install-paths.js" ;
6- import { listManagedPluginNpmProjectRootsSync } from "./npm-project-roots.js" ;
6+ import { listManagedPluginNpmRootsSync } from "./npm-project-roots.js" ;
77
88export const RETAINED_MANAGED_NPM_INSTALL_MARKER = ".openclaw-retained-npm-install.json" ;
99const RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR = ".openclaw-retained-npm-installs" ;
@@ -117,6 +117,55 @@ function isPathEqualOrInside(parentPath: string, childPath: string): boolean {
117117 return relative === "" || ( relative !== ".." && ! relative . startsWith ( `..${ path . sep } ` ) ) ;
118118}
119119
120+ function listManagedNpmPackageDirs ( npmRoot : string ) : string [ ] {
121+ const nodeModulesDir = path . join ( npmRoot , "node_modules" ) ;
122+ let entries : fs . Dirent [ ] ;
123+ try {
124+ entries = fs . readdirSync ( nodeModulesDir , { withFileTypes : true } ) ;
125+ } catch ( error ) {
126+ if ( ( error as NodeJS . ErrnoException ) . code === "ENOENT" ) {
127+ return [ ] ;
128+ }
129+ throw error ;
130+ }
131+ return entries . flatMap ( ( entry ) => {
132+ if ( ! entry . isDirectory ( ) ) {
133+ return [ ] ;
134+ }
135+ if ( ! entry . name . startsWith ( "@" ) ) {
136+ return [ path . join ( nodeModulesDir , entry . name ) ] ;
137+ }
138+ return fs
139+ . readdirSync ( path . join ( nodeModulesDir , entry . name ) , { withFileTypes : true } )
140+ . filter ( ( scopedEntry ) => scopedEntry . isDirectory ( ) )
141+ . map ( ( scopedEntry ) => path . join ( nodeModulesDir , entry . name , scopedEntry . name ) ) ;
142+ } ) ;
143+ }
144+
145+ async function cleanupRetainedLegacyNpmPackages ( params : {
146+ npmRoot : string ;
147+ activeInstallPaths : string [ ] ;
148+ onError ?: ( error : unknown , projectRoot : string ) => void ;
149+ } ) : Promise < number > {
150+ let removed = 0 ;
151+ for ( const packageDir of listManagedNpmPackageDirs ( params . npmRoot ) ) {
152+ if (
153+ ! hasRetainedManagedNpmInstallMarker ( packageDir ) ||
154+ params . activeInstallPaths . some ( ( installPath ) => isPathEqualOrInside ( packageDir , installPath ) )
155+ ) {
156+ continue ;
157+ }
158+ try {
159+ await fs . promises . rm ( packageDir , { recursive : true , force : true } ) ;
160+ await clearRetainedManagedNpmInstallMarker ( packageDir ) ;
161+ removed += 1 ;
162+ } catch ( error ) {
163+ params . onError ?.( error , packageDir ) ;
164+ }
165+ }
166+ return removed ;
167+ }
168+
120169export async function cleanupRetainedManagedNpmInstallGenerations (
121170 params : {
122171 activeInstallPaths ?: Iterable < string > ;
@@ -133,7 +182,15 @@ export async function cleanupRetainedManagedNpmInstallGenerations(
133182 path . resolve ( installPath ) ,
134183 ) ;
135184 let removed = 0 ;
136- for ( const projectRoot of listManagedPluginNpmProjectRootsSync ( npmDir ) ) {
185+ for ( const projectRoot of listManagedPluginNpmRootsSync ( npmDir ) ) {
186+ if ( path . resolve ( projectRoot ) === path . resolve ( npmDir ) ) {
187+ removed += await cleanupRetainedLegacyNpmPackages ( {
188+ npmRoot : projectRoot ,
189+ activeInstallPaths,
190+ onError : params . onError ,
191+ } ) ;
192+ continue ;
193+ }
137194 const markerDir = path . join ( projectRoot , RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR ) ;
138195 let markerEntries : fs . Dirent [ ] ;
139196 try {
0 commit comments