@@ -95,33 +95,53 @@ export default class AsyncRecycler {
9595 }
9696
9797 // Asynchronously delete the folder contents.
98- const recyclerFolderContents : string = path . join ( this . recyclerFolder , '*' ) ;
99-
100- const windowsTrimmedRecyclerFolder : string = this . recyclerFolder . match ( / \\ $ / )
101- ? this . recyclerFolder . substring ( 0 , this . recyclerFolder . length - 1 )
102- : this . recyclerFolder ;
103- const command : string = os . platform ( ) === 'win32'
104- // Windows
105- ? 'cmd.exe'
106- // Assume 'NIX or Darwin
107- : 'rm' ;
108-
109- const args : string [ ] = os . platform ( ) === 'win32'
110- // Windows
111- ? [
98+ let command : string ;
99+ let args : string [ ] ;
100+
101+ if ( os . platform ( ) === 'win32' ) {
102+ const recyclerFolderWildcard : string = path . join ( this . recyclerFolder , '*' ) ;
103+
104+ const windowsTrimmedRecyclerFolder : string = this . recyclerFolder . match ( / \\ $ / )
105+ ? this . recyclerFolder . substring ( 0 , this . recyclerFolder . length - 1 )
106+ : this . recyclerFolder ;
107+ command = 'cmd.exe' ;
108+
109+ args = [
112110 '/c' ,
113- `FOR /F %f IN ('dir /B \\\\?\\${ recyclerFolderContents } ') `
111+ `FOR /F %f IN ('dir /B \\\\?\\${ recyclerFolderWildcard } ') `
114112 + `DO rd /S /Q \\\\?\\${ windowsTrimmedRecyclerFolder } \\%f`
115- ]
116- // Assume 'NIX or Darwin
117- : [ '-rf' , `"${ recyclerFolderContents } "` ] ;
113+ ] ;
114+ } else {
115+ command = 'rm' ;
116+ args = [ '-rf' ] ;
117+
118+ let pathCount : number = 0 ;
119+
120+ // child_process.spawn() doesn't expand wildcards. To be safe, we will do it manually
121+ // rather than rely on an unknown shell.
122+ for ( const filename of fsx . readdirSync ( this . recyclerFolder ) ) {
123+ // The "." and ".." are supposed to be excluded, but let's be safe
124+ if ( filename !== '.' && filename !== '..' ) {
125+ args . push ( path . join ( this . recyclerFolder , filename ) ) ;
126+ ++ pathCount ;
127+ }
128+ }
129+
130+ if ( pathCount === 0 ) {
131+ // Nothing to do
132+ return ;
133+ }
134+ }
118135
119136 const options : child_process . SpawnOptions = {
120137 detached : true ,
138+ // The child won't stay alive unless we detach its stdio
121139 stdio : [ 'ignore' , 'ignore' , 'ignore' ]
122140 } ;
123141
124142 const process : child_process . ChildProcess = child_process . spawn ( command , args , options ) ;
143+
144+ // The child won't stay alive unless we unlink it from the parent process
125145 process . unref ( ) ;
126146 }
127147
0 commit comments