@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
91 lines (90 loc) • 3.76 kB
JavaScript
"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 TeamList extends base_1.default {
constructor() {
super(...arguments);
this.getActiveTeam = async (inputs) => {
try {
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.printTeams = async (inputs) => {
this.log("Here's the list of your teams:\n");
const { displayTeams } = inputs;
await this.ux.print(displayTeams
.map((value) => {
return ` • ${value.displayName}`;
})
.join('\n'));
return Object.assign({}, inputs);
};
this.sendAnalytics = (inputs) => {
const { config, teams } = inputs;
this.services.analytics.track('Ops CLI Team:List', {
username: config.user.username,
results: teams.length,
}, config);
};
this.startSpinner = async (inputs) => {
this.ux.spinner.start(`🔍 ${this.ux.colors.white('Searching for your teams')}`);
return inputs;
};
this.stopSpinner = async (inputs) => {
this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`);
return inputs;
};
}
async run() {
this.parse(TeamList);
const config = await this.isLoggedIn();
try {
const listPipeline = (0, utils_1.asyncPipe)(this.startSpinner, this.getActiveTeam, this.getTeamsFromApi, this.setTeamsDisplayName, this.stopSpinner, this.printTeams, this.sendAnalytics);
await listPipeline({ config });
}
catch (err) {
this.ux.spinner.stop(`${this.ux.colors.errorRed('Failed')}`);
this.debug('%O', err);
this.config.runHook('error', {
err,
accessToken: config.tokens.accessToken,
});
}
}
}
exports.default = TeamList;
TeamList.description = 'Shows the list of your teams.';
TeamList.flags = {
help: base_1.flags.help({ char: 'h' }),
};