UNPKG

sfdx-cpq-scripts-deployment

Version:
100 lines 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const command_1 = require("@salesforce/command"); const core_1 = require("@salesforce/core"); const fs_1 = require("fs"); const path_1 = require("path"); const utils_1 = require("../../utils"); core_1.Messages.importMessagesDirectory(__dirname); const messages = core_1.Messages.loadMessages("sfdx-cpq-scripts-deployment", "deploy"); class Deploy extends command_1.SfdxCommand { async run() { const scriptNames = this.flags.scripts .split(",") .map((scriptName) => scriptName.trim()); this.ux.startSpinner("Deploying CPQ scripts", "fetching existing CPQ scripts"); const scriptNameToId = await this.getExistingScriptsIds(scriptNames); const customScripts = await Promise.all(scriptNames.map((scriptName) => this.prepareCustomScriptSobject(scriptName, scriptNameToId.get(scriptName)))); const scriptsToUpdate = customScripts.filter((script) => script.Id != null); const dmlPromises = []; if (scriptsToUpdate.length > 0) { const updatePromise = this.org .getConnection() .sobject("SBQQ__CustomScript__c") .update(scriptsToUpdate, { allOrNone: true }); dmlPromises.push(updatePromise); } const scriptsToInsert = customScripts.filter((script) => script.Id == null); if (scriptsToInsert.length > 0) { const insertPromise = this.org .getConnection() .sobject("SBQQ__CustomScript__c") .insert(scriptsToInsert); dmlPromises.push(insertPromise); } this.ux.setSpinnerStatus("Upserting records"); return Promise.all(dmlPromises); } async prepareCustomScriptSobject(scriptName, id) { var _a, _b, _c, _d, _e; const result = await Promise.all([ this.readScriptConfig(scriptName), this.readScriptContent(scriptName), ]); const [scriptConfig, content] = result; return { Name: scriptName, Id: id, SBQQ__Code__c: content, SBQQ__ConsumptionRateFields__c: (_a = scriptConfig.consumptionRateFields) === null || _a === void 0 ? void 0 : _a.join("\n"), SBQQ__ConsumptionScheduleFields__c: (_b = scriptConfig.consumptionScheduleFields) === null || _b === void 0 ? void 0 : _b.join("\n"), SBQQ__GroupFields__c: (_c = scriptConfig.quoteLineGroupFields) === null || _c === void 0 ? void 0 : _c.join("\n"), SBQQ__QuoteFields__c: (_d = scriptConfig.quoteFields) === null || _d === void 0 ? void 0 : _d.join("\n"), SBQQ__QuoteLineFields__c: (_e = scriptConfig.quoteLineFields) === null || _e === void 0 ? void 0 : _e.join("\n"), }; } async readScriptContent(scriptName) { const path = (0, path_1.join)(this.project.getPath(), utils_1.CPQ_SCRIPTS_FOLDER_NAME, scriptName, "index.js"); return fs_1.promises.readFile(path, "utf-8"); } async readScriptConfig(scriptName) { const path = (0, path_1.join)(this.project.getPath(), utils_1.CPQ_SCRIPTS_FOLDER_NAME, scriptName, "package.json"); return fs_1.promises .readFile(path, "utf-8") .then((content) => { console.log(content); return JSON.parse(content); }) .then((packageJson) => packageJson.cpqScriptConfig); } async getExistingScriptsIds(scriptNames) { const scriptNameToId = new Map(); return this.org .getConnection() .sobject("SBQQ__CustomScript__c") .find({ Name: { $in: scriptNames } }, { Id: 1, Name: 1 }) .then((scripts) => { for (const script of scripts) { // @ts-ignore field Name is not typed scriptNameToId.set(script.Name, script.Id); } return scriptNameToId; }); } } exports.default = Deploy; Deploy.description = messages.getMessage("description"); Deploy.requiresUsername = true; Deploy.flagsConfig = { all: command_1.flags.boolean({ description: messages.getMessage("flag:all"), char: "a", }), scripts: command_1.flags.string({ description: messages.getMessage("flag:scripts"), char: "s", required: true, }), }; Deploy.requiresProject = true; //# sourceMappingURL=deploy.js.map