UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

137 lines 6.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = run; /* eslint-disable @typescript-eslint/no-explicit-any */ const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const picocolors_1 = __importDefault(require("picocolors")); const prompts_1 = require("@inquirer/prompts"); const migration_1 = require("../utils/migration"); const api_1 = require("../utils/api"); const jiti_1 = require("jiti"); const handlers_1 = require("../handlers"); // Initialize jiti with the current working directory const jiti = (0, jiti_1.createJiti)(process.cwd()); /** * Runs a migration file based on the provided file path and options. * This function performs the following operations: * 1. Applies rate limiting to the API requests * 2. If no file path is provided, prompts the user to select a migration * 3. Resolves and loads the migration file * 4. Executes the migration based on the migration type * * @param {string} [filePath] - The path to the migration file to run. If not provided, will prompt interactively. * @param {RunOptions} [options] - Configuration options for the migration run * @param {number} [options.throttle] - The throttle time in milliseconds between API requests (default: 300ms) * @param {boolean} [options.dryRun] - Whether to perform a dry run of the migration (default: false) * @param {boolean} [options.publish] - Whether to publish the migration (default: false) * @param {string[]} [options.languages] - The languages to publish the migration to (default: all languages) */ async function run(filePath, options = {}) { // Apply rate limiting if provided if (options.throttle && options.throttle > 0) { // Convert throttle milliseconds to requests per second // e.g., 500ms throttle = 2 requests per second const requestsPerSecond = 1000 / options.throttle; console.log(`${picocolors_1.default.blue("-")} Setting API rate limit to ${requestsPerSecond.toFixed(2)} requests per second (${options.throttle}ms between requests)`); (0, api_1.setRequestsPerSecond)(requestsPerSecond); } else { // Default to a safe rate limit of 3 requests per second (0, api_1.setRequestsPerSecond)(3); } // If no file path is provided, let the user select from available migrations if (!filePath) { const migrations = await (0, migration_1.findMigrations)(); if (migrations.length === 0) { console.error(`${picocolors_1.default.red("✗")} No migrations found in migrations directory`); process.exit(1); } filePath = await (0, prompts_1.select)({ message: "Select a migration to run:", choices: migrations.map((migration) => ({ name: migration, value: migration, })), }); filePath = path_1.default.join("migrations", filePath); } // Resolve and load the migration file const resolvedPath = path_1.default.resolve(process.cwd(), filePath); console.log(`${picocolors_1.default.blue("-")} Loading migration from ${resolvedPath}`); if (!fs_1.default.existsSync(resolvedPath)) { console.error(`${picocolors_1.default.red("✗")} Migration file not found: ${resolvedPath}`); process.exit(1); } try { // Use jiti to load the migration file const migrationModule = (await jiti.import(resolvedPath)); const migration = migrationModule.default || migrationModule; // Common options for all migration types const migrationOptions = { isDryrun: options.dryRun, publish: options.publish, publishLanguages: options.languages, }; try { switch (migration.type) { case "create-component-group": await (0, handlers_1.handleCreateComponentGroup)(migration, migrationOptions); break; case "update-component-group": await (0, handlers_1.handleUpdateComponentGroup)(migration, migrationOptions); break; case "delete-component-group": await (0, handlers_1.handleDeleteComponentGroup)(migration, migrationOptions); break; case "create-component": await (0, handlers_1.handleCreateComponent)(migration, migrationOptions); break; case "update-component": await (0, handlers_1.handleUpdateComponent)(migration, migrationOptions); break; case "delete-component": await (0, handlers_1.handleDeleteComponent)(migration, migrationOptions); break; case "create-story": await (0, handlers_1.handleCreateStory)(migration, migrationOptions); break; case "update-story": await (0, handlers_1.handleUpdateStory)(migration, migrationOptions); break; case "delete-story": await (0, handlers_1.handleDeleteStory)(migration, migrationOptions); break; case "create-datasource": await (0, handlers_1.handleCreateDatasource)(migration, migrationOptions); break; case "update-datasource": await (0, handlers_1.handleUpdateDatasource)(migration, migrationOptions); break; case "delete-datasource": await (0, handlers_1.handleDeleteDatasource)(migration, migrationOptions); break; case "transform-entries": await (0, handlers_1.handleTransformEntries)(migration, migrationOptions); break; default: console.error(`${picocolors_1.default.red("✗")} Unknown migration type: ${migration.type}`); process.exit(1); } } catch (error) { // This is a migration error, log it and exit console.error(`${picocolors_1.default.red("✗")} Migration failed:`, error); process.exit(1); } } catch (error) { // This is an import error, log it and exit console.error(`${picocolors_1.default.red("✗")} Failed to load migration:`, error); process.exit(1); } } //# sourceMappingURL=run.js.map