@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
128 lines (127 loc) • 4.96 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 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([Object.assign({}, activeTeam)], {
team: {
get: row => row.name,
minWidth: 40,
},
team_id: {
get: row => row.id,
header: 'Team ID',
minWidth: 40,
},
});
this.log('');
this.ux.table([Object.assign({}, creator)], {
creationDate: {
get: row => new Date(row.createdAt).toLocaleDateString(),
header: 'Creation Date',
minWidth: 40,
},
});
this.log('');
members.sort((a, b) => {
return a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : 0;
});
this.ux.table(members, {
joinDate: {
get: row => new Date(row.createdAt).toLocaleDateString(),
header: 'Join Date',
},
members: {
get: row => row.username,
minWidth: 40,
},
owner: {
get: row => (row.userId !== creator.userId ? '' : 'Owner'),
header: ' ',
},
}, { sort: '-owner' });
this.log(' ');
return inputs;
};
this.sendAnalytics = async (inputs) => {
const { config } = inputs;
this.services.analytics.track('Ops CLI Team:Info', {
username: config.user.username,
}, config);
};
this.startSpinner = async (inputs) => {
this.ux.spinner.start(`🔍 ${this.ux.colors.white('Getting your active team information')}`);
return inputs;
};
this.stopSpinner = async (inputs) => {
this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`);
return inputs;
};
}
async run() {
this.parse(TeamInfo);
const config = await this.isLoggedIn();
try {
const infoPipeline = (0, utils_1.asyncPipe)(this.startSpinner, this.getActiveTeam, this.getActiveTeamInfo, this.getActiveTeamCreator, this.stopSpinner, this.logTeamInfo, this.sendAnalytics);
await infoPipeline({ config });
}
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 = TeamInfo;
TeamInfo.description = 'Shows basic team information for the team you are currently on.';
TeamInfo.flags = {
help: base_1.flags.help({ char: 'h' }),
};