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 read multiple custom labels files once during conversion #1368

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@salesforce/core": "^8.1.0",
"@salesforce/core": "^8.1.2",
"@salesforce/kit": "^3.1.6",
"@salesforce/ts-types": "^2.0.10",
"fast-levenshtein": "^3.0.0",
Expand All @@ -40,7 +40,7 @@
},
"devDependencies": {
"@jsforce/jsforce-node": "^3.2.1",
"@salesforce/cli-plugins-testkit": "^5.3.16",
"@salesforce/cli-plugins-testkit": "^5.3.18",
"@salesforce/dev-scripts": "^10.2.2",
"@types/deep-equal-in-any-order": "^1.0.1",
"@types/fast-levenshtein": "^0.0.4",
Expand Down
25 changes: 23 additions & 2 deletions src/convert/convertContext/recompositionFinalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,31 @@ type ChildWithXml = {
const recompose =
(cache: XmlCache) =>
async (stateValue: RecompositionStateValueWithParent): Promise<JsonMap> => {
await getXmlFromCache(cache)(stateValue.component);
const childComponents = stateValue.children?.toArray() ?? [];

// RecompositionState combines all labels metadata files into 1 component containing
// all the children. This checks for multiple parent components and gets the xml
// file content from each.
if (
childComponents &&
mshanemc marked this conversation as resolved.
Show resolved Hide resolved
stateValue.component.type.strategies?.recomposition === 'startEmpty' &&
stateValue.component.type.strategies?.transformer === 'nonDecomposed'
) {
const parentLabelNames: string[] = [];
for (const childComp of childComponents) {
const parentComp = childComp.parent as SourceComponent;
if (parentComp && !parentLabelNames.includes(parentComp.name)) {
parentLabelNames.push(parentComp.name);
// eslint-disable-next-line no-await-in-loop
await getXmlFromCache(cache)(parentComp);
}
}
} else {
await getXmlFromCache(cache)(stateValue.component);
}

const childXmls = await Promise.all(
(stateValue.children?.toArray() ?? []).filter(ensureMetadataComponentWithParent).map(
childComponents.filter(ensureMetadataComponentWithParent).map(
async (child): Promise<ChildWithXml> => ({
cmp: child,
xmlContents: await getXmlFromCache(cache)(child),
Expand Down
10 changes: 5 additions & 5 deletions test/convert/convertContext/recomposition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ describe('Recomposition', () => {
expect(readFileSpy.callCount, JSON.stringify(readFileSpy.getCalls(), undefined, 2)).to.equal(1);
});

describe('should only read unique child xml files once for non-decomposed components', () => {
describe('should only read unique child xml files once per parent for non-decomposed components', () => {
// This test sets up 2 CustomLabels files; 1 in each package directory. The CustomLabels files
// each have 2 labels within them. This should result in only 2 file reads.
// each have 2 labels within them. This should result in only 2 file reads; 1 per parent CustomLabels file.
const customLabelsType = new RegistryAccess().getTypeByName('CustomLabels');
const labelsFileName = 'CustomLabels.labels-meta.xml';
const projectDir = join(process.cwd(), 'my-project');
Expand Down Expand Up @@ -210,7 +210,7 @@ describe('Recomposition', () => {
new VirtualTreeContainer(vDir)
);

it('one item in transaction state covering all the children', async () => {
it('one main component with multiple parents in transaction state covering all the children', async () => {
const context = new ConvertContext();
const compSet = new ComponentSet();
component.getChildren().forEach((child) => compSet.add(child));
Expand All @@ -220,11 +220,11 @@ describe('Recomposition', () => {
children: compSet,
});

const readFileSpy = env.spy(component.tree, 'readFile');
const readFileSpy = env.spy(VirtualTreeContainer.prototype, 'readFile');

await context.recomposition.finalize();

expect(readFileSpy.callCount).to.equal(context.recomposition.transactionState.size);
expect(readFileSpy.callCount, 'readFile() should only be called twice').to.equal(2);
});
});
});
Expand Down
Loading
Loading