contentful-migration
Version:
Migration tooling for contentful
376 lines • 19.3 kB
JavaScript
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.migration = void 0;
const bluebird_1 = __importDefault(require("bluebird"));
const action_creators_1 = __importDefault(require("./action-creators"));
const first_external_caller_1 = __importDefault(require("./first-external-caller"));
const dispatch_proxy_1 = __importDefault(require("./dispatch-proxy"));
const lodash_1 = require("lodash");
const deprecated_1 = require("../utils/deprecated");
const createInstanceIdManager = () => {
const instanceCounts = {};
return {
getNew: (id) => {
let instanceId;
if (typeof instanceCounts[id] === 'undefined') {
instanceId = 0;
}
else {
instanceId = instanceCounts[id] + 1;
}
instanceCounts[id] = instanceId;
return instanceId;
}
};
};
class Movement extends dispatch_proxy_1.default {
}
class Field extends dispatch_proxy_1.default {
constructor(contentType, id, props = {}, { dispatchUpdate }) {
super({ dispatchUpdate });
this.contentType = contentType;
this.id = id;
// Initialize from second argument
Object.keys(props).forEach((propertyName) => {
this[propertyName](props[propertyName]);
});
}
setAnnotations(annotationIds, fieldAnnotationPayload) {
const callsite = (0, first_external_caller_1.default)();
const fieldInstanceId = this.contentType.fieldInstanceIds.getNew(this.id);
this.contentType.dispatch(action_creators_1.default.field.setAnnotations(this.contentType.id, this.contentType.instanceId, this.id, fieldInstanceId, callsite, annotationIds, fieldAnnotationPayload));
return this;
}
clearAnnotations() {
const callsite = (0, first_external_caller_1.default)();
const fieldInstanceId = this.contentType.fieldInstanceIds.getNew(this.id);
this.contentType.dispatch(action_creators_1.default.field.setAnnotations(this.contentType.id, this.contentType.instanceId, this.id, fieldInstanceId, callsite, undefined));
return this;
}
}
class EditorLayout extends dispatch_proxy_1.default {
constructor(contentTypeId, instanceId, dispatch) {
super({
dispatchUpdate: (callsite, propertyName) => {
this.dispatch(action_creators_1.default.editorLayout.callInvalidEditorLayoutMethod(this.contentTypeId, this.instanceId, callsite, propertyName));
}
});
this.contentTypeId = contentTypeId;
this.instanceId = instanceId;
this.dispatch = dispatch;
}
createFieldGroup(fieldGroupId, init) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.editorLayout.createFieldGroup(this.contentTypeId, this.instanceId, callsite, fieldGroupId));
return this.editFieldGroup(fieldGroupId, init);
}
changeFieldGroupId(fieldGroupId, newFieldGroupId) {
const callsite = (0, first_external_caller_1.default)();
return this.dispatch(action_creators_1.default.editorLayout.changeFieldGroupId(this.contentTypeId, this.instanceId, fieldGroupId, newFieldGroupId, callsite));
}
editFieldGroup(fieldGroupId, init) {
const updateFieldGroup = action_creators_1.default.editorLayout.updateFieldGroup.bind(null, this.contentTypeId, this.instanceId, fieldGroupId);
return new EditorLayoutFieldGroup(this.contentTypeId, this.instanceId, fieldGroupId, this.dispatch, init, {
dispatchUpdate: (callsite, property, value) => {
return this.dispatch(updateFieldGroup(callsite, property, value));
}
});
}
moveField(fieldId, fieldGroupId) {
const movement = new Movement({
dispatchUpdate: (callsite, property, value) => {
const action = action_creators_1.default.editorLayout.moveField(this.contentTypeId, this.instanceId, fieldId, fieldGroupId, { direction: property, pivot: value }, callsite);
this.dispatch(action);
}
});
return movement;
}
deleteFieldGroup(fieldGroupId) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.editorLayout.deleteEditorLayoutFieldGroup(this.contentTypeId, this.instanceId, callsite, fieldGroupId));
}
changeFieldGroupControl(fieldGroupId, widgetNamespace, widgetId, settings) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.editorLayout.changeFieldGroupControl(this.contentTypeId, this.instanceId, fieldGroupId, callsite, {
widgetId,
widgetNamespace,
settings
}));
}
}
class EditorLayoutFieldGroup extends dispatch_proxy_1.default {
constructor(contentTypeId, instanceId, fieldGroupId, dispatch, props = {}, { dispatchUpdate }) {
super({ dispatchUpdate });
this.contentTypeId = contentTypeId;
this.instanceId = instanceId;
this.fieldGroupId = fieldGroupId;
this.dispatch = dispatch;
// Initialize properties by delegating to dispatchUpdate
Object.keys(props).forEach((propertyName) => {
this[propertyName](props[propertyName]);
});
}
createFieldGroup(fieldGroupId, init) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.editorLayout.createFieldGroup(this.contentTypeId, this.instanceId, callsite, fieldGroupId, this.fieldGroupId));
const updateFieldGroup = action_creators_1.default.editorLayout.updateFieldGroup.bind(null, this.contentTypeId, this.instanceId, fieldGroupId);
return new EditorLayoutFieldGroup(this.contentTypeId, this.instanceId, fieldGroupId, this.dispatch, init, {
dispatchUpdate: (callsite, property, value) => {
return this.dispatch(updateFieldGroup(callsite, property, value));
}
});
}
}
class ContentType extends dispatch_proxy_1.default {
constructor(id, instanceId, props = {}, dispatch) {
const dispatchUpdate = (callsite, propertyName, propertyValue) => {
dispatch(action_creators_1.default.contentType.update(id, instanceId, callsite, propertyName, propertyValue));
};
super({ dispatchUpdate });
this.id = id;
this.instanceId = instanceId;
this.dispatch = dispatch;
this.fieldInstanceIds = createInstanceIdManager();
// Initialize from pros
Object.keys(props).forEach((propertyName) => {
this[propertyName](props[propertyName]);
});
}
setAnnotations(annotationIds) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.setAnnotations(this.id, this.instanceId, callsite, annotationIds));
return this;
}
clearAnnotations() {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.setAnnotations(this.id, this.instanceId, callsite, undefined));
return this;
}
setTaxonomyValidations(taxonomyValidations) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.setTaxonomyValidations(this.id, this.instanceId, callsite, taxonomyValidations));
return this;
}
addTaxonomyValidation(id, linkType, options = {}) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.addTaxonomyValidation(this.id, this.instanceId, callsite, id, linkType, options));
return this;
}
clearTaxonomyValidations() {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.clearTaxonomyValidations(this.id, this.instanceId, callsite));
return this;
}
createField(id, init) {
const callsite = (0, first_external_caller_1.default)();
const fieldInstanceId = this.fieldInstanceIds.getNew(id);
this.dispatch(action_creators_1.default.field.create(this.id, this.instanceId, id, fieldInstanceId, callsite));
const updateField = action_creators_1.default.field.update.bind(null, this.id, this.instanceId, id, fieldInstanceId);
const field = new Field(this, id, init, {
dispatchUpdate: (callsite, property, value) => {
return this.dispatch(updateField(callsite, property, value));
}
});
return field;
}
editField(id, init) {
const fieldInstanceId = this.fieldInstanceIds.getNew(id);
const updateField = action_creators_1.default.field.update.bind(null, this.id, this.instanceId, id, fieldInstanceId);
const field = new Field(this, id, init, {
dispatchUpdate: (callsite, property, value) => {
return this.dispatch(updateField(callsite, property, value));
}
});
return field;
}
moveField(id) {
const fieldInstanceId = this.fieldInstanceIds.getNew(id);
const contentTypeId = this.id;
const contentTypeInstanceId = this.instanceId;
const movement = new Movement({
dispatchUpdate: (callsite, property, value) => {
const action = action_creators_1.default.field.move(contentTypeId, contentTypeInstanceId, id, fieldInstanceId, callsite, { direction: property, pivot: value });
this.dispatch(action);
}
});
return movement;
}
deleteField(id) {
const callsite = (0, first_external_caller_1.default)();
const fieldInstanceId = this.fieldInstanceIds.getNew(id);
this.dispatch(action_creators_1.default.field.delete(this.id, this.instanceId, id, fieldInstanceId, callsite));
}
changeFieldId(oldId, newId) {
const callsite = (0, first_external_caller_1.default)();
const fieldInstanceId = this.fieldInstanceIds.getNew(oldId);
this.dispatch(action_creators_1.default.field.rename(this.id, this.instanceId, oldId, fieldInstanceId, callsite, newId));
}
changeFieldControl(fieldId, widgetNamespace, widgetId, settings) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.changeEditorInterface(this.id, this.instanceId, callsite, fieldId, widgetId, settings, widgetNamespace));
return this;
}
/** deprecated, use changeFieldControl instead */
changeEditorInterface(fieldId, widgetId, settings, widgetNamespace) {
(0, deprecated_1.deprecatedMethod)('changeEditorInterface', 'changeFieldControl');
return this.changeFieldControl(fieldId, widgetNamespace, widgetId, settings);
}
copyFieldControl(sourceFieldId, destinationFieldId) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.copyEditorInterface(this.id, this.instanceId, callsite, sourceFieldId, destinationFieldId));
return this;
}
/** deprecated, use copyFieldControl instead */
copyEditorInterface(sourceFieldId, destinationFieldId) {
(0, deprecated_1.deprecatedMethod)('copyEditorInterface', 'copyFieldControl');
return this.copyFieldControl(sourceFieldId, destinationFieldId);
}
resetFieldControl(fieldId) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.resetEditorInterface(this.id, this.instanceId, callsite, fieldId));
return this;
}
/** deprecated, use resetFieldControl instead */
resetEditorInterface(fieldId) {
(0, deprecated_1.deprecatedMethod)('resetEditorInterface', 'resetFieldControl');
return this.resetFieldControl(fieldId);
}
resetEntryEditor() {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.resetEntryEditor(this.id, this.instanceId, callsite));
return this;
}
configureEntryEditor(widgetNamespace, widgetId, settings) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.configureEntryEditor(this.id, this.instanceId, callsite, widgetNamespace, widgetId, settings));
return this;
}
configureEntryEditors(editors) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.configureEntryEditors(this.id, this.instanceId, callsite, editors));
return this;
}
addSidebarWidget(widgetNamespace, widgetId, settings = {}, insertBeforeWidgetId = null) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.addSidebarWidget(this.id, this.instanceId, callsite, widgetId, widgetNamespace, insertBeforeWidgetId, settings));
return this;
}
updateSidebarWidget(widgetNamespace, widgetId, settings = {}) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.updateSidebarWidget(this.id, this.instanceId, callsite, widgetId, widgetNamespace, settings));
return this;
}
removeSidebarWidget(widgetNamespace, widgetId) {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.removeSidebarWidget(this.id, this.instanceId, callsite, widgetId, widgetNamespace));
return this;
}
resetSidebarToDefault() {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.resetSidebarToDefault(this.id, this.instanceId, callsite));
return this;
}
createEditorLayout() {
const callsite = (0, first_external_caller_1.default)();
this.dispatch(action_creators_1.default.contentType.createEditorLayout(this.id, this.instanceId, callsite));
return this.editEditorLayout();
}
editEditorLayout() {
return new EditorLayout(this.id, this.instanceId, this.dispatch);
}
deleteEditorLayout() {
const callsite = (0, first_external_caller_1.default)();
return this.dispatch(action_creators_1.default.contentType.deleteEditorLayout(this.id, this.instanceId, callsite));
}
}
class Tag extends dispatch_proxy_1.default {
constructor(id, instanceId, props = {}, dispatch) {
const dispatchUpdate = (callsite, propertyName, propertyValue) => {
dispatch(action_creators_1.default.tag.update(id, instanceId, callsite, propertyName, propertyValue));
};
super({ dispatchUpdate });
this.id = id;
this.instanceId = instanceId;
this.dispatch = dispatch;
// Initialize from pros
Object.keys(props).forEach((propertyName) => {
this[propertyName](props[propertyName]);
});
}
}
async function migration(migrationCreator, makeRequest, config) {
const actions = [];
const instanceIdManager = createInstanceIdManager();
const dispatch = (action) => actions.push(action);
const migration = {
createContentType: function (id, init) {
const callsite = (0, first_external_caller_1.default)();
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.contentType.create(id, instanceId, callsite));
return new ContentType(id, instanceId, init, dispatch);
},
editContentType: function (id, changes) {
const instanceId = instanceIdManager.getNew(id);
const ct = new ContentType(id, instanceId, changes, dispatch);
return ct;
},
deleteContentType: function (id) {
const callsite = (0, first_external_caller_1.default)();
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.contentType.delete(id, instanceId, callsite));
},
transformEntries: function (transformation) {
const callsite = (0, first_external_caller_1.default)();
const id = transformation.contentType;
const stripped = (0, lodash_1.omit)(transformation, 'contentType');
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.contentType.transformEntries(id, instanceId, stripped, callsite));
},
deriveLinkedEntries: function (transformation) {
const callsite = (0, first_external_caller_1.default)();
const id = transformation.contentType;
const stripped = (0, lodash_1.omit)(transformation, 'contentType');
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.contentType.deriveLinkedEntries(id, instanceId, stripped, callsite));
},
transformEntriesToType: function (transformation) {
const callsite = (0, first_external_caller_1.default)();
const stripped = (0, lodash_1.omit)(transformation, 'contentType');
const instanceId = instanceIdManager.getNew(transformation.sourceContentType);
dispatch(action_creators_1.default.contentType.transformEntriesToType(instanceId, stripped, callsite));
},
createTag: function (id, init, visibility = 'private') {
const callsite = (0, first_external_caller_1.default)();
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.tag.create(id, instanceId, callsite, visibility));
return new Tag(id, instanceId, init, dispatch);
},
editTag: function (id, changes) {
const instanceId = instanceIdManager.getNew(id);
const ct = new Tag(id, instanceId, changes, dispatch);
return ct;
},
deleteTag: function (id) {
const callsite = (0, first_external_caller_1.default)();
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.tag.delete(id, instanceId, callsite));
},
setTagsForEntries: function (transformation) {
const callsite = (0, first_external_caller_1.default)();
const id = transformation.contentType;
const stripped = (0, lodash_1.omit)(transformation, 'contentType');
const instanceId = instanceIdManager.getNew(id);
dispatch(action_creators_1.default.contentType.setTagsForEntries(id, instanceId, stripped, callsite));
}
};
// Create the migration
await bluebird_1.default.try(function () {
return migrationCreator(migration, Object.assign({ makeRequest }, config));
});
return actions;
}
exports.migration = migration;
//# sourceMappingURL=index.js.map