66 */
77
88import { rm } from 'node:fs/promises' ;
9- import { dirname , join , resolve } from 'node:path' ;
9+ import { dirname , join , resolve , sep } from 'node:path' ;
1010import * as fs from 'node:fs' ;
1111
1212import { MultiStageOutput } from '@oclif/multi-stage-output' ;
@@ -21,6 +21,7 @@ import {
2121 MetadataApiRetrieveStatus ,
2222 RegistryAccess ,
2323 RequestStatus ,
24+ ComponentStatus ,
2425} from '@salesforce/source-deploy-retrieve' ;
2526import { SfCommand , toHelpSection , Flags , Ux } from '@salesforce/sf-plugins-core' ;
2627import { getString } from '@salesforce/ts-types' ;
@@ -354,14 +355,18 @@ export default class RetrieveMetadata extends SfCommand<RetrieveResultJson> {
354355 ) ;
355356 return directories ;
356357 }
357- // If we retrieved only a package.xml, just return.
358- if ( this . retrieveResult . getFileResponses ( ) . length < 2 ) {
358+
359+ // skip file move if all retrieves failed to avoid ENOENT err (no such file or directory).
360+ if (
361+ this . retrieveResult . getFileResponses ( ) . length ===
362+ this . retrieveResult . getFileResponses ( ) . filter ( ( c ) => c . state === ComponentStatus . Failed ) . length
363+ ) {
359364 return ;
360365 }
361366
362367 // getFileResponses fails once the files have been moved, calculate where they're moved to, and then move them
363368 this . retrieveResult . getFileResponses ( ) . forEach ( ( fileResponse ) => {
364- fileResponse . filePath = fileResponse . filePath ?. replace ( join ( 'main' , 'default' ) , '' ) ;
369+ fileResponse . filePath = fileResponse . filePath ?. replace ( join ( 'main' , 'default' , sep ) , '' ) ;
365370 } ) ;
366371 // move contents of 'main/default' to 'retrievetargetdir'
367372 await promisesQueue ( [ join ( resolvedTargetDir , 'main' , 'default' ) ] , mv , 5 , true ) ;
0 commit comments