UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

142 lines 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GlobalInfo = exports.deepCopy = exports.SfInfoKeys = void 0; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ const kit_1 = require("@salesforce/kit"); const ts_types_1 = require("@salesforce/ts-types"); const global_1 = require("../global"); const configFile_1 = require("./configFile"); const sfdxDataHandler_1 = require("./sfdxDataHandler"); var SfInfoKeys; (function (SfInfoKeys) { SfInfoKeys["ORGS"] = "orgs"; SfInfoKeys["TOKENS"] = "tokens"; })(SfInfoKeys = exports.SfInfoKeys || (exports.SfInfoKeys = {})); function deepCopy(data) { return JSON.parse(JSON.stringify(data)); } exports.deepCopy = deepCopy; class GlobalInfo extends configFile_1.ConfigFile { constructor() { super(...arguments); this.sfdxHandler = new sfdxDataHandler_1.SfdxDataHandler(); } static get emptyDataModel() { return deepCopy(GlobalInfo.EMPTY_DATA_MODEL); } static async getInstance() { if (!GlobalInfo.instance) { GlobalInfo.instance = await GlobalInfo.create(); } return GlobalInfo.instance; } /** * Clear the cache to force reading from disk. * * *NOTE: Only call this method if you must and you know what you are doing.* */ static clearInstance() { delete GlobalInfo.instance; } static getFileName() { return 'sf.json'; } /** * Gets default options for the SfConfig */ static getDefaultOptions() { return { isGlobal: true, isState: true, filename: GlobalInfo.getFileName(), stateFolder: global_1.Global.SF_STATE_FOLDER, }; } getOrgs(decrypt = false) { return this.get(SfInfoKeys.ORGS, decrypt); } getOrg(username, decrypt = false) { const auth = this.get(`${SfInfoKeys.ORGS}["${username}"]`, decrypt); // For legacy, some things wants the username in the returned auth info. if (auth && !auth.username) auth.username = username; return auth; } hasOrg(username) { return !!this.getOrgs()[username]; } setOrg(username, org) { // For legacy, and to keep things standard, some things wants the username in auth info. if (!org.username) org.username = username; this.set(`${SfInfoKeys.ORGS}["${username}"]`, org); } updateOrg(username, authorization) { // For legacy, and to keep things standard, some things wants the username in auth info. if (!authorization.username) authorization.username = username; this.update(`${SfInfoKeys.ORGS}["${username}"]`, authorization); } unsetOrg(username) { delete this.get(SfInfoKeys.ORGS)[username]; } getTokens(decrypt = false) { return this.get(SfInfoKeys.TOKENS, decrypt) || {}; } getToken(name, decrypt = false) { return this.get(`${SfInfoKeys.TOKENS}["${name}"]`, decrypt); } hasToken(name) { return !!this.getTokens()[name]; } setToken(name, token) { this.set(`${SfInfoKeys.TOKENS}["${name}"]`, token); } updateToken(name, token) { this.update(`${SfInfoKeys.TOKENS}["${name}"]`, token); } unsetToken(name) { delete this.get(SfInfoKeys.TOKENS)[name]; } set(key, value) { if (ts_types_1.isPlainObject(value)) { value = this.timestamp(value); } super.set(key, value); } async write(newContents) { const result = await super.write(newContents); if (global_1.Global.SFDX_INTEROPERABILITY) await this.sfdxHandler.write(result); return result; } async init() { await this.initCrypto(); const contents = global_1.Global.SFDX_INTEROPERABILITY ? await this.mergeWithSfdxData() : await this.loadSfData(); this.setContents(contents); } timestamp(data) { return Object.assign(data, { timestamp: new Date() }); } async loadSfData() { const data = await this.read(); return kit_1.isEmpty(data) ? GlobalInfo.emptyDataModel : data; } async mergeWithSfdxData() { const sfData = await this.loadSfData(); const merged = await this.sfdxHandler.merge(sfData); return merged; } } exports.GlobalInfo = GlobalInfo; GlobalInfo.encryptedKeys = [/token/gi, /password/gi, /secret/gi]; GlobalInfo.EMPTY_DATA_MODEL = { [SfInfoKeys.ORGS]: {}, [SfInfoKeys.TOKENS]: {}, }; //# sourceMappingURL=globalInfoConfig.js.map