@@ -332,27 +332,47 @@ void loop() {
332332 await this . loadSketch ( sketch . uri ) ; // Sanity check.
333333 return sketch . uri ;
334334 }
335+
336+ const copy = async ( sourcePath : string , destinationPath : string ) => {
337+ return new Promise < void > ( ( resolve , reject ) => {
338+ ncp . ncp ( sourcePath , destinationPath , async error => {
339+ if ( error ) {
340+ reject ( error ) ;
341+ return ;
342+ }
343+ const newName = path . basename ( destinationPath ) ;
344+ try {
345+ const oldPath = path . join ( destinationPath , new URI ( sketch . mainFileUri ) . path . base ) ;
346+ const newPath = path . join ( destinationPath , `${ newName } .ino` ) ;
347+ if ( oldPath !== newPath ) {
348+ await promisify ( fs . rename ) ( oldPath , newPath ) ;
349+ }
350+ await this . loadSketch ( FileUri . create ( destinationPath ) . toString ( ) ) ; // Sanity check.
351+ resolve ( ) ;
352+ } catch ( e ) {
353+ reject ( e ) ;
354+ }
355+ } ) ;
356+ } ) ;
357+ }
358+ // https://github.com/arduino/arduino-ide/issues/65
359+ // When copying `/path/to/sketchbook/sketch_A` to `/path/to/sketchbook/sketch_A/anything` on a non-POSIX filesystem,
360+ // `ncp` makes a recursion and copies the folders over and over again. In such cases, we copy the source into a temp folder,
361+ // then move it to the desired destination.
335362 const destination = FileUri . fsPath ( destinationUri ) ;
336- await new Promise < void > ( ( resolve , reject ) => {
337- ncp . ncp ( source , destination , async error => {
338- if ( error ) {
339- reject ( error ) ;
363+ let tempDestination = await new Promise < string > ( ( resolve , reject ) => {
364+ temp . track ( ) . mkdir ( { prefix } , async ( err , dirPath ) => {
365+ if ( err ) {
366+ reject ( err ) ;
340367 return ;
341368 }
342- const newName = path . basename ( destination ) ;
343- try {
344- const oldPath = path . join ( destination , new URI ( sketch . mainFileUri ) . path . base ) ;
345- const newPath = path . join ( destination , `${ newName } .ino` ) ;
346- if ( oldPath !== newPath ) {
347- await promisify ( fs . rename ) ( oldPath , newPath ) ;
348- }
349- await this . loadSketch ( destinationUri ) ; // Sanity check.
350- resolve ( ) ;
351- } catch ( e ) {
352- reject ( e ) ;
353- }
369+ resolve ( dirPath ) ;
354370 } ) ;
355371 } ) ;
372+ tempDestination = path . join ( tempDestination , sketch . name ) ;
373+ await fs . promises . mkdir ( tempDestination , { recursive : true } ) ;
374+ await copy ( source , tempDestination ) ;
375+ await copy ( tempDestination , destination ) ;
356376 return FileUri . create ( destination ) . toString ( ) ;
357377 }
358378
0 commit comments