/* eslint-disable no-console */ import output from '@/output' import { convertCIDeploymentBranchName, convertPromptsAnswerToLoginParams, displayCIDeploymentPrompts, displayLoginPrompts, displayPickteamPromptAfterLogin, fetchUserProfile, getCIDeploymentConfigContent, loadScopeMap, pickTheTeamInfo, updateCIDeploymentConfig, updateTeamInfoIntoConfig, } from '@/scripts' import { CI_DEPLOY_DEFAULT_BRANCH_CONTENT, CI_DEPLOY_RELATIVE_PATH } from '@ones-open/cli-utils' import type { CISettingCLIOptionsSchema, LoginRequestParamsSchema, LoginResponseSchema, } from '@ones-open/cli-utils' import { isEqual, isNil } from '@senojs/lodash' import Listr from 'listr' import type { ListrTask } from 'listr' async function updateCIConfig(baseURL: string | undefined, options: CISettingCLIOptionsSchema) { const scopeMap = await loadScopeMap() const { branchName } = await displayCIDeploymentPrompts(options) const loginAnswers = await displayLoginPrompts({ baseURL }, scopeMap, { ...options, isNeedHostURL: false, }) try { const updateDeploymentConfigTasks: ListrTask<{ deployBranchName: string loginParams: LoginRequestParamsSchema userProfile: LoginResponseSchema }>[] = [ { title: 'Converting CI deployment params', task: async (ctx) => { const loginParams = await convertPromptsAnswerToLoginParams(loginAnswers, scopeMap) const deployBranchName = convertCIDeploymentBranchName(branchName) ctx.loginParams = loginParams ctx.deployBranchName = deployBranchName }, }, { title: 'Validating CI deployment params', 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 ${CI_DEPLOY_RELATIVE_PATH}`, task: async (ctx) => { const { loginParams: { baseURL: host, password }, userProfile: { user: { email, phone }, }, deployBranchName, } = ctx const username = email || phone const CIdeploymentConfigContent = await getCIDeploymentConfigContent() const currentBranchDeploymentConfig = CIdeploymentConfigContent?.[deployBranchName] const { host: currentHost, username: currentUsername } = currentBranchDeploymentConfig ?? {} const isNeedUpdate = [currentHost, currentUsername].some(isNil) || !isEqual([host, username], [currentHost, currentUsername]) if (!isNeedUpdate) return const freshDeploymentConfigForCurrentBranch = { [deployBranchName]: { ...CI_DEPLOY_DEFAULT_BRANCH_CONTENT, host, username, password, }, } return updateCIDeploymentConfig(freshDeploymentConfigForCurrentBranch) }, }, ] const updateCIDeploymentActions = new Listr(updateDeploymentConfigTasks) const { userProfile, deployBranchName } = await updateCIDeploymentActions.run() const succeedMessage = `Updated CI deployment configuration` + `\nNow you can commit your changes to the remote repository and deploy your project` output.success(succeedMessage) const isNeedExecutePickteam = await displayPickteamPromptAfterLogin() if (isNeedExecutePickteam) { await pickteamAfterUpdateCIConfig(userProfile, deployBranchName) } } catch (error) { output.error(error) } } async function pickteamAfterUpdateCIConfig(userProfile: LoginResponseSchema, branchName: string) { const selectedTeam = await pickTheTeamInfo(userProfile) await updateTeamInfoIntoConfig(selectedTeam, { targetType: 'ci', branchName }) const succeedMessage = 'Updated team information for CI deployment configuration' output.newline() output.success(succeedMessage) } export { updateCIConfig }