Skip to content

Commit

Permalink
fix: metadataConverter writes zip to dir and works outside of project (
Browse files Browse the repository at this point in the history
…#1252)

* fix: metadataConverter writes zip to dir and works outside of project

* fix: update fs import

* fix: update write file call and unit-tests for metadataConverter

* fix: reading replacements from project ignores invalid workspace error
  • Loading branch information
aaron-csetter authored Mar 13, 2024
1 parent 2526a59 commit d947fc6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/convert/metadataConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ export class MetadataConverter {
await Promise.all([conversionPipeline, ...tasks]);

const result: ConvertResult = { packagePath };
if (output.type === 'zip' && !packagePath) {
result.zipBuffer = (writer as ZipWriter).buffer;
} else if (output.type !== 'zip') {

if (output.type === 'zip') {
const buffer = (writer as ZipWriter).buffer;
if (!packagePath) {
result.zipBuffer = buffer;
} else if (buffer) {
await promises.writeFile(packagePath, buffer);
}
} else {
result.converted = (writer as StandardWriter).converted;
}
return result;
Expand Down
17 changes: 13 additions & 4 deletions src/convert/replacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { readFile } from 'node:fs/promises';
import { Transform, Readable } from 'node:stream';
import { sep, posix } from 'node:path';
import { Lifecycle, Messages, SfProject } from '@salesforce/core';
import { Lifecycle, Messages, SfError, SfProject } from '@salesforce/core';
import * as minimatch from 'minimatch';
import { Env } from '@salesforce/kit';
import { ensureString, isString } from '@salesforce/ts-types';
Expand Down Expand Up @@ -222,9 +222,18 @@ const getEnvValue = (env: string, allowUnset = false): string =>
* Read the `replacement` property from sfdx-project.json
*/
const readReplacementsFromProject = async (projectDir?: string): Promise<ReplacementConfig[]> => {
const proj = await SfProject.resolve(projectDir);
const projJson = (await proj.resolveProjectConfig()) as { replacements?: ReplacementConfig[] };
return projJson.replacements ?? [];
try {
const proj = await SfProject.resolve(projectDir);
const projJson = (await proj.resolveProjectConfig()) as { replacements?: ReplacementConfig[] };

return projJson.replacements ?? [];
} catch (e) {
if (e instanceof SfError && e.name === 'InvalidProjectWorkspaceError') {
return [];
}

throw e;
}
};

/** escape any special characters used in the string so it can be used as a regex */
Expand Down
17 changes: 16 additions & 1 deletion test/convert/metadataConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,29 @@ describe('MetadataConverter', () => {

it('should convert to specified output dir', async () => {
const zipPath = outputDirectory + '.zip';
const testBuffer = Buffer.from('🥔');
$$.SANDBOX.stub(streams.ZipWriter.prototype, 'buffer').value(testBuffer);
await converter.convert(components, 'metadata', {
type: 'zip',
outputDirectory,
genUniqueDir: false,
});

expect(ensureDirectoryStub.calledBefore(pipelineStub)).to.be.true;
expect(ensureDirectoryStub.firstCall.args[0]).to.equal(dirname(zipPath));
expect(writeFileStub.firstCall.args[0]).to.equal(zipPath);
expect(writeFileStub.firstCall.args[1]).to.deep.equal(testBuffer);
});

it('should not return zipBuffer result when outputDirectory is specified', async () => {
const testBuffer = Buffer.from('🥔');
$$.SANDBOX.stub(streams.ZipWriter.prototype, 'buffer').value(testBuffer);
const result = await converter.convert(components, 'metadata', {
type: 'zip',
outputDirectory,
genUniqueDir: false,
});

expect(result.zipBuffer).to.be.undefined;
});

it('should return zipBuffer result for in-memory configuration', async () => {
Expand Down

0 comments on commit d947fc6

Please sign in to comment.