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: only scan pkgDirs when merging non-decomposed MD #666

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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 = [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the output dir passed to the converter in the conversion pipeline, if the dir isn't a package in a project SDR don't need to scan it.

new ComponentConverter(targetFormat, this.registry, mergeSet, defaultDirectory),

When running sfdx force:mdapi:convert -r mdapi -d src SDR will not look for non-decomposed metadata in /home/user/dreamhouse-lwc/src.

}

// 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