Skip to content

Commit

Permalink
fix: only read multiple custom labels files once during conversion (#…
Browse files Browse the repository at this point in the history
…1368)

* fix: lint fixes

* fix: increase conversion time expectation to 10 seconds

* chore: update comment too

* chore: bump libs

* fix: better conditional
  • Loading branch information
shetzel authored Jul 16, 2024
1 parent b9ef881 commit d5606ba
Show file tree
Hide file tree
Showing 13 changed files with 69,083 additions and 2,224 deletions.
1,973 changes: 405 additions & 1,568 deletions CHANGELOG.md

Large diffs are not rendered by default.

1,268 changes: 633 additions & 635 deletions METADATA_SUPPORT.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 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.1",
"@salesforce/core": "^8.1.2",
"@salesforce/kit": "^3.1.6",
"@salesforce/ts-types": "^2.0.10",
"fast-levenshtein": "^3.0.0",
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.length &&
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

0 comments on commit d5606ba

Please sign in to comment.