Skip to content

Commit ee567b2

Browse files
authored
Merge pull request #1289 from salesforcecli/cd/fix-output-mv
fix: handle single file components move to output-dir W-17615754
2 parents 055c94d + a97d976 commit ee567b2

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/commands/project/retrieve/start.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { rm } from 'node:fs/promises';
9-
import { dirname, join, resolve } from 'node:path';
9+
import { dirname, join, resolve, sep } from 'node:path';
1010
import * as fs from 'node:fs';
1111

1212
import { 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';
2526
import { SfCommand, toHelpSection, Flags, Ux } from '@salesforce/sf-plugins-core';
2627
import { 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);

test/nuts/retrieve/metadata.nut.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import * as fs from 'node:fs';
1010
import { fileURLToPath } from 'node:url';
1111
import { execCmd } from '@salesforce/cli-plugins-testkit';
1212
import { SourceTestkit } from '@salesforce/source-testkit';
13-
import { expect } from 'chai';
13+
import { expect, config } from 'chai';
1414
import { RetrieveResultJson } from '../../../src/utils/types.js';
1515

16+
config.truncateThreshold = 0;
17+
1618
const ELECTRON = { id: '04t6A000002zgKSQAY', name: 'ElectronBranding' };
1719

1820
describe('retrieve metadata NUTs', () => {
@@ -83,6 +85,17 @@ describe('retrieve metadata NUTs', () => {
8385
await testkit.expect.filesToBeRetrieved(['myOutput/classes/*', 'myOutput/aura/**/*']);
8486
});
8587

88+
it('should retrieve a single metadata file with correct path', async () => {
89+
const result = await testkit.retrieve({ args: '--metadata CustomTab:Broker__c --output-dir myOutput --json' });
90+
expect(result?.status).to.equal(0);
91+
const retrieveResult = result?.result as unknown as RetrieveResultJson;
92+
expect(retrieveResult.success).to.equal(true);
93+
expect(retrieveResult.files).to.be.an('array').with.lengthOf(1);
94+
expect(retrieveResult.files[0].filePath).to.equal(
95+
path.join(testkit.projectDir, 'myOutput', 'tabs', 'Broker__c.tab-meta.xml')
96+
);
97+
});
98+
8699
it('should warn when nothing retrieved into output-dir and not throw ENOENT', async () => {
87100
const result = await testkit.retrieve({ args: '--metadata ApexClass:NonExistant --output-dir myOutput' });
88101
expect(result?.status).to.equal(0);

0 commit comments

Comments
 (0)