@jsforce/jsforce-node
Version:
Salesforce API Library for JavaScript
136 lines (135 loc) • 4.5 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SfdxRegistry = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const connection_1 = __importDefault(require("../connection"));
function isNotNullOrUndefined(v) {
return v != null;
}
/**
*
*/
class SfdxRegistry {
_cliPath;
_orgList;
_orgInfoMap = {};
_defaultOrgInfo;
constructor({ cliPath }) {
this._cliPath = cliPath;
}
_createCommand(command, options = {}, args = []) {
return `${this._cliPath ? this._cliPath + '/' : ''}sfdx ${command} ${Object.keys(options)
.map((option) => `${option.length > 1 ? '--' : '-'}${option}${options[option] != null ? ' ' + options[option] : ''}`)
.join(' ')} --json ${args.join(' ')}`;
}
async _execCommand(command, options = {}, args = []) {
const cmd = this._createCommand(command, options, args);
const buf = await new Promise((resolve, reject) => {
(0, child_process_1.exec)(cmd, (err, ret) => {
if (err && !ret) {
reject(err);
}
else {
resolve(ret);
}
});
});
const body = (0, util_1.stripVTControlCharacters)(buf.toString());
let ret;
try {
ret = JSON.parse(body);
}
catch (e) {
throw new Error(`Unexpectedd output from Sfdx cli: ${body}`);
}
if (ret.status === 0 && ret.result) {
return ret.result;
}
else {
const err = new Error(ret.message);
err.name = ret.name;
throw err;
}
}
async _getOrgList() {
if (!this._orgList) {
this._orgList = this._execCommand('force:org:list');
}
return this._orgList;
}
async getConnectionNames() {
const { nonScratchOrgs, scratchOrgs } = await this._getOrgList();
return [
...nonScratchOrgs.map((o) => o.alias).filter(isNotNullOrUndefined),
...scratchOrgs.map((o) => o.alias).filter(isNotNullOrUndefined),
...nonScratchOrgs.map((o) => o.username),
...scratchOrgs.map((o) => o.username),
];
}
async getConnection(name) {
const config = await this.getConnectionConfig(name);
return config ? new connection_1.default(config) : null;
}
async _getOrgInfo(username) {
const options = username ? { u: username } : {};
if (!username || !this._orgInfoMap[username]) {
const pOrgInfo = this._execCommand('force:org:display', options);
this._memoOrgInfo(pOrgInfo, username);
}
const orgInfo = username
? this._orgInfoMap[username]
: this._defaultOrgInfo;
if (!orgInfo) {
throw new Error('no orginfo found');
}
return orgInfo;
}
_memoOrgInfo(pOrgInfo, username) {
const pOrgInfo_ = pOrgInfo.then((orgInfo) => {
this._orgInfoMap[orgInfo.username] = pOrgInfo_;
if (orgInfo.alias) {
this._orgInfoMap[orgInfo.alias] = pOrgInfo_;
}
return orgInfo;
});
if (username) {
this._orgInfoMap[username] = pOrgInfo_;
}
else {
this._defaultOrgInfo = pOrgInfo_;
}
}
async getConnectionConfig(name) {
const orgInfo = await this._getOrgInfo(name);
if (!orgInfo) {
return null;
}
const { accessToken, instanceUrl, loginUrl } = orgInfo;
return { accessToken, instanceUrl, loginUrl };
}
async saveConnectionConfig(_name, _connConfig) {
// nothing to do
}
async setDefaultConnection(_name) {
// nothing to do
}
async removeConnectionConfig(name) {
await this._execCommand('force:org:delete', { u: name });
}
// eslint-disable-next-line @typescript-eslint/require-await
async getClientConfig(_name) {
return null;
}
// eslint-disable-next-line @typescript-eslint/require-await
async getClientNames() {
return [];
}
async registerClientConfig(_name, _clientConfig) {
// nothing to do
}
}
exports.SfdxRegistry = SfdxRegistry;
;