@cto.ai/ops
Version:
š» CTO.ai Ops - The CLI built for Teams š
139 lines (138 loc) ⢠5.96 kB
JavaScript
"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 () => {
try {
const { team: activeTeam } = await this.readConfig();
if (!activeTeam)
throw new Error();
return { activeTeam };
}
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: this.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;
const configData = await this.readConfig();
await this.writeConfig(configData, {
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 = (config) => (inputs) => {
const { user: { email, username }, } = config;
const { activeTeam: { id: oldTeamId }, teamSelected: { id: newTeamId }, } = inputs;
this.services.analytics.track({
userId: email,
cliEvent: 'Ops CLI Team:Switch',
event: 'Ops CLI Team:Switch',
properties: {
email,
username,
oldTeamId,
newTeamId,
},
}, this.accessToken);
};
this.startSpinner = async (inputs) => {
await this.ux.spinner.start(`š ${this.ux.colors.white('Searching for your teams')}`);
return inputs;
};
this.stopSpinner = async (inputs) => {
await this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`);
return inputs;
};
}
async run() {
await this.parse(TeamSwitch);
try {
await this.isLoggedIn();
const switchPipeline = utils_1.asyncPipe(this.startSpinner, this.getActiveTeam, this.getTeamsFromApi, this.setTeamsDisplayName, this.stopSpinner, this.getSelectedTeamPrompt, this.updateActiveTeam, this.logMessage, this.sendAnalytics(this.state.config));
await switchPipeline();
}
catch (err) {
await this.ux.spinner.stop(`${this.ux.colors.errorRed('Failed')}`);
this.debug('%O', err);
this.config.runHook('error', { err, accessToken: this.accessToken });
}
}
}
exports.default = TeamSwitch;
TeamSwitch.description = 'Shows the list of your teams.';
TeamSwitch.flags = {
help: base_1.flags.help({ char: 'h' }),
};