UNPKG

@cto.ai/ops

Version:

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

108 lines (107 loc) • 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const sdk_1 = require("@cto.ai/sdk"); const base_1 = tslib_1.__importStar(require("../../base")); const CustomErrors_1 = require("../../errors/CustomErrors"); const utils_1 = require("../../utils"); const { white, reset } = sdk_1.ux.colors; class TeamCreate extends base_1.default { constructor() { super(...arguments); this.guardAgainstInvalidName = async (name) => { try { if (!name) return { name }; const isValidName = await this.validateTeamName(name); if (!isValidName || typeof isValidName === 'string') { throw new CustomErrors_1.InvalidTeamNameFormat(null); } return { name }; } catch (err) { throw err; } }; this.promptForTeamName = async (inputs) => { if (inputs.name) { return inputs; } const { name } = await sdk_1.ux.prompt({ type: 'input', name: 'name', message: `\nChoose a display name for your team and share ops ${reset.green('→')}\nāœļø ${white('Team Name')} `, afterMessage: `${reset.green('āœ“')} Team name `, validate: this.validateTeamName.bind(this), }); return { name }; }; this.createTeam = async (inputs) => { try { const { name } = inputs; const res = await this.services.api.create('/private/teams', { name }, { headers: { Authorization: this.accessToken } }); const team = { id: res.data.id, name: res.data.name }; return Object.assign(Object.assign({}, inputs), { team }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.InvalidTeamNameFormat(err); } }; this.logMessage = (inputs) => { this.log(`\n ${white('šŸ™Œ Your team has been created!')}`); return inputs; }; this.setTeamConfig = async (inputs) => { const { team } = inputs; const oldConfig = await this.readConfig(); await this.writeConfig(oldConfig, { team }); return inputs; }; this.sendAnalytics = (userId) => (inputs) => { const { team: { id: teamId, name }, } = inputs; this.services.analytics.track({ cliEvent: 'Ops Team Create', event: 'Ops Team Create', userId, teamId, properties: { teamName: name, }, }, this.accessToken); }; this.validateTeamName = async (name) => { try { if (!utils_1.validCharsTeamName.test(name)) { return `Invalid team name. May contain only letters (case-sensitive), numbers, dashes (-), and underscores (_).`; } const unique = await this.validateUniqueField({ username: name }, this.accessToken); if (!unique) { return `šŸ˜ž Sorry this name has already been taken. Try again with a different name.`; } return true; } catch (err) { throw new CustomErrors_1.InvalidTeamNameFormat(err); } }; } async run() { let { flags: { name }, } = this.parse(TeamCreate); try { await this.isLoggedIn(); const createPipeline = utils_1.asyncPipe(this.guardAgainstInvalidName, this.promptForTeamName, this.createTeam, this.logMessage, this.setTeamConfig, this.sendAnalytics(this.user.email)); await createPipeline(name); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: this.accessToken }); } } } exports.default = TeamCreate; TeamCreate.description = 'Create your team.'; TeamCreate.flags = { help: base_1.flags.help({ char: 'h' }), name: base_1.flags.string({ char: 'n' }), };