Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 13, 2024
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
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
Loading