UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

57 lines 2.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleCreateDatasource = void 0; const picocolors_1 = __importDefault(require("picocolors")); const storyblok_1 = require("../../utils/storyblok"); const api_1 = require("../../utils/api"); /** * Handles the creation of a new datasource based on the provided migration schema. * This function performs the following operations: * 1. Checks if the datasource already exists * 2. Creates a new datasource instance * 3. Adds entries to the datasource * 4. Logs the creation status * * @param {Object} migration - The migration object containing the datasource schema and entries * @param {DatasourceMigration} migration.datasource - The schema defining the datasource properties * @param {Omit<IPendingDataSourceEntry, "datasource_id">[]} [migration.entries] - Optional entries to add to the datasource * @param {RunMigrationOptions} options - Configuration options for the migration * @param {boolean} [options.isDryrun] - Whether to perform a dry run without making actual changes * @throws {Error} If the datasource creation fails */ const handleCreateDatasource = async (migration, options) => { console.log(`${picocolors_1.default.blue("-")} Creating datasource: ${migration.datasource.name}`); if (options.isDryrun) { console.log(`${picocolors_1.default.yellow("!")} Dry run - not creating datasource`); console.log(migration.datasource); return; } try { // Check if datasource already exists const response = await api_1.api.datasources.getAll(); const existingDatasources = response.data.datasources || []; const existingDatasource = existingDatasources.find((d) => d.name === migration.datasource.name || d.slug === migration.datasource.slug); if (existingDatasource) { console.log(`${picocolors_1.default.yellow("!")} Datasource already exists: ${existingDatasource.name}`); return; } const entries = migration.entries || []; const [datasource, datasourceEntries] = await (0, storyblok_1.addOrUpdateDatasource)(migration.datasource, entries.map((entry) => ({ name: entry.name, value: entry.value, dimension_value: entry.dimension_value, }))); console.log(`${picocolors_1.default.green("✓")} Datasource created successfully: ${datasource.name}`); console.log(`${picocolors_1.default.green("-")} Created ${datasourceEntries.length} entries`); } catch (error) { // Don't rethrow - let the calling function handle it console.error(`${picocolors_1.default.red("✗")} Failed to create datasource:`, error); } }; exports.handleCreateDatasource = handleCreateDatasource; //# sourceMappingURL=create.js.map