@@ -128,13 +128,22 @@ export async function main(): Promise<void> {
128128 await runScript ( script , scriptPath , tempPath )
129129}
130130
131- const rmrf = ( p : string ) => p && fs . rmSync ( p , { force : true , recursive : true } )
131+ // Short & safe remove: unlink symlinks; recurse only for real dirs/files
132+ const rmrf = ( p : string ) => {
133+ if ( ! p ) return
134+ try {
135+ fs . lstatSync ( p ) . isSymbolicLink ( )
136+ ? fs . unlinkSync ( p )
137+ : fs . rmSync ( p , { force : true , recursive : true } )
138+ } catch { }
139+ }
140+
132141async function runScript (
133142 script : string ,
134143 scriptPath : string ,
135144 tempPath : string
136145) : Promise < void > {
137- let nmLink = ''
146+ let nmLink = '' // will hold the alias path (./node_modules) ONLY if it's a symlink
138147 const rmTemp = ( ) => {
139148 rmrf ( tempPath )
140149 rmrf ( nmLink )
@@ -145,9 +154,25 @@ async function runScript(
145154 await fs . writeFile ( tempPath , script )
146155 }
147156 const cwd = path . dirname ( scriptPath )
157+
148158 if ( typeof argv . preferLocal === 'string' ) {
149- nmLink = linkNodeModules ( cwd , argv . preferLocal )
159+ // Keep original behaviour: linkNodeModules returns TARGET (unchanged API)
160+ linkNodeModules ( cwd , argv . preferLocal )
161+
162+ // For cleanup, compute ALIAS and only unlink if it's a symlink
163+ try {
164+ const aliasPath = path . resolve ( cwd , 'node_modules' )
165+ if (
166+ fs . existsSync ( aliasPath ) &&
167+ fs . lstatSync ( aliasPath ) . isSymbolicLink ( )
168+ ) {
169+ nmLink = aliasPath
170+ } else {
171+ nmLink = ''
172+ }
173+ } catch { }
150174 }
175+
151176 if ( argv . install ) {
152177 await installDeps ( parseDeps ( script ) , cwd , argv . registry )
153178 }
@@ -173,6 +198,7 @@ function linkNodeModules(cwd: string, external: string): string {
173198 if ( fs . existsSync ( alias ) || ! fs . existsSync ( target ) ) return ''
174199
175200 fs . symlinkSync ( target , alias , 'junction' )
201+ // Keep behaviour stable: return TARGET (not alias)
176202 return target
177203}
178204
0 commit comments