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.55 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 metrics_repository_1 = require("../../repositories/metrics.repository"); const http_1 = require("../../utils/http"); class MetricsCreate extends core_1.Command { async run() { const { args: { input, }, flags: { profile, apiBaseUrl, filePath, output, }, } = await this.parse(MetricsCreate); 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 metric 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 metricsRepo = new metrics_repository_1.MetricsRepository({ apiKey, apiBaseUrl: baseUrlUsed, }); try { // Create the metric const createdMetric = await metricsRepo.createMetric(parsedPayload); this.logJson(createdMetric); core_1.ux.action.stop(`${cli_1.Icons.checkmark} Successfully created metric ${createdMetric.id}`); // If provided an output path, write to file if (output) { const outputContents = JSON.stringify(createdMetric, null, 2); core_1.ux.action.start('Writing created metric to file'); try { Fs.writeFileSync(output, outputContents); core_1.ux.action.stop(`${cli_1.Icons.checkmark} Output created metric to ${output}`); } catch (error) { this.error(`${error}`); core_1.ux.action.stop(`${cli_1.Icons.xSymbol} Failed to write metric 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 metric`); } } } exports.default = MetricsCreate; MetricsCreate.description = 'Create a metric from file or standard in'; MetricsCreate.examples = [ 'cat my-metric.json | <%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --filePath my-metric.json', ]; MetricsCreate.flags = { ...cli_1.baseGrowthBookCliFlags, ...cli_1.fileInputOutputCliFlags, }; MetricsCreate.args = { input: core_1.Args.string({ description: 'JSON payload of the metric to be created. Docs: https://docs.growthbook.io/api/#tag/metrics/operation/postMetric', required: false, }), };