UNPKG

@cto.ai/ops

Version:

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

106 lines (105 loc) • 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cli_sdk_1 = require("@cto.ai/cli-sdk"); const base_1 = tslib_1.__importStar(require("./../../base")); const CustomErrors_1 = require("./../../errors/CustomErrors"); const utils_1 = require("./../../utils"); const { white, reset } = cli_sdk_1.ux.colors; class TeamCreate extends base_1.default { constructor() { super(...arguments); this.guardAgainstInvalidName = async (inputs) => { try { if (!inputs.name) return Object.assign({}, inputs); const isValidName = await this.validateTeamName(inputs.name); if (!isValidName || typeof isValidName === 'string') { throw new CustomErrors_1.InvalidTeamNameFormat(null); } return inputs; } catch (err) { throw err; } }; this.promptForTeamName = async (inputs) => { if (inputs.name) { return inputs; } const { name } = await cli_sdk_1.ux.prompt({ type: 'input', name: 'name', message: `\nChoose a display name for your team so you can share workflows ${reset.green('→')}\nāœļø ${white('Team Name')} `, afterMessage: `${reset.green('āœ“')} Team name `, validate: this.validateTeamName.bind(this), }); return Object.assign(Object.assign({}, inputs), { 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 = (inputs) => { let { config } = inputs; config.team = inputs.team; this.services.analytics.track('Ops CLI Team:Create', { username: inputs.config.user.username, }, config); }; 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); const config = await this.isLoggedIn(); try { const createPipeline = (0, utils_1.asyncPipe)(this.guardAgainstInvalidName, this.promptForTeamName, this.createTeam, this.logMessage, this.setTeamConfig, this.sendAnalytics); await createPipeline({ name, config }); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: config.tokens.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' }), };