Skip to content

Commit

Permalink
chore: use import rather than fs read
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxuanzhangsfdx committed Jun 28, 2024
1 parent 84a0630 commit c0d1b41
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/registry/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* 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 * as fs from 'node:fs';
import * as path from 'node:path';
import { Logger, SfProject, SfProjectJson, Lifecycle } from '@salesforce/core';
import { deepFreeze } from '../utils/collections';
import { MetadataRegistry } from './types';
import * as registryData from './metadataRegistry.json';
import decomposeCustomLabelsBeta from './presets/decomposeCustomLabelsBeta.json';
import decomposePermissionSetBeta from './presets/decomposePermissionSetBeta.json';
import decomposeSharingRulesBeta from './presets/decomposeSharingRulesBeta.json';
import decomposeWorkflowBeta from './presets/decomposeWorkflowBeta.json';

export type RegistryLoadInput = {
/** The project directory to look at sfdx-project.json file
Expand All @@ -23,6 +25,8 @@ export type RegistryLoadInput = {
export const getEffectiveRegistry = (input?: RegistryLoadInput): MetadataRegistry =>
deepFreeze(firstLevelMerge(registryData as MetadataRegistry, loadVariants(input)));

const presetsJsonMap: Map<string, string> = new Map();

/** read the project to get additional registry customizations and sourceBehaviorOptions */
const loadVariants = ({ projectDir }: RegistryLoadInput = {}): MetadataRegistry => {
const logger = Logger.childFromRoot('variants');
Expand All @@ -49,6 +53,7 @@ const loadVariants = ({ projectDir }: RegistryLoadInput = {}): MetadataRegistry
}
if (sourceBehaviorOptions.length > 0) {
logger.debug(`using sourceBehaviorOptions [${sourceBehaviorOptions.join(',')}] in ${projJson.getPath()}`);
loadPresetJson();
}
const registryFromPresets = sourceBehaviorOptions.reduce<MetadataRegistry>(
(prev, curr) => firstLevelMerge(prev, loadPreset(curr)),
Expand All @@ -75,14 +80,19 @@ const maybeGetProject = (projectDir?: string): SfProjectJson | undefined => {
}
};

const loadPreset = (preset: string): MetadataRegistry => {
const pathToCheck = path.join(__dirname, 'presets', `${preset}.json`);
const loadPresetJson = (): void => {
presetsJsonMap.set('decomposeCustomLabelsBeta', JSON.stringify(decomposeCustomLabelsBeta));
presetsJsonMap.set('decomposePermissionSetBeta', JSON.stringify(decomposePermissionSetBeta));
presetsJsonMap.set('decomposeSharingRulesBeta', JSON.stringify(decomposeSharingRulesBeta));
presetsJsonMap.set('decomposeWorkflowBeta', JSON.stringify(decomposeWorkflowBeta));
};

const loadPreset = (preset: string): MetadataRegistry => {
try {
const rawPreset = fs.readFileSync(pathToCheck, 'utf-8');
return JSON.parse(rawPreset) as MetadataRegistry;
const rawPreset = presetsJsonMap.get(preset);
return JSON.parse(rawPreset as string) as MetadataRegistry;
} catch (e) {
throw new Error(`Failed to load preset ${preset} in ${pathToCheck}`);
throw new Error(`Failed to load preset ${preset}. The value does not exist.`);
}
};

Expand Down

2 comments on commit c0d1b41

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: c0d1b41 Previous: 24f4f6f Ratio
eda-componentSetCreate-linux 180 ms 183 ms 0.98
eda-sourceToMdapi-linux 1968 ms 1937 ms 1.02
eda-sourceToZip-linux 1779 ms 1751 ms 1.02
eda-mdapiToSource-linux 2821 ms 2842 ms 0.99
lotsOfClasses-componentSetCreate-linux 357 ms 473 ms 0.75
lotsOfClasses-sourceToMdapi-linux 3697 ms 3697 ms 1
lotsOfClasses-sourceToZip-linux 3051 ms 3061 ms 1.00
lotsOfClasses-mdapiToSource-linux 3496 ms 3487 ms 1.00
lotsOfClassesOneDir-componentSetCreate-linux 625 ms 632 ms 0.99
lotsOfClassesOneDir-sourceToMdapi-linux 6528 ms 6546 ms 1.00
lotsOfClassesOneDir-sourceToZip-linux 5551 ms 5617 ms 0.99
lotsOfClassesOneDir-mdapiToSource-linux 6287 ms 6295 ms 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: c0d1b41 Previous: 24f4f6f Ratio
eda-componentSetCreate-win32 450 ms 416 ms 1.08
eda-sourceToMdapi-win32 3514 ms 3719 ms 0.94
eda-sourceToZip-win32 2607 ms 2878 ms 0.91
eda-mdapiToSource-win32 5556 ms 6068 ms 0.92
lotsOfClasses-componentSetCreate-win32 852 ms 874 ms 0.97
lotsOfClasses-sourceToMdapi-win32 7446 ms 7568 ms 0.98
lotsOfClasses-sourceToZip-win32 4661 ms 4847 ms 0.96
lotsOfClasses-mdapiToSource-win32 7525 ms 7614 ms 0.99
lotsOfClassesOneDir-componentSetCreate-win32 1482 ms 1505 ms 0.98
lotsOfClassesOneDir-sourceToMdapi-win32 13542 ms 13445 ms 1.01
lotsOfClassesOneDir-sourceToZip-win32 8693 ms 8815 ms 0.99
lotsOfClassesOneDir-mdapiToSource-win32 13523 ms 13516 ms 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.