/* eslint-disable no-console */ import output from '@/output' import { displayCIDeploymentPrompt, fetchUserProfile, getPickteamCommandConfigContent, getPickteamCommandLoginParams, isPickteamCommandAllowTargetType, pickTheTeamInfo, updateTeamInfoIntoConfig, validatePickteamCommandInputParams, } from '@/scripts' import type { PickteamCLIOptionsSchema, ProjectCIDeployConfigBranch } from '@ones-open/cli-utils' async function pickteam(targetType: string, options: PickteamCLIOptionsSchema) { try { if (!targetType) { throw new Error( `To execute the pickteam command, you need to specify the target type, see the help information for usage examples`, ) } if (!isPickteamCommandAllowTargetType(targetType)) { throw new Error(`Target type only support 'local' or 'ci'`) } let config let converterParams try { config = await getPickteamCommandConfigContent(targetType) converterParams = { config } } catch (error) { const errorMessage = `\nFailed to get config content for target type ${targetType}` + `\n${error}` throw errorMessage } try { await validatePickteamCommandInputParams(targetType, options, { config }) } catch (error) { const errorMessage = 'Command params validation failed:' + `\n${error}` throw errorMessage } if (targetType === 'ci') { const { branchName } = await displayCIDeploymentPrompt( options, config as Map, ) converterParams = { config, branchName } } const loginParams = await getPickteamCommandLoginParams(targetType, converterParams) const userProfile = await fetchUserProfile(loginParams) if (!userProfile) throw new Error('Can not get user profile during pickteam') const selectedTeam = await pickTheTeamInfo(userProfile, options) await updateTeamInfoIntoConfig(selectedTeam, { targetType, config, branchName: converterParams?.branchName, }) output.newline() const succeedMessage = `Updated team information for ${targetType} configuration` output.success(succeedMessage) } catch (error) { output.error(error) } } export { pickteam }