@cto.ai/ops
Version:
š» CTO.ai - The CLI built for Teams š
80 lines (79 loc) ⢠3.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const base_1 = tslib_1.__importDefault(require("./../../base"));
const CustomErrors_1 = require("./../../errors/CustomErrors");
const utils_1 = require("./../../utils");
class TeamJoin extends base_1.default {
constructor() {
super(...arguments);
this.inviteCodePrompt = async (inputs) => {
const { inviteCode } = await this.ux.prompt({
type: 'input',
name: 'inviteCode',
message: `Please enter the invite code you received via email to join a team:\n\nš ${this.ux.colors.white('Invite code ')}`,
validate: (input) => !!input,
});
return Object.assign(Object.assign({}, inputs), { inviteCode });
};
this.startSpinner = async (inputs) => {
this.log('');
this.ux.spinner.start(`${this.ux.colors.white('Working on it')}`);
return inputs;
};
this.joinTeam = async (inputs) => {
try {
const { inviteCode } = inputs;
const { data: newTeam } = await this.services.api.create('/private/teams/accept', { inviteCode }, { headers: { Authorization: this.accessToken } });
if (!newTeam)
throw new CustomErrors_1.InviteCodeInvalid(null);
return Object.assign(Object.assign({}, inputs), { newTeam });
}
catch (err) {
this.debug('%O', err);
throw new CustomErrors_1.InviteCodeInvalid(err);
}
};
this.setActiveTeam = async (inputs) => {
const { newTeam: { id, name }, } = inputs;
const oldConfig = await this.readConfig();
await this.writeConfig(oldConfig, { team: { name, id } });
return inputs;
};
this.logMessage = (inputs) => {
const { newTeam: { name }, } = inputs;
this.log(`${this.ux.colors.primary("Success! You've been added to team, ")}${this.ux.colors.callOutCyan(name)} ${this.ux.colors.secondary('(Active)')}`);
this.log(`${this.ux.colors.secondary("You've been automatically switched to this team.")}\n`);
this.log(`Try running this command to get started:\n\n$ ops search`);
return inputs;
};
this.sendAnalytics = (inputs) => {
let { config } = inputs;
config.team = inputs.newTeam;
this.services.analytics.track('Ops CLI Team:Join', {
username: config.user.username,
}, config);
};
this.stopSpinner = async (inputs) => {
this.ux.spinner.stop(`${this.ux.colors.successGreen('Done')}`);
return inputs;
};
}
async run() {
const config = await this.isLoggedIn();
try {
const joinPipeline = (0, utils_1.asyncPipe)(this.inviteCodePrompt, this.startSpinner, this.joinTeam, this.setActiveTeam, this.stopSpinner, this.logMessage, this.sendAnalytics);
await joinPipeline({ config });
}
catch (err) {
this.ux.spinner.stop(`${this.ux.colors.errorRed('Failed')}`);
this.debug('%O', err);
this.config.runHook('error', {
err,
accessToken: this.accessToken,
});
}
}
}
exports.default = TeamJoin;
TeamJoin.description = 'Accept an invite to join a team.';