Skip to content

Commit

Permalink
fix: use import rather than fs read (#1359)
Browse files Browse the repository at this point in the history
* fix: presets are static imports

* chore: use import rather than fs read

* chore: reword

* chore: bump core & jsforce-node

* chore: update yarn lock

* chore: apply shane's suggestion

* chore: apply shane advice

* feat: export presets

* test: update test for presetMap's existence

---------

Co-authored-by: mshanemc <[email protected]>
  • Loading branch information
mingxuanzhangsfdx and mshanemc authored Jun 28, 2024
1 parent c6eff37 commit db46af6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,5 @@ export {
RecompositionStrategy,
TransformerStrategy,
} from './registry';

export { presetMap } from './registry/presets/presetMap';
20 changes: 20 additions & 0 deletions src/registry/presets/presetMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* 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 { MetadataRegistry } from '../types';

// we have to import all presets explicitly for VSCE's esbuild bundling process
import * as decomposeCustomLabelsBeta from './decomposeCustomLabelsBeta.json';
import * as decomposePermissionSetBeta from './decomposePermissionSetBeta.json';
import * as decomposeSharingRulesBeta from './decomposeSharingRulesBeta.json';
import * as decomposeWorkflowBeta from './decomposeWorkflowBeta.json';

export const presetMap = new Map<string, MetadataRegistry>([
['decomposeCustomLabelsBeta', decomposeCustomLabelsBeta as MetadataRegistry],
['decomposePermissionSetBeta', decomposePermissionSetBeta as MetadataRegistry],
['decomposeSharingRulesBeta', decomposeSharingRulesBeta as MetadataRegistry],
['decomposeWorkflowBeta', decomposeWorkflowBeta as MetadataRegistry],
]);
23 changes: 13 additions & 10 deletions src/registry/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* 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 { Logger, SfProject, SfProjectJson, Lifecycle, SfError } from '@salesforce/core';
import { deepFreeze } from '../utils/collections';
import { MetadataRegistry } from './types';
import * as registryData from './metadataRegistry.json';
import { presetMap } from './presets/presetMap';

export type RegistryLoadInput = {
/** The project directory to look at sfdx-project.json file
Expand Down Expand Up @@ -76,14 +75,18 @@ const maybeGetProject = (projectDir?: string): SfProjectJson | undefined => {
};

const loadPreset = (preset: string): MetadataRegistry => {
const pathToCheck = path.join(__dirname, 'presets', `${preset}.json`);

try {
const rawPreset = fs.readFileSync(pathToCheck, 'utf-8');
return JSON.parse(rawPreset) as MetadataRegistry;
} catch (e) {
throw new Error(`Failed to load preset ${preset} in ${pathToCheck}`);
const matchedPreset = presetMap.get(preset);
if (matchedPreset) {
return matchedPreset;
}
throw SfError.create({
message: `Failed to load preset "${preset}"`,
name: 'InvalidPreset',
actions: [
`Use a valid preset. Currently available presets are: [${[...presetMap.keys()].join(', ')}]`,
'Updating your CLI may be required to get newer presets',
],
});
};

const emptyRegistry = {
Expand Down
11 changes: 7 additions & 4 deletions test/registry/presetTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ type RegistryIterator = {
registry: MetadataRegistry;
};

const registriesFromPresets = fs.readdirSync(presetFolder, { withFileTypes: true }).map((file) => ({
name: file.name,
registry: JSON.parse(fs.readFileSync(path.join(file.path, file.name), 'utf-8')) as MetadataRegistry,
}));
const registriesFromPresets = fs
.readdirSync(presetFolder, { withFileTypes: true })
.filter((file) => file.name.endsWith('.json'))
.map((file) => ({
name: file.name,
registry: JSON.parse(fs.readFileSync(path.join(file.path, file.name), 'utf-8')) as MetadataRegistry,
}));

const allMerged = registriesFromPresets.reduce<MetadataRegistry>(
(acc, { registry }) => firstLevelMerge(acc, registry),
Expand Down
44 changes: 44 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,26 @@
strip-ansi "^6.0.0"
xml2js "^0.6.2"

"@jsforce/jsforce-node@^3.2.1":
version "3.2.1"
resolved "https://registry.yarnpkg.com/@jsforce/jsforce-node/-/jsforce-node-3.2.1.tgz#00fab05919e0cbe91ae4d873377e56cfbc087b98"
integrity sha512-hjmZQbYVikm6ATmaErOp5NaKR2VofNZsrcGGHrdbGA+bAgpfg/+MA/HzRTb8BvYyPDq3RRc5A8Yk8gx9Vtcrxg==
dependencies:
"@sindresorhus/is" "^4"
"@types/node" "^18.15.3"
abort-controller "^3.0.0"
base64url "^3.0.1"
csv-parse "^5.5.2"
csv-stringify "^6.4.4"
faye "^1.4.0"
form-data "^4.0.0"
fs-extra "^8.1.0"
https-proxy-agent "^5.0.0"
multistream "^3.1.0"
node-fetch "^2.6.1"
strip-ansi "^6.0.0"
xml2js "^0.6.2"

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -588,6 +608,30 @@
semver "^7.6.2"
ts-retry-promise "^0.8.1"

"@salesforce/core@^8.1.0":
version "8.1.0"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-8.1.0.tgz#8ee25acdacf9d70a6249907a2fe3503461f18766"
integrity sha512-oItr8cdeMe67glJN3dP1Gh/kasD0DUT6S6RfcLTH32wwuZNQAwMXNgBOCvlskr8nxPZ+YSSw7CVuqYMUmCtUXA==
dependencies:
"@jsforce/jsforce-node" "^3.2.1"
"@salesforce/kit" "^3.1.6"
"@salesforce/schemas" "^1.9.0"
"@salesforce/ts-types" "^2.0.10"
ajv "^8.16.0"
change-case "^4.1.2"
fast-levenshtein "^3.0.0"
faye "^1.4.0"
form-data "^4.0.0"
js2xmlparser "^4.0.1"
jsonwebtoken "9.0.2"
jszip "3.10.1"
pino "^9.2.0"
pino-abstract-transport "^1.2.0"
pino-pretty "^11.2.1"
proper-lockfile "^4.1.2"
semver "^7.6.2"
ts-retry-promise "^0.8.1"

"@salesforce/dev-config@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@salesforce/dev-config/-/dev-config-4.1.0.tgz#e529576466d074e7a5f1441236510fef123da01e"
Expand Down

0 comments on commit db46af6

Please sign in to comment.