sfdx-hardis
Version:
Swiss-army-knife Toolbox for Salesforce. Allows you to define a complete CD/CD Pipeline. Orchestrate base commands and assist users with interactive wizards
116 lines • 5.08 kB
JavaScript
import c from "chalk";
import * as he from "he";
import * as path from "path";
import { getConfig } from "../../config/index.js";
import { PACKAGE_ROOT_DIR } from "../../settings.js";
import { uxLog } from "../utils/index.js";
import { soqlQuery } from "../utils/apiUtils.js";
import { deployMetadatas } from "../utils/deployUtils.js";
import { setPoolStorage } from "../utils/poolUtils.js";
export class SalesforceProvider {
name = "salesforce";
description = "Use a custom object on a Salesforce org (usually DevHub) to store scratch org pool tech info";
conn = null;
recordName = null;
async initialize(options) {
await this.manageSfdcOrgAuth(options);
return this.conn !== null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getValue(_key = null) {
if (!this.conn) {
return "ERROR";
}
await this.manageSfdcOrgAuth();
// Single record upsert
const queryRes = await soqlQuery(`SELECT Id,Name,ValueText__c FROM SfdxHardisKeyValueStore__c WHERE Name='${this.recordName}' LIMIT 1`, this.conn);
const valueText = queryRes.records[0] ? queryRes.records[0].ValueText__c || "" : "";
if (valueText.length > 5) {
return valueText.includes(""") ? JSON.parse(he.decode(valueText)) : JSON.parse(valueText);
}
return {};
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async setValue(_key = null, value) {
await this.manageSfdcOrgAuth();
if (!this.conn) {
return false;
}
// Single record upsert
const queryRes = await soqlQuery(`SELECT Id,Name,ValueText__c FROM SfdxHardisKeyValueStore__c WHERE Name='${this.recordName}' LIMIT 1`, this.conn);
if (queryRes.records[0]) {
const recordId = queryRes.records[0].Id;
const queryUpdateRes = await this.conn.sobject("SfdxHardisKeyValueStore__c").update({
Id: recordId,
Name: this.recordName,
ValueText__c: JSON.stringify(value),
});
return queryUpdateRes.success === true;
}
else {
const queryCreateRes = await this.conn.sobject("SfdxHardisKeyValueStore__c").create({
Name: this.recordName,
ValueText__c: JSON.stringify(value),
});
return queryCreateRes.success === true;
}
}
async updateActiveScratchOrg(scratchOrg, keyValues) {
if (!this.conn) {
return;
}
const orgId = scratchOrg?.scratchOrgInfo?.orgId ? scratchOrg.scratchOrgInfo.orgId.slice(0, 15) : scratchOrg.Id.slice(0, 15);
const activeScratchOrg = await this.conn.sobject("ActiveScratchOrg").findOne({ ScratchOrg: orgId }, { Id: true, Description: true });
keyValues.Id = activeScratchOrg.Id;
if (keyValues.Description) {
keyValues.Description = (activeScratchOrg.Description ? activeScratchOrg.Description + "\n" : "") + keyValues.Description;
}
await this.conn.sobject("ActiveScratchOrg").update(keyValues);
}
async manageSfdcOrgAuth(options = {}) {
const config = await getConfig("project");
if (this.conn == null) {
if (options.devHubConn) {
this.conn = options.devHubConn;
}
const projectName = config.projectName || "default";
this.recordName = `ScratchOrgPool_${projectName}`;
}
}
async userSetup(options) {
// Deploy KeyValueStore object on DevHub org
try {
await deployMetadatas({
deployDir: path.join(path.join(PACKAGE_ROOT_DIR, "defaults/utils/sfdxHardisKeyValueStore", ".")),
targetUsername: options.devHubConn.options.authInfo.username,
});
}
catch (e) {
uxLog(this, c.red(`Unable to deploy CustomObject SfdxHardisKeyValueStore__c
You mut create manually an Custom Object SfdxHardisKeyValueStore__c:
- API Name: SfdxHardisKeyValueStore__c
- Field SfdxHardisKeyValueStore__c.ValueText__c of type TextArea (long) (with maximum size 131072 chars)
`));
uxLog(this, c.yellow("You may have to create a Permission Set with all rights on SfdxHardisKeyValueStore__c and assign users to it"));
throw e;
}
// Initialize storage
try {
await setPoolStorage({}, options);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (e) {
uxLog(this, c.yellow("You may have to create a Permission Set with all rights on SfdxHardisKeyValueStore__c and assign users to it"));
}
uxLog(this, c.green("Created KeyValue storage on Salesforce org"));
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async userAuthenticate(options) {
if (options.devHubConn) {
return true;
}
return false;
}
}
//# sourceMappingURL=salesforce.js.map