contentful-migration
Version:
Migration tooling for contentful
145 lines • 7.22 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.EntryDeriveAction = void 0;
const action_1 = require("./action");
const is_defined_1 = __importDefault(require("../utils/is-defined"));
const _ = __importStar(require("lodash"));
const should_publish_local_changes_1 = __importDefault(require("../utils/should-publish-local-changes"));
class EntryDeriveAction extends action_1.APIAction {
constructor(contentTypeId, entryDerivation) {
super();
this.contentTypeId = contentTypeId;
this.fromFields = entryDerivation.from;
this.referenceField = entryDerivation.toReferenceField;
this.derivedContentType = entryDerivation.derivedContentType;
this.deriveEntryForLocale = entryDerivation.deriveEntryForLocale;
this.identityKey = entryDerivation.identityKey;
this.shouldPublish = (0, is_defined_1.default)(entryDerivation.shouldPublish)
? entryDerivation.shouldPublish
: true;
this.useLocaleBasedPublishing = (0, is_defined_1.default)(entryDerivation.useLocaleBasedPublishing)
? entryDerivation.useLocaleBasedPublishing
: false;
}
async publishEntry(api, entry, locales) {
if (this.useLocaleBasedPublishing) {
// Publish only locales that were already published if shouldPublish is 'preserve'
const localesToPublish = this.shouldPublish === 'preserve' && entry.fieldStatus
? Object.entries(entry.fieldStatus['*'])
.filter(([, status]) => status === 'published')
.map(([locale]) => locale)
: locales;
await api.localeBasedPublishEntry(entry.id, localesToPublish);
}
else {
await api.publishEntry(entry.id);
}
}
async applyTo(api) {
const entries = await api.getEntriesForContentType(this.contentTypeId);
const locales = await api.getLocalesForSpace();
const sourceContentType = await api.getContentType(this.contentTypeId);
for (const entry of entries) {
const inputs = _.pick(entry.fields, this.fromFields);
const newEntryId = await this.identityKey(inputs);
const hasEntry = await api.hasEntry(newEntryId);
let skipEntry = true;
let fieldsForTargetEntry = {};
for (const locale of locales) {
let outputsForCurrentLocale;
try {
outputsForCurrentLocale = await this.deriveEntryForLocale(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;
}
if (!hasEntry) {
// TODO: How do we handle already existing links?
// Usually you would not want to derive the contents again
// But what if the previous round may not have been complete
// for example one optional field was missing in the previous iteration
const targetEntry = await api.createEntry(this.derivedContentType, 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)) {
await this.publishEntry(api, targetEntry, locales);
}
}
const field = sourceContentType.fields.getField(this.referenceField);
entry.setField(this.referenceField, {});
for (const locale of locales) {
const sys = {
type: 'Link',
linkType: 'Entry',
id: newEntryId
};
const fieldValue = field.type === 'Array' ? [{ sys }] : { sys };
entry.setFieldForLocale(this.referenceField, locale, fieldValue);
}
await api.saveEntry(entry.id);
if ((0, should_publish_local_changes_1.default)(this.shouldPublish, entry, this.useLocaleBasedPublishing)) {
await this.publishEntry(api, entry, locales);
}
}
}
}
exports.EntryDeriveAction = EntryDeriveAction;
//# sourceMappingURL=entry-derive.js.map