UNPKG

@cto.ai/ops

Version:

šŸ’» CTO.ai Ops - The CLI built for Teams šŸš€

88 lines (87 loc) • 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const base_1 = tslib_1.__importDefault(require("../../base")); const CustomErrors_1 = require("../../errors/CustomErrors"); const utils_1 = require("../../utils"); class TeamJoin extends base_1.default { constructor() { super(...arguments); this.inviteCodePrompt = async () => { const { inviteCode } = await this.ux.prompt({ type: 'input', name: 'inviteCode', message: `Please enter the invite code you received via email to join a team:\n\nšŸŽŸ ${this.ux.colors.white('Invite code ')}`, validate: (input) => !!input, }); return { inviteCode }; }; this.startSpinner = async (inputs) => { this.log(''); await this.ux.spinner.start(`${this.ux.colors.white('Working on it')}`); return inputs; }; this.joinTeam = async (inputs) => { try { const { inviteCode } = inputs; const { data: newTeam } = await this.services.api.create('/private/teams/accept', { inviteCode }, { headers: { Authorization: this.accessToken } }); if (!newTeam) throw new CustomErrors_1.InviteCodeInvalid(null); return Object.assign(Object.assign({}, inputs), { newTeam }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.InviteCodeInvalid(err); } }; this.setActiveTeam = async (inputs) => { const { newTeam: { id, name }, } = inputs; const oldConfig = await this.readConfig(); await this.writeConfig(oldConfig, { team: { name, id } }); return inputs; }; this.logMessage = (inputs) => { const { newTeam: { name }, } = inputs; this.log(`${this.ux.colors.primary("Success! You've been added to team, ")}${this.ux.colors.callOutCyan(name)} ${this.ux.colors.secondary('(Active)')}`); this.log(`${this.ux.colors.secondary("You've been automatically switched to this team.")}\n`); this.log(`Try running this command to get started:\n\n$ ops search`); return inputs; }; this.sendAnalytics = (config) => (inputs) => { const { newTeam: { id: teamId }, } = inputs; const { user: { email, username }, } = config; this.services.analytics.track({ userId: email, teamId, cliEvent: 'Ops CLI team:join', event: 'Ops CLI team:join', properties: { email, username, }, }, this.accessToken); }; this.stopSpinner = async (inputs) => { await this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`); return inputs; }; } async run() { this.parse(TeamJoin); try { await this.isLoggedIn(); const joinPipeline = utils_1.asyncPipe(this.inviteCodePrompt, this.startSpinner, this.joinTeam, this.setActiveTeam, this.stopSpinner, this.logMessage, this.sendAnalytics(this.state.config)); await joinPipeline(); } catch (err) { this.ux.spinner.stop(`${this.ux.colors.errorRed('Failed')}`); this.debug('%O', err); this.config.runHook('error', { err, accessToken: this.accessToken, }); } } } exports.default = TeamJoin; TeamJoin.description = 'Accept an invite to join a team.';