From d947fc6c1e500f3688b8f0cf3a49e56da660afa0 Mon Sep 17 00:00:00 2001 From: Aaron Csetter <134731297+aaron-csetter@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:34:13 -0400 Subject: [PATCH] fix: metadataConverter writes zip to dir and works outside of project (#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 --- src/convert/metadataConverter.ts | 12 +++++++++--- src/convert/replacements.ts | 17 +++++++++++++---- test/convert/metadataConverter.test.ts | 17 ++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/convert/metadataConverter.ts b/src/convert/metadataConverter.ts index 6a27f2cfaa..c152860db0 100644 --- a/src/convert/metadataConverter.ts +++ b/src/convert/metadataConverter.ts @@ -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; diff --git a/src/convert/replacements.ts b/src/convert/replacements.ts index c199dd1e23..82da2d01d8 100644 --- a/src/convert/replacements.ts +++ b/src/convert/replacements.ts @@ -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'; @@ -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 => { - 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 */ diff --git a/test/convert/metadataConverter.test.ts b/test/convert/metadataConverter.test.ts index 99f7097f0b..cb13ba6ef2 100644 --- a/test/convert/metadataConverter.test.ts +++ b/test/convert/metadataConverter.test.ts @@ -290,6 +290,8 @@ 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, @@ -297,7 +299,20 @@ describe('MetadataConverter', () => { }); 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 () => {