growthbook
Version:
The GrowthBook command-line interface (CLI) for working with the GrowthBook A/B testing, feature flagging, and experimentation platform
79 lines (78 loc) • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = require("node:fs");
const Path = require("node:path");
const core_1 = require("@oclif/core");
const config_1 = require("../../utils/config");
const templating_1 = require("../../utils/templating");
const constants_1 = require("../../utils/constants");
const cli_1 = require("../../utils/cli");
const feature_1 = require("../../utils/feature");
class GenerateTypes extends core_1.Command {
async run() {
const { flags: { output, filename, apiBaseUrl, profile, project, } } = await this.parse(GenerateTypes);
core_1.ux.action.start('Getting GrowthBook config');
const profileUsed = profile || constants_1.DEFAULT_GROWTHBOOK_PROFILE;
const config = (0, config_1.getGrowthBookProfileConfigAndThrowForCommand)(profileUsed, this);
const baseUrlUsed = apiBaseUrl || config.apiBaseUrl || constants_1.DEFAULT_GROWTHBOOK_BASE_URL;
core_1.ux.action.stop(cli_1.Icons.checkmark);
const { apiKey } = config;
try {
core_1.ux.action.start('Fetching features');
const features = await (0, feature_1.fetchAllPaginatedFeatures)(baseUrlUsed, apiKey, project);
const typeScriptOutput = (0, templating_1.getCompiledTypeScriptTemplateForFeatures)(features);
core_1.ux.action.stop(cli_1.Icons.checkmark);
let outputPath = output;
if (!outputPath) {
outputPath = Path.resolve(process.cwd(), constants_1.DEFAULT_GROWTHBOOK_TYPES_DESTINATION);
if (!Fs.existsSync(outputPath)) {
core_1.ux.action.start('Creating output directory');
Fs.mkdirSync(outputPath);
Fs.writeFileSync(outputPath + '/.gitkeep', '');
core_1.ux.action.stop(`${cli_1.Icons.checkmark} Created directory ${outputPath}`);
}
}
let outputFilename = constants_1.GROWTHBOOK_APP_FEATURES_FILENAME;
if (filename) {
outputFilename = filename.endsWith('.ts') ? filename : filename + '.ts';
}
this.writeTypeScriptFile(outputPath, outputFilename, typeScriptOutput);
}
catch (error) {
this.error('💥 There was an error fetching the features' + error);
}
}
writeTypeScriptFile(outputPath, outputFilename, typeScriptContents) {
core_1.ux.action.start('Writing types to disk');
try {
const fullyQualifiedPath = Path.resolve(process.cwd(), outputPath);
Fs.writeFileSync(fullyQualifiedPath + '/' + outputFilename, typeScriptContents);
core_1.ux.action.stop(cli_1.Icons.checkmark);
this.log(`${cli_1.Icons.checkmark} Successfully wrote TypeScript definitions to ${fullyQualifiedPath}`);
}
catch (error) {
this.error('💥 Could not write TypeScript definition file to disk' + error);
}
}
}
exports.default = GenerateTypes;
GenerateTypes.description = 'Generate TypeScript types for all your features';
GenerateTypes.examples = [];
GenerateTypes.flags = {
...cli_1.baseGrowthBookCliFlags,
output: core_1.Flags.string({
char: 'o',
description: `Output path for the ${constants_1.GROWTHBOOK_APP_FEATURES_FILENAME} file. All directories in this path should exist. If not provided, the directory ${constants_1.DEFAULT_GROWTHBOOK_TYPES_DESTINATION} will be created in the current working directory.`,
required: false,
}),
filename: core_1.Flags.string({
char: 'f',
description: `Output filename for the generated types. If not provided, the filename ${constants_1.GROWTHBOOK_APP_FEATURES_FILENAME} will be used.`,
required: false,
}),
project: core_1.Flags.string({
description: 'Project ID filter',
required: false,
}),
};
GenerateTypes.args = {};