UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

184 lines 6.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadEnvConfig = loadEnvConfig; exports.saveConfig = saveConfig; exports.loadConfig = loadConfig; exports.verifyExistingConfig = verifyExistingConfig; exports.promptForConfig = promptForConfig; const cosmiconfig_1 = require("cosmiconfig"); const dotenv_1 = require("dotenv"); const promises_1 = require("fs/promises"); const path_1 = __importDefault(require("path")); const prompts_1 = require("@inquirer/prompts"); const picocolors_1 = __importDefault(require("picocolors")); /** * Loads environment variables from the .env file. * * @returns {Promise<Partial<StoryblokConfig>>} A promise that resolves to the loaded configuration */ async function loadEnvConfig() { (0, dotenv_1.config)(); // Load .env file return { spaceId: process.env.STORYBLOK_SPACE_ID, oauthToken: process.env.STORYBLOK_OAUTH_TOKEN, region: process.env.STORYBLOK_REGION, }; } /** * Saves the configuration to a file. * * @param {StoryblokConfig} config - The configuration to save * @returns {Promise<void>} A promise that resolves when the configuration is saved */ async function saveConfig(config) { await (0, promises_1.writeFile)(path_1.default.join(process.cwd(), ".storyblokrc.json"), JSON.stringify(config, null, 2)); } /** * Loads the configuration from the .storyblokrc.json file. * * @returns {Promise<StoryblokConfig | null>} A promise that resolves to the loaded configuration or null if not found */ async function loadConfig() { const explorer = (0, cosmiconfig_1.cosmiconfig)("storyblok"); try { const result = await explorer.search(); return result?.config || null; } catch (error) { console.error("Error loading config:", error); return null; } } /** * Verifies and potentially updates an existing Storyblok configuration. * * This function: * 1. Displays current configuration values (spaceId, oauthToken, region) * 2. Asks user if they want to use existing values * 3. If user declines or values are missing, prompts for new values * 4. Ensures all required fields are present in the final configuration * * @param config - Partial configuration object containing existing values * @returns A complete StoryblokConfig object if user confirms or updates values, null if user declines */ async function verifyExistingConfig(config) { console.log(picocolors_1.default.blue("Please verify the following options:")); if (config.spaceId) { console.log(`Space ID: ${config.spaceId}`); } if (config.oauthToken) { console.log(`OAuth Token: ${config.oauthToken}`); } if (config.region) { console.log(`Region: ${config.region}`); } const useExisting = await (0, prompts_1.confirm)({ message: "Would you like to use these values?", default: true, }); if (!useExisting) { return null; } // If we're missing any required fields, prompt for them const updatedConfig = { ...config }; if (!updatedConfig.spaceId) { updatedConfig.spaceId = await (0, prompts_1.input)({ message: "Enter your Storyblok Space ID:", validate: (value) => value.length > 0, }); } if (!updatedConfig.oauthToken) { updatedConfig.oauthToken = await (0, prompts_1.input)({ message: "Enter your Storyblok OAuth Token:", validate: (value) => value.length > 0, }); } if (!updatedConfig.region) { updatedConfig.region = await (0, prompts_1.select)({ message: "Select your Storyblok region:", choices: [ { value: "eu", name: "EU (default) - For spaces created in the EU", }, { value: "us", name: "US - For spaces created in the US", }, { value: "ap", name: "Australia - For spaces created in Australia", }, { value: "ca", name: "Canada - For spaces created in Canada", }, { value: "cn", name: "China - For spaces created in China", }, ], default: "eu", }); } return updatedConfig; } /** * Prompts the user for all required Storyblok configuration values. * * This function: * 1. Prompts for Space ID with validation * 2. Prompts for OAuth Token with validation * 3. Prompts for Region with predefined options * 4. Uses existing values as defaults if provided * * @param existing - Partial configuration object containing existing values to use as defaults * @returns A complete StoryblokConfig object with all required fields */ async function promptForConfig(existing) { const spaceId = await (0, prompts_1.input)({ message: "Enter your Storyblok Space ID:", default: existing.spaceId, validate: (value) => value.length > 0, }); const oauthToken = await (0, prompts_1.input)({ message: "Enter your Storyblok OAuth Token:", default: existing.oauthToken, validate: (value) => value.length > 0, }); const region = await (0, prompts_1.select)({ message: "Select your Storyblok region:", choices: [ { value: "eu", name: "EU (default) - For spaces created in the EU", }, { value: "us", name: "US - For spaces created in the US", }, { value: "ap", name: "Australia - For spaces created in Australia", }, { value: "ca", name: "Canada - For spaces created in Canada", }, { value: "cn", name: "China - For spaces created in China", }, ], default: existing.region || "eu", }); return { spaceId, oauthToken, region, }; } //# sourceMappingURL=config.js.map