UNPKG

contentful-migration

Version:
93 lines 4.25 kB
"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.EntryTransformAction = 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 EntryTransformAction extends action_1.APIAction { constructor(contentTypeId, fromFields, transformation, shouldPublish = 'preserve', useLocaleBasedPublishing = false) { super(); this.contentTypeId = contentTypeId; this.fromFields = fromFields; // this.toFields = toFields this.transformEntryForLocale = transformation; this.shouldPublish = shouldPublish; this.useLocaleBasedPublishing = useLocaleBasedPublishing; } async applyTo(api) { const entries = await api.getEntriesForContentType(this.contentTypeId); const locales = await api.getLocalesForSpace(); for (const entry of entries) { const inputs = _.pick(entry.fields, this.fromFields); let changesForThisEntry = false; 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; } changesForThisEntry = true; // TODO verify that the toFields actually get written to // and to no other field Object.keys(outputsForCurrentLocale).forEach((fieldId) => { if (!entry.fields[fieldId]) { entry.setField(fieldId, {}); } entry.setFieldForLocale(fieldId, locale, outputsForCurrentLocale[fieldId]); }); } if (changesForThisEntry) { await api.saveEntry(entry.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(entry.id, localesToPublish); } else { await api.publishEntry(entry.id); } } } } } } exports.EntryTransformAction = EntryTransformAction; //# sourceMappingURL=entry-transform.js.map