contentful-migration
Version:
Migration tooling for contentful
158 lines • 8.06 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntryTransformToTypeAction = void 0;
const should_publish_local_changes_1 = __importDefault(require("../utils/should-publish-local-changes"));
const action_1 = require("./action");
const _ = __importStar(require("lodash"));
class EntryTransformToTypeAction extends action_1.APIAction {
constructor(entryTransformation) {
super();
this.fromFields = entryTransformation.from;
this.sourceContentTypeId = entryTransformation.sourceContentType;
this.targetContentTypeId = entryTransformation.targetContentType;
this.entryIds = entryTransformation.entryIds;
this.identityKey = entryTransformation.identityKey;
this.shouldPublish = entryTransformation.shouldPublish || false;
this.removeOldEntries = entryTransformation.removeOldEntries || false;
this.updateReferences = entryTransformation.updateReferences || false;
this.transformEntryForLocale = entryTransformation.transformEntryForLocale;
this.useLocaleBasedPublishing = entryTransformation.useLocaleBasedPublishing || false;
}
async applyTo(api) {
const allEntries = await api.getEntriesForContentType(this.sourceContentTypeId);
const entries = this.entryIds
? allEntries.filter((entry) => this.entryIds.includes(entry.id))
: allEntries;
const locales = await api.getLocalesForSpace();
for (const entry of entries) {
const inputs = this.fromFields ? _.pick(entry.fields, this.fromFields) : entry.fields;
const newEntryId = await this.identityKey(inputs);
const hasEntry = await api.hasEntry(newEntryId);
if (hasEntry) {
await api.recordRuntimeError(new Error(`Entry with id '${newEntryId}' already exists`));
continue;
}
let skipEntry = true;
let fieldsForTargetEntry = {};
for (const locale of locales) {
let outputsForCurrentLocale;
try {
outputsForCurrentLocale = await this.transformEntryForLocale(inputs, locale, {
id: entry.id
});
}
catch (err) {
await api.recordRuntimeError(err);
continue;
}
if (outputsForCurrentLocale === undefined) {
continue;
}
skipEntry = false;
// we collect all the values for the target entry before writing it to the
// offline API because we don't yet know if the entry might be skipped
// TODO: verify that the derivedFields actually get written to
// and to no other field
for (const [fieldId, localizedValue] of _.entries(outputsForCurrentLocale)) {
if (!fieldsForTargetEntry[fieldId]) {
fieldsForTargetEntry[fieldId] = {};
}
fieldsForTargetEntry[fieldId][locale] = localizedValue;
}
}
// if derive returned undefined for all locales of this entry, there are no changes
// to be made, neither on the source nor the target entry, so we move on to the next
if (skipEntry) {
continue;
}
const targetEntry = await api.createEntry(this.targetContentTypeId, newEntryId);
// we are not skipping this source entry and the target entry does not yet exist,
// so now is the time to write the collected target entry values to the offline API
for (const [fieldId, localizedField] of _.entries(fieldsForTargetEntry)) {
if (!targetEntry.fields[fieldId]) {
targetEntry.setField(fieldId, {});
}
for (const [locale, localizedValue] of _.entries(localizedField)) {
targetEntry.setFieldForLocale(fieldId, locale, localizedValue);
}
}
await api.saveEntry(targetEntry.id);
if ((0, should_publish_local_changes_1.default)(this.shouldPublish, entry, this.useLocaleBasedPublishing)) {
if (this.useLocaleBasedPublishing) {
const localesToPublish = this.shouldPublish === 'preserve'
? Object.entries(entry.fieldStatus['*'])
.filter(([, status]) => status === 'published')
.map(([locale]) => locale)
: locales;
await api.localeBasedPublishEntry(targetEntry.id, localesToPublish);
}
else {
await api.publishEntry(targetEntry.id);
}
}
// look for entries linking to the old entry and replace them with references to the new entry
if (this.updateReferences) {
const links = await api.getLinks(entry.id, locales);
for (const link of links) {
if (!link.isInArray()) {
link.element.setFieldForLocale(link.field, link.locale, {
sys: { id: newEntryId, type: 'Link', linkType: 'Entry' }
});
}
else {
link.element.replaceArrayLinkForLocale(link.field, link.locale, link.index, newEntryId);
}
await api.saveEntry(link.element.id);
if ((0, should_publish_local_changes_1.default)(this.shouldPublish, link.element, this.useLocaleBasedPublishing)) {
if (this.useLocaleBasedPublishing) {
const localesToPublish = this.shouldPublish === 'preserve'
? Object.entries(link.element.fieldStatus['*'])
.filter(([, status]) => status === 'published')
.map(([locale]) => locale)
: locales;
await api.localeBasedPublishEntry(link.element.id, localesToPublish);
}
else {
await api.publishEntry(link.element.id);
}
}
}
}
// remove the original item
if (this.removeOldEntries) {
if (entry.isPublished) {
await api.unpublishEntry(entry.id);
}
await api.deleteEntry(entry.id);
}
}
}
}
exports.EntryTransformToTypeAction = EntryTransformToTypeAction;
//# sourceMappingURL=entry-transform-to-type.js.map