UNPKG

growthbook

Version:

The GrowthBook command-line interface (CLI) for working with the GrowthBook A/B testing, feature flagging, and experimentation platform

82 lines (81 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Fs = require("node:fs"); const core_1 = require("@oclif/core"); const cli_1 = require("../../utils/cli"); const constants_1 = require("../../utils/constants"); const config_1 = require("../../utils/config"); const saved_groups_repository_1 = require("../../repositories/saved-groups.repository"); const http_1 = require("../../utils/http"); class SavedgroupsCreate extends core_1.Command { async run() { const { args: { input, }, flags: { profile, apiBaseUrl, filePath, output, }, } = await this.parse(SavedgroupsCreate); const profileUsed = profile || constants_1.DEFAULT_GROWTHBOOK_PROFILE; const { apiKey, apiBaseUrl: configApiBaseUrl } = (0, config_1.getGrowthBookProfileConfigAndThrowForCommand)(profileUsed, this); const baseUrlUsed = apiBaseUrl || configApiBaseUrl || constants_1.DEFAULT_GROWTHBOOK_BASE_URL; // Read payload from standard in let payload = input; // If no input from standard in, try the filePath (if provided) if (!input && filePath) { payload = Fs.readFileSync(filePath, 'utf-8'); } // If no payload from file, prompt for input if (!payload) { payload = await core_1.ux.prompt('Paste the saved group payload', { required: true, }); } let parsedPayload = null; try { parsedPayload = JSON.parse(payload); } catch { this.error('Unable to parse payload'); } core_1.ux.action.start('Posting parsed payload'); this.logJson(parsedPayload); const repo = new saved_groups_repository_1.SavedGroupsRepository({ apiKey, apiBaseUrl: baseUrlUsed, }); try { // Create the saved group const createdSavedGroup = await repo.createSavedGroup(parsedPayload); this.logJson(createdSavedGroup); core_1.ux.action.stop(`${cli_1.Icons.checkmark} Successfully created saved group ${createdSavedGroup.id}`); // If provided an output path, write to file if (output) { const outputContents = JSON.stringify(createdSavedGroup, null, 2); core_1.ux.action.start('Writing created saved group to file'); try { Fs.writeFileSync(output, outputContents); core_1.ux.action.stop(`${cli_1.Icons.checkmark} Output created saved group to ${output}`); } catch (error) { this.error(`${error}`); core_1.ux.action.stop(`${cli_1.Icons.xSymbol} Failed to write saved group output to file path ${output}`); } } } catch (error) { this.error((0, http_1.errorStringFromResponse)(error)); core_1.ux.action.stop(`${cli_1.Icons.xSymbol} Failed to create saved group`); } } } exports.default = SavedgroupsCreate; SavedgroupsCreate.description = 'Create a saved group'; SavedgroupsCreate.examples = [ '<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --filePath input.json', ]; SavedgroupsCreate.flags = { ...cli_1.baseGrowthBookCliFlags, ...cli_1.fileInputOutputCliFlags, }; SavedgroupsCreate.args = { input: core_1.Args.string({ description: 'JSON payload of the saved group to be created. Docs: https://docs.growthbook.io/api/#tag/saved-groups/operation/postSavedGroup', required: false, }), };