UNPKG

growthbook

Version:

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

105 lines (104 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const Fs = require("node:fs"); const toml = require("@iarna/toml"); const file_1 = require("../../utils/file"); const constants_1 = require("../../utils/constants"); const cli_1 = require("../../utils/cli"); class Login extends core_1.Command { async run() { const apiKey = await core_1.ux.prompt('What is your GrowthBook secret API Key?', { type: 'hide', required: true, }); if (!apiKey) { this.error(`${cli_1.Icons.xSymbol} You must provide a GrowthBook secret API key to continue`); } let profile = await core_1.ux.prompt(`What is the name of this profile? You can leave this blank (default: ${constants_1.DEFAULT_GROWTHBOOK_PROFILE})`, { required: false, }); if (!profile) { profile = constants_1.DEFAULT_GROWTHBOOK_PROFILE; } let apiBaseUrl = await core_1.ux.prompt(`What is the API base URL of the GrowthBook instance? If using GrowthBook cloud, you can leave this blank (e.g. http://localhost:3100, default: ${constants_1.DEFAULT_GROWTHBOOK_BASE_URL})`, { required: false, }); if (!apiBaseUrl) { apiBaseUrl = constants_1.DEFAULT_GROWTHBOOK_BASE_URL; } this.writeApiKeyToGrowthBookConfig(apiKey, profile, apiBaseUrl); } /** * Writes the API key to the config file. Will throw errors if it cannot read or write to the file * @param {string} apiKey The GrowthBook secret * @param {string} profile The profile to write in the config * @param {string} apiBaseUrl The base URL of the GrowthBook API * @return void */ writeApiKeyToGrowthBookConfig(apiKey, profile, apiBaseUrl) { const configText = this.getGrowthBookConfigFileContents(); const config = toml.parse(configText); if (config[profile]) { this.log(`Overwriting existing configuration for the '${profile}' profile`); } config[profile] = { growthbook_secret: apiKey, api_base_url: apiBaseUrl, }; const stringified = toml.stringify(config); this.writeConfigFile(stringified); } /** * Finds the GrowthBook config.toml file in ~/.growthbook/config.toml * If the directory doesn't exist, it will be created. * If the directory cannot be created, the program will exit. * @returns {string} Contents of the config.toml file or an empty string */ getGrowthBookConfigFileContents() { const growthBookDirectory = (0, file_1.getGrowthBookConfigDirectory)(); // Create the ~/.growthbook directory if it doesn't exist try { if (!Fs.existsSync(growthBookDirectory)) { Fs.mkdirSync(growthBookDirectory); return ''; } } catch (error) { this.error('💥 Cannot create ~/.growthbook directory \n' + error); } // Read the ~/.growthbook/config.toml file const configFilePath = growthBookDirectory + '/config.toml'; try { if (!Fs.existsSync(configFilePath)) { this.log('Created ~/.growthbook/config.toml'); Fs.writeFileSync(configFilePath, ''); return ''; } } catch (error) { this.error(`💥 Cannot create file ${configFilePath} \n` + error); } try { return Fs.readFileSync(configFilePath, 'utf-8'); } catch (error) { this.error(`💥 Cannot read file ${configFilePath} \n` + error); } } writeConfigFile(fileContents) { const configFilePath = (0, file_1.getGrowthBookConfigFilePath)(); try { Fs.writeFileSync(configFilePath, fileContents); this.log(`The GrowthBook config has been written at ~/.growthbook/config.toml ${cli_1.Icons.checkmark}`); } catch (error) { this.error(`💥 Cannot write to file at ${configFilePath} \n` + error); } } } exports.default = Login; Login.description = 'Configure your API key with the GrowthBook SDK with your project'; Login.examples = []; Login.flags = {}; Login.args = {};