@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
102 lines • 5.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleUpdateComponent = void 0;
const picocolors_1 = __importDefault(require("picocolors"));
const api_1 = require("../../utils/api");
const migration_1 = require("../../utils/migration");
const lodash_1 = require("lodash");
const storyblok_1 = require("../../utils/storyblok");
/**
* Handles the update of an existing component based on the provided migration schema.
* This function performs the following operations:
* 1. Checks if the component exists before updating
* 2. Updates the component schema fields and tabs if provided
* 3. Updates other properties like display name, root status, and nestable status
* 4. Associates the component with a component group if specified
* 5. Saves the updated component to the API
* 6. Creates a rollback file for the component
*
* @param {Object} migration - The migration object containing the component schema
* @param {Partial<ComponentMigration>} migration.schema - The schema defining the component properties
* @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 component update fails
*/
const handleUpdateComponent = async (migration, options) => {
console.log(`${picocolors_1.default.blue("-")} Updating component: ${migration.schema.name}`);
if (options.isDryrun) {
console.log(`${picocolors_1.default.yellow("!")} Dry run - not updating component`);
console.log(migration);
return;
}
try {
// Check if component exists before updating
const response = await api_1.api.components.getAll();
const existingComponents = response.data.components || [];
const componentExists = existingComponents.some((c) => c.name === migration.schema.name);
if (!componentExists) {
console.error(`${picocolors_1.default.red("✗")} Component "${migration.schema.name}" not found. Cannot update a non-existent component.`);
throw new Error(`Component "${migration.schema.name}" not found`);
}
const component = await migration_1.helper.updateComponent(migration.schema.name || "");
// Store original component data for rollback
const originalComponent = (0, lodash_1.cloneDeep)(component.instance);
// Update fields if provided
if (migration.schema?.schema) {
component.addOrUpdateFields(migration.schema.schema, migration.schema.tabs
? {
general: migration.schema.tabs.general || [],
...migration.schema.tabs,
}
: undefined);
}
// Update other properties if provided
if (migration.schema.display_name) {
component.instance.display_name = migration.schema.display_name;
}
if (migration.schema.is_root !== undefined) {
component.instance.is_root = migration.schema.is_root;
}
if (migration.schema.is_nestable !== undefined) {
component.instance.is_nestable = migration.schema.is_nestable;
}
if (migration.schema.component_group_name) {
// Get component groups to find the UUID
const response = await api_1.api.componentGroups.getAll();
const componentGroups = response.data.component_groups || [];
const group = componentGroups.find((g) => g.name === migration.schema.component_group_name);
if (group) {
component.instance.component_group_uuid = group.uuid;
}
else {
console.warn(`${picocolors_1.default.yellow("!")} Component group "${migration.schema.component_group_name}" not found`);
}
}
// Save the component
await component.save();
// Create rollback data
const rollbackData = [
{
id: originalComponent.id,
name: originalComponent.name,
schema: originalComponent.schema,
display_name: originalComponent.display_name,
is_root: originalComponent.is_root,
is_nestable: originalComponent.is_nestable,
component_group_uuid: originalComponent.component_group_uuid,
},
];
// Create rollback file
await (0, storyblok_1.createRollbackFile)(rollbackData, `component_${migration.schema.name?.replace(/[^a-zA-Z0-9]/g, "_")}`, "update");
console.log(`${picocolors_1.default.green("✓")} Component updated successfully: ${migration.schema.name}`);
}
catch (error) {
console.error(`${picocolors_1.default.red("✗")} Failed to update component:`, error);
throw error;
}
};
exports.handleUpdateComponent = handleUpdateComponent;
//# sourceMappingURL=update.js.map