Skip to content

Commit

Permalink
fix: only scan pkgDirs when merging non-decomposed MD (#666)
Browse files Browse the repository at this point in the history
Co-authored-by: Willie Ruemmele <[email protected]>
  • Loading branch information
cristiand391 and WillieRuemmele authored Jul 25, 2022
1 parent e64bd58 commit 098875f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/convert/convertContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* 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 { dirname, join, resolve } from 'path';
import { join } from 'path';
import { getString, JsonArray, JsonMap } from '@salesforce/ts-types';
import { SfProject } from '@salesforce/core';
import { META_XML_SUFFIX, XML_NS_KEY, XML_NS_URL } from '../common';
import { ComponentSet } from '../collections';
import { normalizeToArray } from '../utils';
Expand Down Expand Up @@ -193,12 +194,21 @@ class NonDecompositionFinalizer extends ConvertTransactionFinalizer<NonDecomposi
return writerData;
}
this.tree = tree;

const packageDirectories = SfProject.getInstance().getPackageDirectories();
const pkgPaths = packageDirectories.map((pkg) => pkg.fullPath);

// nondecomposed metadata types can exist in multiple locations under the same name
// so we have to find all components that could potentially match inbound components
const allNonDecomposed = this.getAllComponentsOfType(
defaultDirectory,
this.transactionState.exampleComponent.type.name
);
let allNonDecomposed: SourceComponent[];

if (pkgPaths.includes(defaultDirectory)) {
allNonDecomposed = this.getAllComponentsOfType(pkgPaths, this.transactionState.exampleComponent.type.name);
} else {
// defaultDirectory isn't a package, assumes it's the target output dir for conversion
// so no need to scan this folder
allNonDecomposed = [];
}

// prepare 3 maps to simplify component merging
await this.initMergeMap(allNonDecomposed);
Expand Down Expand Up @@ -271,11 +281,9 @@ class NonDecompositionFinalizer extends ConvertTransactionFinalizer<NonDecomposi
* child type before recomposing the final xml.
* The labels could belong in any of the files OR need to go in the default location which already contains labels
*/
private getAllComponentsOfType(defaultDirectory: string, componentType: string): SourceComponent[] {
// assumes that defaultDir is one level below project dir
const projectDir = resolve(dirname(defaultDirectory));
private getAllComponentsOfType(pkgDirs: string[], componentType: string): SourceComponent[] {
const unprocessedComponents = ComponentSet.fromSource({
fsPaths: [projectDir],
fsPaths: pkgDirs,
include: new ComponentSet([{ fullName: '*', type: componentType }]),
tree: this.tree,
}).getSourceComponents();
Expand Down
34 changes: 33 additions & 1 deletion test/convert/convertContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { Readable } from 'stream';
import { join } from 'path';

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

describe('NonDecomposition', () => {
let sfProjectStub: sinon.SinonStub;
beforeEach(() => {
sfProjectStub = env.stub(SfProject, 'getInstance').returns({
getPackageDirectories: () => {
return [
{
name: 'force-app',
path: 'force-app',
fullPath: nonDecomposed.DEFAULT_DIR,
},
];
},
} as unknown as SfProject);
});
it('should return WriterFormats for claimed children', async () => {
const component = nonDecomposed.COMPONENT_1;
const context = new ConvertContext();
Expand Down Expand Up @@ -394,11 +409,28 @@ describe('Convert Transaction Constructs', () => {
});

it('should merge 1 updated file to non-default dir and not write default file', async () => {
sfProjectStub.restore();
env.stub(SfProject, 'getInstance').returns({
getPackageDirectories: () => {
return [
{
name: 'my-app',
path: 'my-app',
fullPath: nonDecomposed.NON_DEFAULT_DIR,
},
{
name: 'force-app',
path: 'force-app',
fullPath: nonDecomposed.DEFAULT_DIR,
},
];
},
} as unknown as SfProject);
const component = nonDecomposed.COMPONENT_2;
const context = new ConvertContext();
const type = component.type;

// change the word first to 'updated'
// change the word 'third' to 'updated'
const updatedChild3Xml = {
...nonDecomposed.CHILD_3_XML,
value: nonDecomposed.CHILD_3_XML.value.replace('third', 'updated'),
Expand Down

0 comments on commit 098875f

Please sign in to comment.