@salesforce/source-deploy-retrieve
Version:
JavaScript library to run Salesforce metadata deploys and retrieves
159 lines • 7.1 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.firstLevelMerge = exports.getEffectiveRegistry = void 0;
/*
* Copyright 2025, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const core_1 = require("@salesforce/core");
// The static import of json file should never be changed,
// other read methods might make esbuild fail to bundle the json file
const registryData = __importStar(require("./metadataRegistry.json"));
const presetMap_1 = require("./presets/presetMap");
/** combine the standard registration with any overrides specific in the sfdx-project.json */
const getEffectiveRegistry = (input) => removeEmptyStrings((0, exports.firstLevelMerge)(registryData, mergeVariants(input?.presets?.length ?? input?.registryCustomizations ? input : getProjectVariants(input?.projectDir))));
exports.getEffectiveRegistry = getEffectiveRegistry;
/** read the project to get additional registry customizations and sourceBehaviorOptions */
const getProjectVariants = (projectDir) => {
const logger = core_1.Logger.childFromRoot('variants:getProjectVariants');
const projJson = maybeGetProject(projectDir);
if (!projJson) {
logger.debug('no project found, using standard registry');
// there might not be a project at all and that's ok
return {};
}
// there might not be any customizations in a project, so we default to the emptyRegistry
const registryCustomizations = projJson.get('registryCustomizations') ?? emptyRegistry;
const presets = [
...new Set([
// TODO: deprecated, remove this
...(projJson.get('registryPresets') ?? []),
...(projJson.get('sourceBehaviorOptions') ?? []),
]),
];
return logProjectVariants({
registryCustomizations,
presets: presets.map(loadPreset),
}, projJson.getPath());
};
const mergeVariants = ({ registryCustomizations = emptyRegistry, presets }) => {
const registryFromPresets = [...(presets ?? []), registryCustomizations].reduce((prev, curr) => (0, exports.firstLevelMerge)(prev, curr), emptyRegistry);
return (0, exports.firstLevelMerge)(registryFromPresets, registryCustomizations);
};
const maybeGetProject = (projectDir) => {
try {
return core_1.SfProject.getInstance(projectDir ?? process.cwd()).getSfProjectJson();
}
catch (e) {
return undefined;
}
};
const loadPreset = (preset) => {
const matchedPreset = presetMap_1.presetMap.get(preset);
if (matchedPreset) {
return matchedPreset;
}
throw core_1.SfError.create({
message: `Failed to load preset "${preset}"`,
name: 'InvalidPreset',
actions: [
`Use a valid preset. Currently available presets are: [${[...presetMap_1.presetMap.keys()].join(', ')}]`,
'Updating your CLI may be required to get newer presets',
],
});
};
const emptyRegistry = {
types: {},
childTypes: {},
suffixes: {},
strictDirectoryNames: {},
};
/** merge the children of the top-level properties (ex: types, suffixes, etc) on 2 registries */
const firstLevelMerge = (original, overrides) => ({
types: { ...original.types, ...(overrides.types ?? {}) },
childTypes: { ...original.childTypes, ...(overrides.childTypes ?? {}) },
suffixes: { ...original.suffixes, ...(overrides.suffixes ?? {}) },
strictDirectoryNames: {
...original.strictDirectoryNames,
...(overrides.strictDirectoryNames ?? {}),
},
});
exports.firstLevelMerge = firstLevelMerge;
const removeEmptyStrings = (reg) => ({
types: reg.types,
childTypes: removeEmptyString(reg.childTypes),
suffixes: removeEmptyString(reg.suffixes),
strictDirectoryNames: removeEmptyString(reg.strictDirectoryNames),
});
// presets can remove an entry by setting it to an empty string ex: { "childTypes": { "foo": "" } }
const removeEmptyString = (obj) => Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== ''));
// returns the projectVariants passed in. Side effects: logger and telemetry only
const logProjectVariants = (variants, projectDir) => {
const customizationTypes = Object.keys(variants.registryCustomizations?.types ?? {});
const logger = core_1.Logger.childFromRoot('variants:logProjectVariants');
if (customizationTypes.length) {
logger.debug(`found registryCustomizations for types [${customizationTypes.join(',')}] in ${projectDir}`);
}
if (variants.presets?.length) {
logger.debug(`using sourceBehaviorOptions [${variants.presets.join(',')}] in ${projectDir}`);
}
if (variants?.presets?.length ?? customizationTypes.length) {
void core_1.Lifecycle.getInstance().emitTelemetry({
library: 'SDR',
eventName: 'RegistryVariants',
presetCount: variants.presets?.length ?? 0,
presets: variants.presets?.join(','),
customizationsCount: customizationTypes.length,
customizationsTypes: customizationTypes.join(','),
});
}
else {
logger.debug(`no registryCustomizations or sourceBehaviorOptions found in ${projectDir}`);
}
return variants;
};
//# sourceMappingURL=variants.js.map