contentful-migration
Version:
Migration tooling for contentful
70 lines • 2.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = __importDefault(require("./errors"));
const isSameEntity = (step, otherStep) => {
return (step.payload.contentTypeId === otherStep.payload.contentTypeId &&
step.payload.fieldId === otherStep.payload.fieldId);
};
const getErrorMsgParams = (step) => {
const type = step.type.split('/')[0];
const params = {
field: {
type: 'field',
id: step.payload.fieldId
},
contentType: {
type: 'content type',
id: step.payload.contentTypeId
},
tag: {
type: 'tag',
id: step.payload.tagId
}
};
return params[type];
};
const getDuplicateProps = (step, chunk, stepIndex) => {
return Object.keys(step.payload.props).reduce((duplicateProps, prop) => {
const isDuplicate = chunk.slice(0, stepIndex).some((stepBefore) => {
return (stepBefore.payload.props &&
isSameEntity(step, stepBefore) &&
stepBefore.payload.props.hasOwnProperty(prop));
});
if (isDuplicate) {
duplicateProps.push(prop);
}
return duplicateProps;
}, []);
};
const getErrorsForIntents = (intents) => {
return intents.reduce((intentErrors, intent, stepIndex) => {
const step = intent.toRaw();
if (step.payload.props) {
const duplicateProps = getDuplicateProps(step, intents, stepIndex);
const errors = duplicateProps.map((duplicateProp) => {
const { type, id } = getErrorMsgParams(step);
return {
type: 'InvalidAction',
message: errors_1.default.generic.DUPLICATE_PROP(duplicateProp, type, id),
details: { intent }
};
});
intentErrors = intentErrors.concat(errors);
}
return intentErrors;
}, []);
};
function default_1(intentList) {
const intents = intentList.compressed().getIntents();
return intents.reduce((errors, intent) => {
if (intent.isComposedIntent()) {
return errors.concat(getErrorsForIntents(intent.getIntents()));
}
return errors;
}, []);
}
exports.default = default_1;
//# sourceMappingURL=duplicate-props.js.map