UNPKG

growthbook

Version:

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

75 lines (74 loc) 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGrowthBookProfileConfigAndThrowForCommand = exports.getGrowthBookProfileConfig = exports.getGrowthBookConfigToml = void 0; const Fs = require("node:fs"); const toml = require("@iarna/toml"); const file_1 = require("./file"); const constants_1 = require("./constants"); /** * Get the text content of the config file. * @return {string} String contents of the config file or an empty string */ function getGrowthBookConfigToml() { try { const filePath = (0, file_1.getGrowthBookConfigFilePath)(); return Fs.readFileSync(filePath, 'utf-8'); } catch { return ''; } } exports.getGrowthBookConfigToml = getGrowthBookConfigToml; /** * Gets a valid GrowthBook config or null * @param {string} profileKey The desired profile in the GrowthBook config * @return {GrowthBookCLIConfig | null} Valid GrowthBook config. If no valid config found, this will be null. */ function getGrowthBookProfileConfig(profileKey) { try { const filePath = (0, file_1.getGrowthBookConfigFilePath)(); const configText = Fs.readFileSync(filePath, 'utf-8'); const config = toml.parse(configText); const profile = config[profileKey]; if (!profile) { return null; } const apiKey = profile.growthbook_secret; if (!apiKey) { return null; } let apiBaseUrl = profile.api_base_url; if (!apiBaseUrl) { apiBaseUrl = constants_1.DEFAULT_GROWTHBOOK_BASE_URL; } return { apiKey, apiBaseUrl, }; } catch { return null; } } exports.getGrowthBookProfileConfig = getGrowthBookProfileConfig; /** * Will return do standard configuration missing messaging if there's an error calling {@link getGrowthBookProfileConfig} * @param profileKey {string} * @param command {Command} * @return {GrowthBookCLIConfig | null} Valid GrowthBook config. If no valid config found, this will be null but the application would display an error and exit. */ function getGrowthBookProfileConfigAndThrowForCommand(profileKey, command) { const config = getGrowthBookProfileConfig(profileKey); if (!config) { if (profileKey === constants_1.DEFAULT_GROWTHBOOK_PROFILE) { // Default profile command.error('💥 Invalid GrowthBook config. Configure the CLI with the following command:\n\n $ growthbook auth login'); } else { // User is trying to use a custom profile command.error(`💥 Cannot find config for profile '${profileKey}'. Configure the CLI with the following command:\n\n $ growthbook auth login`); } } return config; } exports.getGrowthBookProfileConfigAndThrowForCommand = getGrowthBookProfileConfigAndThrowForCommand;