From 5faca15bc4c73c05212b6b636c5c7a65776475b6 Mon Sep 17 00:00:00 2001 From: Aaron Csetter Date: Sun, 3 Mar 2024 16:53:15 -0500 Subject: [PATCH 1/4] fix: metadataConverter writes zip to dir and works outside of project --- src/collections/componentSet.ts | 2 +- src/convert/metadataConverter.ts | 13 ++++++++++--- src/convert/replacements.ts | 1 + test/collections/componentSet.test.ts | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/collections/componentSet.ts b/src/collections/componentSet.ts index 665974940c..6a1faa67ee 100644 --- a/src/collections/componentSet.ts +++ b/src/collections/componentSet.ts @@ -711,7 +711,7 @@ export class ComponentSet extends LazyCollection { try { version = `${await getCurrentApiVersion()}.0`; } catch (e) { - version = '58.0'; + version = '60.0'; this.logger.warn(messages.getMessage('missingApiVersion')); } } diff --git a/src/convert/metadataConverter.ts b/src/convert/metadataConverter.ts index 6a27f2cfaa..f3251fafdd 100644 --- a/src/convert/metadataConverter.ts +++ b/src/convert/metadataConverter.ts @@ -4,6 +4,7 @@ * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ +import * as fs from 'node:fs'; import { Readable, PassThrough } from 'node:stream'; import { dirname, join, normalize } from 'node:path'; import { Messages, SfError } from '@salesforce/core'; @@ -67,9 +68,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) { + fs.writeFileSync(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..bc7bb971c2 100644 --- a/src/convert/replacements.ts +++ b/src/convert/replacements.ts @@ -222,6 +222,7 @@ const getEnvValue = (env: string, allowUnset = false): string => * Read the `replacement` property from sfdx-project.json */ const readReplacementsFromProject = async (projectDir?: string): Promise => { + if (!projectDir) return []; // ? Allows tool to be used outside of a project without error. const proj = await SfProject.resolve(projectDir); const projJson = (await proj.resolveProjectConfig()) as { replacements?: ReplacementConfig[] }; return projJson.replacements ?? []; diff --git a/test/collections/componentSet.test.ts b/test/collections/componentSet.test.ts index 888c6e5128..fef6185f08 100644 --- a/test/collections/componentSet.test.ts +++ b/test/collections/componentSet.test.ts @@ -742,7 +742,7 @@ describe('ComponentSet', () => { }); it('should get an API version from sfdx-project.json', async () => { - const sourceApiVersion = '58.0'; + const sourceApiVersion = '60.0'; const set = ComponentSet.fromSource({ fsPaths: ['.'], registry: registryAccess, @@ -814,7 +814,7 @@ describe('ComponentSet', () => { members: ['b', 'c'], }, ], - version: '58.0', + version: '60.0', }, }); expect(getCurrentApiVersionStub.called).to.be.true; From d441b4250ef420ddfaac4dc79451d4012e0b4fd4 Mon Sep 17 00:00:00 2001 From: Aaron Csetter Date: Thu, 7 Mar 2024 13:25:06 -0500 Subject: [PATCH 2/4] fix: update fs import --- src/convert/metadataConverter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/convert/metadataConverter.ts b/src/convert/metadataConverter.ts index f3251fafdd..5ac974c2b7 100644 --- a/src/convert/metadataConverter.ts +++ b/src/convert/metadataConverter.ts @@ -4,7 +4,7 @@ * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ -import * as fs from 'node:fs'; +import { writeFileSync } from 'node:fs'; import { Readable, PassThrough } from 'node:stream'; import { dirname, join, normalize } from 'node:path'; import { Messages, SfError } from '@salesforce/core'; @@ -74,7 +74,7 @@ export class MetadataConverter { if (!packagePath) { result.zipBuffer = buffer; } else if (buffer) { - fs.writeFileSync(packagePath, buffer); + writeFileSync(packagePath, buffer); } } else { result.converted = (writer as StandardWriter).converted; From b2931c890d1ef4fe861b6cdad3cc92a9f6c4dd8f Mon Sep 17 00:00:00 2001 From: Aaron Csetter Date: Wed, 13 Mar 2024 11:48:13 -0400 Subject: [PATCH 3/4] fix: update write file call and unit-tests for metadataConverter --- src/collections/componentSet.ts | 2 +- src/convert/metadataConverter.ts | 3 +-- test/collections/componentSet.test.ts | 4 ++-- test/convert/metadataConverter.test.ts | 17 ++++++++++++++++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/collections/componentSet.ts b/src/collections/componentSet.ts index 6a1faa67ee..665974940c 100644 --- a/src/collections/componentSet.ts +++ b/src/collections/componentSet.ts @@ -711,7 +711,7 @@ export class ComponentSet extends LazyCollection { try { version = `${await getCurrentApiVersion()}.0`; } catch (e) { - version = '60.0'; + version = '58.0'; this.logger.warn(messages.getMessage('missingApiVersion')); } } diff --git a/src/convert/metadataConverter.ts b/src/convert/metadataConverter.ts index 5ac974c2b7..c152860db0 100644 --- a/src/convert/metadataConverter.ts +++ b/src/convert/metadataConverter.ts @@ -4,7 +4,6 @@ * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ -import { writeFileSync } from 'node:fs'; import { Readable, PassThrough } from 'node:stream'; import { dirname, join, normalize } from 'node:path'; import { Messages, SfError } from '@salesforce/core'; @@ -74,7 +73,7 @@ export class MetadataConverter { if (!packagePath) { result.zipBuffer = buffer; } else if (buffer) { - writeFileSync(packagePath, buffer); + await promises.writeFile(packagePath, buffer); } } else { result.converted = (writer as StandardWriter).converted; diff --git a/test/collections/componentSet.test.ts b/test/collections/componentSet.test.ts index fef6185f08..888c6e5128 100644 --- a/test/collections/componentSet.test.ts +++ b/test/collections/componentSet.test.ts @@ -742,7 +742,7 @@ describe('ComponentSet', () => { }); it('should get an API version from sfdx-project.json', async () => { - const sourceApiVersion = '60.0'; + const sourceApiVersion = '58.0'; const set = ComponentSet.fromSource({ fsPaths: ['.'], registry: registryAccess, @@ -814,7 +814,7 @@ describe('ComponentSet', () => { members: ['b', 'c'], }, ], - version: '60.0', + version: '58.0', }, }); expect(getCurrentApiVersionStub.called).to.be.true; diff --git a/test/convert/metadataConverter.test.ts b/test/convert/metadataConverter.test.ts index 878a39a497..4463d57bbc 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 () => { From 9f9112b9c6ff2fcb3b4d6c510f22cf6626f61d61 Mon Sep 17 00:00:00 2001 From: Aaron Csetter Date: Wed, 13 Mar 2024 15:58:19 -0400 Subject: [PATCH 4/4] fix: reading replacements from project ignores invalid workspace error --- src/convert/replacements.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/convert/replacements.ts b/src/convert/replacements.ts index bc7bb971c2..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,10 +222,18 @@ const getEnvValue = (env: string, allowUnset = false): string => * Read the `replacement` property from sfdx-project.json */ const readReplacementsFromProject = async (projectDir?: string): Promise => { - if (!projectDir) return []; // ? Allows tool to be used outside of a project without error. - 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 */