UNPKG

@salesforce/core

Version:

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

105 lines 4.04 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 sfdxError_1 = require("../sfdxError"); const configGroup_1 = require("./configGroup"); 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 * * @deprecated Replaced by GlobalInfo in v3 {@link https://github.com/forcedotcom/sfdx-core/blob/v3/MIGRATING_V2-V3.md#globalinfo} */ class Aliases extends configGroup_1.ConfigGroup { /** * Constructor * **Do not directly construct instances of this class -- use {@link Aliases.create} instead.** * * @param options The options for the class instance */ constructor(options) { super(options); } /** * The aliases state file filename. */ static getFileName() { return ALIAS_FILE_NAME; } /** * Get Aliases specific options. */ static getDefaultOptions() { return configGroup_1.ConfigGroup.getOptions(AliasGroup.ORGS, Aliases.getFileName()); } /** * 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 sfdxError_1.SfdxError.create('@salesforce/core', 'core', 'NoAliasesFound', []); } for (const arg of aliasKeyAndValues) { const split = arg.split('='); if (split.length !== 2) { throw sfdxError_1.SfdxError.create('@salesforce/core', 'core', '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)); } // 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