UNPKG

@becklyn/contentful-adapter

Version:

[![CI](https://github.com/Becklyn-Studios/contentful-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/Becklyn-Studios/contentful-adapter/actions/workflows/ci.yml)

45 lines (44 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeMigrations = void 0; const migration_1 = require("./migration"); const api_1 = require("../contentful/api"); const contentful_migration_1 = require("contentful-migration"); const executeMigrations = async (clientConfig, allMigrations) => { const environment = await (0, api_1.connectToContentfulManagementApi)(clientConfig); const locale = await (0, api_1.getDefaultLocale)(environment); const executedMigrations = await (0, api_1.getExecutedMigrations)(environment, locale); const filteredMigrations = allMigrations.filter(migration => !executedMigrations.includes(migration.key)); await runMigrations(filteredMigrations, environment, locale, clientConfig); }; exports.executeMigrations = executeMigrations; const runMigrations = async (migrations, environment, locale, config) => { console.log(`Running ${migrations.length} migrations`); for (let i = 0; i < migrations.length; i++) { await runSingleMigration(migrations[i], environment, locale, config); } console.log(`All migrations were successful`); }; const runSingleMigration = async (migration, environment, locale, config) => { console.log(`Running migration "${migration.key}"`); await (0, contentful_migration_1.runMigration)({ migrationFunction: migration.migration, spaceId: config.spaceId, accessToken: config.management.accessToken, environmentId: config.environmentId, yes: true, }); console.log(`Migration "${migration.key}" succeeded`); const newVersionEntry = await environment.createEntry(migration_1.MIGRATIONS_MODEL_NAME, { fields: { version: { [locale]: migration.key, }, executedAt: { [locale]: new Date().toString(), }, }, }); await newVersionEntry.publish(); console.log(`Saved ${migration.key} to ${migration_1.MIGRATIONS_MODEL_NAME}`); };