Skip to content

Commit 098875f

Browse files
fix: only scan pkgDirs when merging non-decomposed MD (#666)
Co-authored-by: Willie Ruemmele <[email protected]>
1 parent e64bd58 commit 098875f

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/convert/convertContext.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { dirname, join, resolve } from 'path';
7+
import { join } from 'path';
88
import { getString, JsonArray, JsonMap } from '@salesforce/ts-types';
9+
import { SfProject } from '@salesforce/core';
910
import { META_XML_SUFFIX, XML_NS_KEY, XML_NS_URL } from '../common';
1011
import { ComponentSet } from '../collections';
1112
import { normalizeToArray } from '../utils';
@@ -193,12 +194,21 @@ class NonDecompositionFinalizer extends ConvertTransactionFinalizer<NonDecomposi
193194
return writerData;
194195
}
195196
this.tree = tree;
197+
198+
const packageDirectories = SfProject.getInstance().getPackageDirectories();
199+
const pkgPaths = packageDirectories.map((pkg) => pkg.fullPath);
200+
196201
// nondecomposed metadata types can exist in multiple locations under the same name
197202
// so we have to find all components that could potentially match inbound components
198-
const allNonDecomposed = this.getAllComponentsOfType(
199-
defaultDirectory,
200-
this.transactionState.exampleComponent.type.name
201-
);
203+
let allNonDecomposed: SourceComponent[];
204+
205+
if (pkgPaths.includes(defaultDirectory)) {
206+
allNonDecomposed = this.getAllComponentsOfType(pkgPaths, this.transactionState.exampleComponent.type.name);
207+
} else {
208+
// defaultDirectory isn't a package, assumes it's the target output dir for conversion
209+
// so no need to scan this folder
210+
allNonDecomposed = [];
211+
}
202212

203213
// prepare 3 maps to simplify component merging
204214
await this.initMergeMap(allNonDecomposed);
@@ -271,11 +281,9 @@ class NonDecompositionFinalizer extends ConvertTransactionFinalizer<NonDecomposi
271281
* child type before recomposing the final xml.
272282
* The labels could belong in any of the files OR need to go in the default location which already contains labels
273283
*/
274-
private getAllComponentsOfType(defaultDirectory: string, componentType: string): SourceComponent[] {
275-
// assumes that defaultDir is one level below project dir
276-
const projectDir = resolve(dirname(defaultDirectory));
284+
private getAllComponentsOfType(pkgDirs: string[], componentType: string): SourceComponent[] {
277285
const unprocessedComponents = ComponentSet.fromSource({
278-
fsPaths: [projectDir],
286+
fsPaths: pkgDirs,
279287
include: new ComponentSet([{ fullName: '*', type: componentType }]),
280288
tree: this.tree,
281289
}).getSourceComponents();

test/convert/convertContext.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { Readable } from 'stream';
99
import { join } from 'path';
1010

11+
import { SfProject } from '@salesforce/core';
1112
import { createSandbox } from 'sinon';
1213
import chai = require('chai');
1314
import deepEqualInAnyOrder = require('deep-equal-in-any-order');
@@ -281,6 +282,20 @@ describe('Convert Transaction Constructs', () => {
281282
});
282283

283284
describe('NonDecomposition', () => {
285+
let sfProjectStub: sinon.SinonStub;
286+
beforeEach(() => {
287+
sfProjectStub = env.stub(SfProject, 'getInstance').returns({
288+
getPackageDirectories: () => {
289+
return [
290+
{
291+
name: 'force-app',
292+
path: 'force-app',
293+
fullPath: nonDecomposed.DEFAULT_DIR,
294+
},
295+
];
296+
},
297+
} as unknown as SfProject);
298+
});
284299
it('should return WriterFormats for claimed children', async () => {
285300
const component = nonDecomposed.COMPONENT_1;
286301
const context = new ConvertContext();
@@ -394,11 +409,28 @@ describe('Convert Transaction Constructs', () => {
394409
});
395410

396411
it('should merge 1 updated file to non-default dir and not write default file', async () => {
412+
sfProjectStub.restore();
413+
env.stub(SfProject, 'getInstance').returns({
414+
getPackageDirectories: () => {
415+
return [
416+
{
417+
name: 'my-app',
418+
path: 'my-app',
419+
fullPath: nonDecomposed.NON_DEFAULT_DIR,
420+
},
421+
{
422+
name: 'force-app',
423+
path: 'force-app',
424+
fullPath: nonDecomposed.DEFAULT_DIR,
425+
},
426+
];
427+
},
428+
} as unknown as SfProject);
397429
const component = nonDecomposed.COMPONENT_2;
398430
const context = new ConvertContext();
399431
const type = component.type;
400432

401-
// change the word first to 'updated'
433+
// change the word 'third' to 'updated'
402434
const updatedChild3Xml = {
403435
...nonDecomposed.CHILD_3_XML,
404436
value: nonDecomposed.CHILD_3_XML.value.replace('third', 'updated'),

0 commit comments

Comments
 (0)