UNPKG

@cto.ai/ops

Version:

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

78 lines (77 loc) • 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigService = void 0; const tslib_1 = require("tslib"); const fuzzy_1 = tslib_1.__importDefault(require("fuzzy")); const debug_1 = tslib_1.__importDefault(require("debug")); const cli_sdk_1 = require("@cto.ai/cli-sdk"); const utils_1 = require("./../utils"); const CustomErrors_1 = require("./../errors/CustomErrors"); const debug = (0, debug_1.default)('ops:ConfigService'); const { bold, callOutCyan, multiBlue, reset, whiteBright } = cli_sdk_1.ux.colors; class ConfigService { constructor() { this.teamConfigs = []; this.getApiConfigsList = async (inputs) => { const { config: { team: { name: teamName }, tokens: { accessToken }, }, api, } = inputs; try { const { data: teamConfigs, } = await api.find(`/private/teams/${teamName}/configs`, { headers: { Authorization: accessToken, }, }); return Object.assign(Object.assign({}, inputs), { teamConfigs }); } catch (err) { throw new CustomErrors_1.APIError(err); } }; this.teamConfigSelectorPrompt = async (inputs) => { if (inputs.teamConfigs.length < 1) { console.log(whiteBright(`\nšŸ˜ž No configs found for team ${multiBlue(inputs.config.team.name)}.\n Try again or run ${(0, utils_1.terminalText)('ops team:switch')} to switch your current team. \n`)); process.exit(); } const { config: { team }, teamConfigs, } = inputs; console.log(bold(callOutCyan(` Listing all configs for team ${multiBlue(team.name)} ${reset.green('→')}`))); this.teamConfigs = teamConfigs; const { selectedConfig } = await cli_sdk_1.ux.prompt({ type: 'autocomplete', name: 'selectedConfig', pageSize: 5, message: cli_sdk_1.ux.colors.reset.dim('šŸ” Search:'), source: this._autocompleteConfigList.bind(this), }); return Object.assign(Object.assign({}, inputs), { selectedConfig }); }; this._autocompleteConfigList = async (_, searchQuery = '') => { const { list, options } = this.fuzzyFilterParamsList(); const fuzzyResult = fuzzy_1.default.filter(searchQuery, list, options); return fuzzyResult.map(result => result.original); }; this.fuzzyFilterParamsList = () => { const list = this.teamConfigs.map(config => { return { name: config.key, value: config, }; }); const options = { extract: el => el.name }; return { list, options }; }; } async runListPipeline(config, api) { try { const configsListPipeline = (0, utils_1.asyncPipe)(this.getApiConfigsList, this.teamConfigSelectorPrompt); const results = await configsListPipeline({ config, api, }); return results; } catch (err) { debug('%O', err); throw err; } } } exports.ConfigService = ConfigService;