autoforce
Version:
Developer Automation tool for Github / Gitlab and Salesforce projects.
127 lines (126 loc) • 4.17 kB
JavaScript
import sf from "./connect.js";
import { default as templateGenerator } from "./template.js";
import { DICTIONARY_FOLDER, getModelFolders } from "./util.js";
let _templateEngine;
function getTemplateEngine() {
if (!_templateEngine) {
_templateEngine = templateGenerator(getModelFolders('templates/dictionary'), "md");
}
return _templateEngine;
}
import { sortByLabel } from "./util.js";
async function getMetadata(objetos) {
try {
await sf.connect();
const objects = await sf.customObjects(objetos);
return Array.isArray(objects) ? objects : [objects];
}
catch (e) {
console.error(e);
}
return [];
}
function descriptionFormula() {
return this.description?.replaceAll(/[\n\r]/g, "<br/>");
}
function isManaged() {
return this.fullName.split("__").length == 3;
}
function isMetadataFormula() {
return this.fullName?.endsWith("__mdt") || this.customSettingsType;
}
function attributesFormula() {
const attributes = [];
// Object Attributes
if (this.enableHistory === "true") {
attributes.push(``);
}
// "enableActivities": "true",
// "enableBulkApi": "true",
// "enableChangeDataCapture": "false",
// "enableFeeds": "false",
// "enableHistory": "true",
// "enableReports": "true",
// "enableSearch": "false",
// "enableSharing": "true",
// "enableStreamingApi": "true",
// "externalSharingModel": "Private",
// Field Attributes
if (this.required === "true") {
attributes.push(``);
}
if (this.trackHistory === "true") {
attributes.push(``);
}
// if ( this.inlineHelpText ) {
// attributes.push( `` );
// }
if (this.externalId === "true") {
attributes.push(``);
}
if (this.encrypted === "true") {
attributes.push(``);
}
return attributes.join(" ");
}
function typeFormula() {
if (this.formula) {
return `Formula(${this.type})`;
}
if (this.type === "Lookup" || this.type === "MasterDetail") {
return `[Lookup a ${this.referenceTo}](/diccionarios/objects/${this.referenceTo})`;
}
if (this.length || this.precision) {
const longitud = (this.length || this.precision) +
(this.scale && this.scale > 0 ? "." + this.scale : "");
return `${this.type}(${longitud})`;
}
return this.type;
}
function getObjects(files) {
const items = new Set();
for (const file of files) {
let desde = file.indexOf("/objects/");
if (desde !== -1) {
desde += 9; // se desplaza hasta el proximo slash
const hasta = file.indexOf("/", desde + 1);
const objectName = file.substring(desde, hasta);
items.add(objectName);
}
}
return [...items.values()];
}
async function executeObjects(items, filename, folder) {
const templateEngine = getTemplateEngine();
if (items.length === 0) {
return;
}
// Busca la metadata
const contexts = await getMetadata(items);
if (!contexts || contexts.length === 0) {
return;
}
// Arma el diccionario de cada Objeto
templateEngine.read("object");
for (const context of contexts) {
if (context.fullName) {
templateEngine.render(context, {
helpers: { isManaged, descriptionFormula, typeFormula, attributesFormula }
});
templateEngine.save(context.fullName, DICTIONARY_FOLDER + "/objects");
}
}
// Arma el documento indice del grupo de objetos
contexts.sort(sortByLabel);
templateEngine.read("objects");
const objectContext = { objects: contexts };
templateEngine.render(objectContext, {
helpers: { isManaged, isMetadataFormula, attributesFormula }
});
templateEngine.save(filename, folder);
}
const objectModule = {
getItems: getObjects,
execute: executeObjects
};
export default objectModule;