UNPKG

@cto.ai/ops

Version:

💻 CTO.ai Ops - The CLI built for Teams 🚀

126 lines (125 loc) 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const base_1 = tslib_1.__importStar(require("../../base")); const CustomErrors_1 = require("../../errors/CustomErrors"); const utils_1 = require("../../utils"); class TeamInfo extends base_1.default { constructor() { super(...arguments); this.getActiveTeam = async (inputs) => { try { const { team: activeTeam } = await this.readConfig(); if (!activeTeam) throw new Error(); return Object.assign(Object.assign({}, inputs), { activeTeam }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.ConfigError(err); } }; this.getActiveTeamInfo = async (inputs) => { try { const { accessToken } = inputs.config.tokens; const { data: members } = await this.services.api.find(`/private/teams/${inputs.activeTeam.name}/members`, { headers: { Authorization: accessToken }, }); return Object.assign(Object.assign({}, inputs), { members }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.APIError(err); } }; this.getActiveTeamCreator = async (inputs) => { try { const { accessToken } = inputs.config.tokens; const { data: creator } = await this.services.api.find(`/private/teams/${inputs.activeTeam.name}/creator`, { headers: { Authorization: accessToken }, }); return Object.assign(Object.assign({}, inputs), { creator }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.APIError(err); } }; this.logTeamInfo = (inputs) => { const { members, creator, activeTeam } = inputs; this.log(''); this.ux.table([activeTeam], { team: { get: row => row.name, minWidth: 40, }, creationDate: { header: 'Creation Date', get: () => new Date(creator.createdAt).toLocaleDateString(), }, }); this.log(''); members.sort((a, b) => { return a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : 0; }); this.ux.table(members, { members: { get: row => row.username, minWidth: 40, }, joinDate: { header: 'Join Date', get: row => new Date(row.createdAt).toLocaleDateString(), }, creator: { header: ' ', get: row => (row.userId !== creator.userId ? '' : 'Creator'), }, }, { sort: '-creator' }); this.log(' '); return inputs; }; this.sendAnalytics = (inputs) => { const { config: { user: { email, username }, tokens: { accessToken }, }, activeTeam: { name: activeTeamName }, } = inputs; this.services.analytics.track({ userId: email, cliEvent: 'Ops CLI Team:Info', event: 'Ops CLI Team:Info', properties: { email, username, activeTeamName, }, }, accessToken); }; this.startSpinner = async (inputs) => { await this.ux.spinner.start(`🔍 ${this.ux.colors.white('Getting your active team information')}`); return inputs; }; this.stopSpinner = async (inputs) => { await this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`); return inputs; }; } async run() { await this.parse(TeamInfo); const config = await this.isLoggedIn(); try { const infoPipeline = utils_1.asyncPipe(this.startSpinner, this.getActiveTeam, this.getActiveTeamInfo, this.getActiveTeamCreator, this.stopSpinner, this.logTeamInfo, this.sendAnalytics); await infoPipeline({ config }); } catch (err) { await this.ux.spinner.stop(`${this.ux.colors.errorRed('Failed')}`); this.debug('%O', err); this.config.runHook('error', { err, accessToken: config.tokens.accessToken, }); } } } exports.default = TeamInfo; TeamInfo.description = 'Shows basic team information for the team you are currently on.'; TeamInfo.flags = { help: base_1.flags.help({ char: 'h' }), };