UNPKG

@salesforce/core

Version:

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

104 lines 4.07 kB
"use strict"; /* * 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 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Aliases = exports.AliasGroup = void 0; const lib_1 = require("@salesforce/ts-types/lib"); const messages_1 = require("../messages"); const global_1 = require("../global"); const configGroup_1 = require("./configGroup"); messages_1.Messages.importMessagesDirectory(__dirname); const messages = messages_1.Messages.load('@salesforce/core', 'core', ['noAliasesFound', 'invalidFormat']); const ALIAS_FILE_NAME = 'alias.json'; /** * Different groups of aliases. Currently only support orgs. */ var AliasGroup; (function (AliasGroup) { AliasGroup["ORGS"] = "orgs"; })(AliasGroup = exports.AliasGroup || (exports.AliasGroup = {})); /** * Aliases specify alternate names for groups of properties used by the Salesforce CLI, such as orgs. * By default, all aliases are stored under 'orgs', but groups allow aliases to be applied for * other commands, settings, and parameters. * * **Note:** All aliases are stored at the global level. * * ``` * const aliases = await Aliases.create(); * aliases.set('myAlias', 'username@company.org'); * await aliases.write(); * // Shorthand to get an alias. * const username: string = await Aliases.fetch('myAlias'); * ``` * https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_cli_usernames_orgs.htm */ class Aliases extends configGroup_1.ConfigGroup { /** * The aliases state file filename. */ static getFileName() { return ALIAS_FILE_NAME; } /** * Get Aliases specific options. */ static getDefaultOptions() { const opts = configGroup_1.ConfigGroup.getOptions(AliasGroup.ORGS, Aliases.getFileName()); opts.stateFolder = global_1.Global.SFDX_STATE_FOLDER; return opts; } /** * Updates a group of aliases in a bulk save and returns the new aliases that were saved. * * ``` * const aliases = await Aliases.parseAndUpdate(['foo=bar', 'bar=baz']) * ``` * * @param aliasKeyAndValues An array of strings in the format `<alias>=<value>`. * Each element will be saved in the Aliases state file under the group. * @param group The group the alias belongs to. Defaults to ORGS. */ static async parseAndUpdate(aliasKeyAndValues, group = AliasGroup.ORGS) { const newAliases = {}; if (aliasKeyAndValues.length === 0) { throw messages.createError('noAliasesFound'); } for (const arg of aliasKeyAndValues) { const split = arg.split('='); if (split.length !== 2) { throw messages.createError('invalidFormat', [arg]); } const [name, value] = split; newAliases[name] = value || undefined; } const aliases = await Aliases.create(Aliases.getDefaultOptions()); return await aliases.updateValues(newAliases, group); } /** * Get an alias from a key and group. Shorthand for `Alias.create().get(key)`. Returns the promise resolved when the * alias is created. * * @param key The value of the alias to match. * @param group The group the alias belongs to. Defaults to Orgs. */ static async fetch(key, group = AliasGroup.ORGS) { const aliases = await Aliases.create(Aliases.getDefaultOptions()); return lib_1.asString(aliases.getInGroup(key, group)); } static get(key, group = AliasGroup.ORGS) { const aliases = new Aliases(Aliases.getDefaultOptions()); aliases.readSync(); return lib_1.asString(aliases.getInGroup(key, group)); } // Don't use kit's set to prevent nested object save setMethod(contents, key, value) { contents[key] = value; } } exports.Aliases = Aliases; //# sourceMappingURL=aliases.js.map