UNPKG

salesforce-alm

Version:

This package contains tools, and APIs, for an improved salesforce.com developer experience.

148 lines (146 loc) 8.11 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.OrgCloneCommand = void 0; const command_1 = require("@salesforce/command"); const core_1 = require("@salesforce/core"); const kit_1 = require("@salesforce/kit"); const consts = require("../../../lib/core/constants"); const SFDXCommonMessages = require("../../../lib/messages"); const sandboxConstants_1 = require("../../../lib/org/sandbox/sandboxConstants"); const sandboxOrg_1 = require("../../../lib/org/sandbox/sandboxOrg"); const sandboxOrgApi_1 = require("../../../lib/org/sandbox/sandboxOrgApi"); const sandboxProgressReporter_1 = require("../../../lib/org/sandbox/sandboxProgressReporter"); const orgTypes_1 = require("../../../lib/orgTypes"); core_1.Messages.importMessagesDirectory(__dirname); const messages = core_1.Messages.loadMessages('salesforce-alm', 'org_clone'); const sfdxCommonMessages = SFDXCommonMessages(); class OrgCloneCommand extends command_1.SfdxCommand { async readJsonDefFile() { // the -f option if (this.flags.definitionfile) { this.logger.debug('Reading JSON DefFile %s ', this.flags.definitionfile); return core_1.fs.readJson(this.flags.definitionfile); } return; } async run() { const alias = await core_1.Aliases.create({}); this.logger.debug('Clone started with args %s ', this.flags); const defFileContents = await this.readJsonDefFile(); if (this.flags.type === orgTypes_1.OrgTypes.Sandbox) { const sandboxOrg = sandboxOrg_1.SandboxOrg.getInstance(this.org, this.flags.wait, this.logger, this.flags.clientid); // Keep all console output in the command sandboxOrg.on(sandboxConstants_1.SandboxEventNames.EVENT_ASYNCRESULT, (results) => { this.ux.log(messages.getMessage('commandSuccess', [results.sandboxProcessObj.Id, results.sandboxProcessObj.SandboxName])); }); sandboxOrg.on(sandboxConstants_1.SandboxEventNames.EVENT_STATUS, (results) => { sandboxProgressReporter_1.SandboxProgressReporter.logSandboxProgress(this.ux, results.sandboxProcessObj, results.interval, results.retries, results.waitingOnAuth); }); sandboxOrg.on(sandboxConstants_1.SandboxEventNames.EVENT_RESULT, (results) => { sandboxProgressReporter_1.SandboxProgressReporter.logSandboxProcessResult(this.ux, results.sandboxProcessObj, results.sandboxRes); if (results.sandboxRes && results.sandboxRes.authUserName) { if (this.flags.setalias) { const result = alias.set(this.flags.setalias, results.sandboxRes.authUserName); this.logger.debug('Set Alias: %s result: %s', this.flags.setalias, result); } if (this.flags.setdefaultusername) { const globalConfig = this.configAggregator.getGlobalConfig(); globalConfig.set(core_1.Config.DEFAULT_USERNAME, results.sandboxRes.authUserName); globalConfig .write() .then((result) => this.logger.debug('Set defaultUsername: %s result: %s', this.flags.setdefaultusername, result)); } } }); this.logger.debug('Clone Varargs: %s ', this.varargs); const sandboxReq = new sandboxOrgApi_1.SandboxRequest(); // definitionjson and varargs override file input Object.assign(sandboxReq, defFileContents, this.varargs); this.logger.debug('SandboxRequest after merging DefFile and Varargs: %s ', sandboxReq); // try to find the source sandbox name either from the definition file or the commandline arg // NOTE the name and the case "SourceSandboxName" must match exactly const srcSandboxName = sandboxReq[sandboxConstants_1.SANDBOXDEF_SRC_SANDBOXNAME]; if (srcSandboxName) { // we have to delete this property from the sandboxRequest object, // because sandboxRequest object represent the POST request to create SandboxInfo bpo, // sandboxInfo does not have a column named SourceSandboxName, this field will be converted to sourceId in the clone call below delete sandboxReq[sandboxConstants_1.SANDBOXDEF_SRC_SANDBOXNAME]; } else { // error - we need SourceSandboxName to know which sandbox to clone from throw core_1.SfdxError.create(new core_1.SfdxErrorConfig('salesforce-alm', 'org_clone', 'missingSourceSandboxName', [ sandboxConstants_1.SANDBOXDEF_SRC_SANDBOXNAME, ]).addAction('missingSourceSandboxNameAction', [sandboxConstants_1.SANDBOXDEF_SRC_SANDBOXNAME])); } this.logger.debug('Calling clone with SandboxRequest: %s and SandboxName: %s ', sandboxReq, srcSandboxName); return await sandboxOrg.cloneSandbox(sandboxReq, srcSandboxName); } else { throw core_1.SfdxError.create(new core_1.SfdxErrorConfig('salesforce-alm', 'org_clone', 'commandOrganizationTypeNotSupport', [ orgTypes_1.OrgTypes.Sandbox, ]).addAction('commandOrganizationTypeNotSupportAction', [orgTypes_1.OrgTypes.Sandbox])); } } } exports.OrgCloneCommand = OrgCloneCommand; OrgCloneCommand.longDescription = messages.getMessage('commandLongDescription'); OrgCloneCommand.help = messages.getMessage('commandHelp'); /* * TODO: When SfdxCommand change the messages for description to include both the description and the help messages * we have to remove the help from description. */ OrgCloneCommand.description = messages.getMessage('commandDescription') + '\n\n' + OrgCloneCommand.help; OrgCloneCommand.showProgress = true; OrgCloneCommand.requiresProject = false; OrgCloneCommand.varargs = true; OrgCloneCommand.orgType = consts.DEFAULT_DEV_HUB_USERNAME; OrgCloneCommand.requiresUsername = true; OrgCloneCommand.flagsConfig = { type: command_1.flags.enum({ char: 't', description: messages.getMessage('typeFlagDescription'), longDescription: messages.getMessage('typeFlagLongDescription'), required: true, options: [orgTypes_1.OrgTypes.Sandbox], }), definitionfile: command_1.flags.filepath({ char: 'f', description: messages.getMessage('definitionfileFlagDescription'), longDescription: messages.getMessage('definitionfileFlagLongDescription'), required: false, }), definitionjson: command_1.flags.string({ char: 'j', description: messages.getMessage('definitionjsonFlagDescription'), longDescription: messages.getMessage('definitionjsonFlagLongDescription'), hidden: true, required: false, }), setdefaultusername: command_1.flags.boolean({ char: 's', description: messages.getMessage('setdefaultusernameFlagDescription'), longDescription: messages.getMessage('setdefaultusernameFlagLongDescription'), required: false, }), setalias: command_1.flags.string({ char: 'a', description: messages.getMessage('setaliasFlagDescription'), longDescription: messages.getMessage('setaliasFlagLongDescription'), required: false, }), wait: command_1.flags.minutes({ char: 'w', description: sfdxCommonMessages.getMessage('streamingWait', []), longDescription: sfdxCommonMessages.getMessage('streamingWaitLong', []), required: false, min: kit_1.Duration.minutes(consts.MIN_STREAM_TIMEOUT_MINUTES), default: kit_1.Duration.minutes(consts.DEFAULT_STREAM_TIMEOUT_MINUTES), }), }; //# sourceMappingURL=clone.js.map