UNPKG

vtex

Version:

The platform for e-commerce apps

92 lines (91 loc) 4.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.returnToPreviousAccount = exports.switchAccount = void 0; const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const utils_1 = require("../../error/utils"); const SessionManager_1 = require("../../session/SessionManager"); const logger_1 = tslib_1.__importDefault(require("../../logger")); const create_1 = require("../workspace/create"); const welcome_1 = tslib_1.__importDefault(require("../../../modules/auth/welcome")); const constants_1 = require("../../constants"); var SwitchStatus; (function (SwitchStatus) { SwitchStatus["SwitchedAccount"] = "SwitchedAccount"; SwitchStatus["SwitchedOnlyWorkspace"] = "SwitchedOnlyWorkspace"; SwitchStatus["SwitchedNothing"] = "SwitchedNothing"; })(SwitchStatus || (SwitchStatus = {})); const checkAndSwitch = async (targetAccount, targetWorkspace) => { const session = SessionManager_1.SessionManager.getSingleton(); const { account: initialAccount, workspace: initialWorkspace } = session; const isValidAccount = /^\s*[\w-]+\s*$/.test(targetAccount); if (!isValidAccount) { throw utils_1.createFlowIssueError('Invalid account format'); } else if (!initialAccount) { throw utils_1.createFlowIssueError("You're not logged in right now"); } await session.login(targetAccount, { targetWorkspace, workspaceCreation: { promptCreation: true, creator: create_1.workspaceCreator, onError: create_1.handleErrorCreatingWorkspace, }, }); const { account, workspace, userLogged } = session; logger_1.default.info(`Logged into ${chalk_1.default.hex(constants_1.COLORS.BLUE)(account)} as ${chalk_1.default.green(userLogged)} at workspace ${chalk_1.default.green(workspace)}`); if (initialAccount !== targetAccount) { return SwitchStatus.SwitchedAccount; } if (initialWorkspace !== targetWorkspace) { return SwitchStatus.SwitchedOnlyWorkspace; } return SwitchStatus.SwitchedNothing; }; exports.switchAccount = async (targetAccount, options) => { const { account: currAccount, lastUsedAccount, workspace: currWorkspace } = SessionManager_1.SessionManager.getSingleton(); if (options.gracefulAccountCheck && currAccount === targetAccount) { return false; } if (targetAccount === '-') { targetAccount = lastUsedAccount; if (targetAccount == null) { throw utils_1.createFlowIssueError('No last used account was found'); } } const previousAccount = currAccount; const [parsedAccount, parsedWorkspace] = targetAccount.split('/'); if (parsedWorkspace) { options = { ...options, workspace: parsedWorkspace }; } const switchStatus = await checkAndSwitch(parsedAccount, options.workspace || 'master'); if (switchStatus === SwitchStatus.SwitchedAccount) { logger_1.default.info(`Switched from ${chalk_1.default.hex(constants_1.COLORS.BLUE)(previousAccount)} to ${chalk_1.default.hex(constants_1.COLORS.BLUE)(parsedAccount)}`); } else if (switchStatus === SwitchStatus.SwitchedOnlyWorkspace) { logger_1.default.info(`Switched from workspace ${chalk_1.default.hex(constants_1.COLORS.BLUE)(currWorkspace)} to ${chalk_1.default.hex(constants_1.COLORS.BLUE)(parsedWorkspace)} in account ${chalk_1.default.hex(constants_1.COLORS.BLUE)(parsedAccount)}`); } else if (switchStatus === SwitchStatus.SwitchedNothing) { logger_1.default.info(`You're already logged in ${chalk_1.default.hex(constants_1.COLORS.BLUE)(targetAccount)}`); } if (options.showWelcomeMessage && switchStatus === SwitchStatus.SwitchedAccount) { await welcome_1.default(); } return true; }; function returnToPreviousAccount({ previousAccount, previousWorkspace, promptConfirmation = true, }) { return exports.switchAccount(previousAccount, { workspace: previousWorkspace, gracefulAccountCheck: true, showWelcomeMessage: false, ...(promptConfirmation ? { initialPrompt: { message: `Now you are logged in ${chalk_1.default.hex(constants_1.COLORS.BLUE)(SessionManager_1.SessionManager.getSingleton().account)}. Do you want to return to ${chalk_1.default.hex(constants_1.COLORS.BLUE)(previousAccount)} account?`, }, } : null), }); } exports.returnToPreviousAccount = returnToPreviousAccount;