UNPKG

@cto.ai/ops

Version:

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

150 lines (149 loc) • 6.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fuzzy_1 = tslib_1.__importDefault(require("fuzzy")); const base_1 = tslib_1.__importStar(require("./../../base")); const utils_1 = require("./../../utils"); const CustomErrors_1 = require("./../../errors/CustomErrors"); class TeamSwitch extends base_1.default { constructor() { super(...arguments); this.displayTeams = [{ name: '' }]; this.getActiveTeam = async (inputs) => { try { if (!inputs.config.team) throw new Error(); return Object.assign(Object.assign({}, inputs), { activeTeam: inputs.config.team }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.ConfigError(err); } }; this.getTeamsFromApi = async (inputs) => { try { const { data: teams } = await this.services.api.find('/private/teams', { headers: { Authorization: inputs.config.tokens.accessToken }, }); return Object.assign(Object.assign({}, inputs), { teams }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.APIError(err); } }; this.setTeamsDisplayName = (inputs) => { const { teams, activeTeam } = inputs; const displayTeams = teams.map(t => { // If the team is the user's active team, add custom styling to it if (activeTeam && t.name === activeTeam.name) { return Object.assign(Object.assign({}, t), { displayName: `${this.ux.colors.blue(t.name)} ${this.ux.colors.dim('[Active]')}` }); } // If the team isn't the user's active team, simply copy the display name from the team name return Object.assign(Object.assign({}, t), { displayName: t.name }); }); return Object.assign(Object.assign({}, inputs), { displayTeams }); }; this._autocompleteSearch = async (_, input = '') => { const { list, options } = this.fuzzyFilterParams(); const fuzzyResult = fuzzy_1.default.filter(input, list, options); return fuzzyResult.map(result => result.original); }; this.fuzzyFilterParams = () => { const list = this.displayTeams.map(team => { return { name: `${team.name}`, value: team, }; }); const options = { extract: el => el.name }; return { list, options }; }; this.getSelectedTeamPrompt = async (inputs) => { this.log("Here's the list of your teams:\n"); const { displayTeams } = inputs; this.displayTeams = displayTeams; const { teamSelected } = await this.ux.prompt({ type: 'autocomplete', name: 'teamSelected', message: 'Select a team', source: this._autocompleteSearch.bind(this), bottomContent: `\n \n${this.ux.colors.white(`Or, run ${this.ux.colors.italic.dim('ops help')} for usage information.`)}`, }); this.log(`\nā± Switching teams`); return Object.assign(Object.assign({}, inputs), { teamSelected }); }; this.updateActiveTeam = async (inputs) => { try { const { teamSelected: { name, id }, } = inputs; await this.writeConfig(inputs.config, { team: { name, id }, }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.ConfigError(err); } return inputs; }; this.logMessage = (inputs) => { const { teamSelected: { name }, } = inputs; this.log(`\nšŸš€ Huzzah! ${this.ux.colors.callOutCyan(name)} is now the active team.\n`); return inputs; }; this.sendAnalytics = (inputs) => { const { config } = inputs; config.team = inputs.teamSelected; this.services.analytics.track('Ops CLI Team:Switch', { username: config.user.username, }, config); }; this.startSpinner = async (inputs) => { const { teamName } = inputs; this.ux.spinner.start(`šŸ” ${this.ux.colors.white(`Searching for your team${teamName === undefined ? 's' : ''}`)}`); return inputs; }; this.stopSpinner = async (inputs) => { this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`); return inputs; }; this.isArgTeamAvailable = async (inputs) => { const { teamName, teams } = inputs; const teamSelected = teams.find(team => team.name === teamName); if (teamSelected === undefined) { this.log(`ā— We couldn't find a team with the name: ${this.ux.colors.errorRed(teamName === undefined ? '' : `"${teamName}"`)}`); process.exit(0); } return Object.assign(Object.assign({}, inputs), { teamSelected }); }; } async run() { const { args: { teamName }, } = this.parse(TeamSwitch); const config = await this.isLoggedIn(); try { let switchPipeline; if (teamName === undefined) { switchPipeline = (0, utils_1.asyncPipe)(this.startSpinner, this.getActiveTeam, this.getTeamsFromApi, this.setTeamsDisplayName, this.stopSpinner, this.getSelectedTeamPrompt); } else { switchPipeline = (0, utils_1.asyncPipe)(this.startSpinner, this.getTeamsFromApi, this.stopSpinner, this.isArgTeamAvailable); } const inputs = await switchPipeline({ config, teamName }); await (0, utils_1.asyncPipe)(this.updateActiveTeam, this.logMessage, this.sendAnalytics)(inputs); } catch (err) { this.ux.spinner.stop(`${this.ux.colors.errorRed('Failed')}`); this.debug('%O', err); this.config.runHook('error', { accessToken: config.tokens.accessToken, err, }); } } } exports.default = TeamSwitch; TeamSwitch.description = 'Switch your currently active team.'; TeamSwitch.flags = { help: base_1.flags.help({ char: 'h' }), }; TeamSwitch.args = [{ name: 'teamName', description: 'Team Name' }];