/* eslint-disable no-console */ import output from '@/output' import { convertPromptsAnswerToLoginParams, displayLoginPrompts, displayPickteamPromptAfterLogin, fetchUserProfile, loadScopeMap, pickTheTeamInfo, updateLocalConfig, updateScopeIntoGlobalPluginConfig, updateTeamInfoIntoConfig, getLocalConfigContent, } from '@/scripts' import { createUpgradeMessage } from '@/scripts/shared' import { GLOBAL_CLI_PLUGIN_CONFIG_FILE_PATH, LOCAL_CONFIG_RELATIVE_PATH, } from '@ones-open/cli-utils' import type { LoginCLIOptionsSchema, LoginRequestParamsSchema, LoginResponseSchema, } from '@ones-open/cli-utils' import { isEqual, isNil, merge } from '@senojs/lodash' import Listr from 'listr' import type { ListrTask } from 'listr' async function login( baseURL: string | undefined, hostURL: string | undefined, options: LoginCLIOptionsSchema, ) { const scopeMap = await loadScopeMap() const answers = await displayLoginPrompts({ baseURL, hostURL }, scopeMap, options) try { const loginActionTasks: ListrTask<{ loginParams: LoginRequestParamsSchema userProfile: LoginResponseSchema }>[] = [ { title: 'Converting inputted answers to login params', task: async (ctx) => { const loginParams = await convertPromptsAnswerToLoginParams(answers, scopeMap) ctx.loginParams = loginParams }, }, { title: 'Fetching user profile', task: async (ctx) => { const userProfile = await fetchUserProfile(ctx.loginParams) if (!userProfile) throw new Error('Failed to fetch user profile') ctx.userProfile = userProfile }, }, { title: `Updating configuration into ${LOCAL_CONFIG_RELATIVE_PATH}`, task: async (ctx) => { const { loginParams: { baseURL, password }, userProfile: { user: { email, phone, uuid: userUUID }, }, } = ctx const username = email || phone const currentLocalConfigContent = await getLocalConfigContent() const { platform: { baseURL: currentBaseUrl, username: currentUsername }, local: { user_uuid: currentUserUUID }, } = currentLocalConfigContent const isNeedToClearOrgAndTeamUUID = [currentBaseUrl, currentUsername, currentUserUUID].some(isNil) || !isEqual( [baseURL, username, userUUID], [currentBaseUrl, currentUsername, currentUserUUID], ) let freshLocalConfigFields = { platform: { address: answers.hostURL, baseURL, username, password, }, local: { user_uuid: userUUID, }, } if (isNeedToClearOrgAndTeamUUID) { freshLocalConfigFields = merge(freshLocalConfigFields, { local: { organization_uuid: null, team_uuid: null, }, }) } return updateLocalConfig(freshLocalConfigFields) }, }, { title: `Updating scope info into ${GLOBAL_CLI_PLUGIN_CONFIG_FILE_PATH}`, task: (ctx) => { const { loginParams, userProfile } = ctx const cloneScopeMap = new Map(scopeMap) cloneScopeMap.set(loginParams.baseURL, { baseURL: loginParams.baseURL, username: userProfile.user.email || userProfile.user.phone, password: loginParams.password, }) return updateScopeIntoGlobalPluginConfig(cloneScopeMap) }, }, ] const loginActions = new Listr(loginActionTasks) const { userProfile } = await loginActions.run() const succeedMessage = 'Login in' output.success(succeedMessage) const isNeedExecutePickteam = await displayPickteamPromptAfterLogin() if (isNeedExecutePickteam) { await pickteamAfterLogin(userProfile) } } catch (error) { output.error(error) } } async function pickteamAfterLogin(userProfile: LoginResponseSchema) { const selectedTeam = await pickTheTeamInfo(userProfile) await updateTeamInfoIntoConfig(selectedTeam, { targetType: 'local' }) output.newline() const succeedMessage = 'Updated team information for local configuration' const upgradeMessage = await createUpgradeMessage() output.success(succeedMessage) upgradeMessage && output.warn(upgradeMessage) } export { login }