Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/commands/project/retrieve/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { rm } from 'node:fs/promises';
import { dirname, join, resolve } from 'node:path';
import { dirname, join, resolve, sep } from 'node:path';
import * as fs from 'node:fs';

import { MultiStageOutput } from '@oclif/multi-stage-output';
Expand All @@ -21,6 +21,7 @@ import {
MetadataApiRetrieveStatus,
RegistryAccess,
RequestStatus,
ComponentStatus,
} from '@salesforce/source-deploy-retrieve';
import { SfCommand, toHelpSection, Flags, Ux } from '@salesforce/sf-plugins-core';
import { getString } from '@salesforce/ts-types';
Expand Down Expand Up @@ -354,14 +355,18 @@ export default class RetrieveMetadata extends SfCommand<RetrieveResultJson> {
);
return directories;
}
// If we retrieved only a package.xml, just return.
if (this.retrieveResult.getFileResponses().length < 2) {

// skip file move if all retrieves failed to avoid ENOENT err (no such file or directory).
if (
this.retrieveResult.getFileResponses().length ===
this.retrieveResult.getFileResponses().filter((c) => c.state === ComponentStatus.Failed).length
) {
return;
}

// getFileResponses fails once the files have been moved, calculate where they're moved to, and then move them
this.retrieveResult.getFileResponses().forEach((fileResponse) => {
fileResponse.filePath = fileResponse.filePath?.replace(join('main', 'default'), '');
fileResponse.filePath = fileResponse.filePath?.replace(join('main', 'default', sep), '');
});
// move contents of 'main/default' to 'retrievetargetdir'
await promisesQueue([join(resolvedTargetDir, 'main', 'default')], mv, 5, true);
Expand Down
15 changes: 14 additions & 1 deletion test/nuts/retrieve/metadata.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { execCmd } from '@salesforce/cli-plugins-testkit';
import { SourceTestkit } from '@salesforce/source-testkit';
import { expect } from 'chai';
import { expect, config } from 'chai';
import { RetrieveResultJson } from '../../../src/utils/types.js';

config.truncateThreshold = 0;

const ELECTRON = { id: '04t6A000002zgKSQAY', name: 'ElectronBranding' };

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

it('should retrieve a single metadata file with correct path', async () => {
const result = await testkit.retrieve({ args: '--metadata CustomTab:Broker__c --output-dir myOutput --json' });
expect(result?.status).to.equal(0);
const retrieveResult = result?.result as unknown as RetrieveResultJson;
expect(retrieveResult.success).to.equal(true);
expect(retrieveResult.files).to.be.an('array').with.lengthOf(1);
expect(retrieveResult.files[0].filePath).to.equal(
path.join(testkit.projectDir, 'myOutput', 'tabs', 'Broker__c.tab-meta.xml')
);
});

it('should warn when nothing retrieved into output-dir and not throw ENOENT', async () => {
const result = await testkit.retrieve({ args: '--metadata ApexClass:NonExistant --output-dir myOutput' });
expect(result?.status).to.equal(0);
Expand Down
Loading