forest-express
Version:
Official package for all Forest Express Lianas
214 lines (212 loc) • 10.4 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _ = require('lodash');
var _require = require('../utils/string'),
parameterize = _require.parameterize;
var _require2 = require('../utils/json'),
prettyPrint = _require2.prettyPrint;
var SchemaFileUpdater = /*#__PURE__*/function () {
function SchemaFileUpdater(_ref) {
var logger = _ref.logger,
fs = _ref.fs;
(0, _classCallCheck2["default"])(this, SchemaFileUpdater);
this.logger = logger;
this.fs = fs;
}
(0, _createClass2["default"])(SchemaFileUpdater, [{
key: "cleanActions",
value: function cleanActions(actions) {
var _this = this;
actions = actions.filter(function (action) {
return action.name;
});
actions.forEach(function (action) {
if (action.global) {
_this.logger.warn("REMOVED OPTION: The support for Smart Action \"global\" option is now removed. Please set \"type: 'global'\" instead of \"global: true\" for the \"".concat(action.name, "\" Smart Action."));
}
if (action.type && !_.includes(['bulk', 'global', 'single'], action.type)) {
_this.logger.warn("Please set a valid Smart Action type (\"bulk\", \"global\" or \"single\") for the \"".concat(action.name, "\" Smart Action."));
action.type = null;
}
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'endpoint', "/forest/actions/".concat(parameterize(action.name)));
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'httpMethod', 'POST');
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'fields', []);
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'redirect', null);
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'baseUrl', null);
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'type', 'bulk');
SchemaFileUpdater.setDefaultValueIfNecessary(action, 'download', false);
// NOTICE: Set a position to the Smart Actions fields.
action.fields = action.fields.filter(function (field) {
return field.field;
});
_.each(action.fields, function (field, position) {
field.position = position;
if (field.defaultValue === undefined) {
field.defaultValue = null;
}
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'type', 'String');
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isRequired', false);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'description', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'reference', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'enums', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'widget', null);
});
});
return actions;
}
}, {
key: "cleanCollection",
value: function cleanCollection(collection) {
if (_.isNil(collection.isSearchable)) {
collection.isSearchable = true;
}
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'onlyForRelationships', false);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'isVirtual', false);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'isReadOnly', false);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'paginationType', 'page');
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'icon', null);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'nameOld', collection.name);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'integration', null);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'fields', []);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'segments', []);
SchemaFileUpdater.setDefaultValueIfNecessary(collection, 'actions', []);
collection.fields = SchemaFileUpdater.cleanFields(collection.fields);
collection.segments = SchemaFileUpdater.cleanSegments(collection.segments);
collection.actions = this.cleanActions(collection.actions);
}
}, {
key: "update",
value: function update(filename, collections, meta, serializerOptions) {
var _this2 = this;
collections = collections.map(function (collection) {
_this2.cleanCollection(collection);
var collectionFormatted = SchemaFileUpdater.formatObject(collection, serializerOptions.attributes);
SchemaFileUpdater.formatFields(collectionFormatted, serializerOptions);
SchemaFileUpdater.formatSegments(collectionFormatted, serializerOptions);
SchemaFileUpdater.formatActions(collectionFormatted, serializerOptions);
return collectionFormatted;
});
collections.sort(function (collection1, collection2) {
return collection1.name.localeCompare(collection2.name);
});
var schema = {
collections: collections,
meta: meta
};
this.fs.writeFileSync(filename, prettyPrint(schema));
return schema;
}
}], [{
key: "formatObject",
value: function formatObject(object, attributes) {
var objectFormated = {};
var objectOrdered = _.sortBy(Object.keys(object), function (attribute) {
return attributes.indexOf(attribute);
});
_.each(objectOrdered, function (attribute) {
if (attributes.includes(attribute)) {
objectFormated[attribute] = object[attribute];
}
});
return objectFormated;
}
}, {
key: "setDefaultValueIfNecessary",
value: function setDefaultValueIfNecessary(object, property, value) {
if (!Object.prototype.hasOwnProperty.call(object, property)) {
object[property] = value;
}
}
}, {
key: "cleanFields",
value: function cleanFields(fields) {
fields = fields.filter(function (field) {
return field.field;
});
fields.forEach(function (field) {
if (field.defaultValue === undefined) {
field.defaultValue = null;
}
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'type', 'String');
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isRequired', false);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isPrimaryKey', false);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isReadOnly', false);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isSortable', true);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isFilterable', true);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'isVirtual', false);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'description', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'reference', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'inverseOf', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'relationships', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'enums', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'validations', null);
SchemaFileUpdater.setDefaultValueIfNecessary(field, 'integration', null);
field.validations = field.validations || [];
field.validations.forEach(function (validation) {
if (validation.message === undefined) {
validation.message = null;
}
SchemaFileUpdater.setDefaultValueIfNecessary(validation, 'value', null);
});
});
return fields;
}
}, {
key: "cleanSegments",
value: function cleanSegments(segments) {
return segments.filter(function (segment) {
return segment.name;
});
}
}, {
key: "formatFields",
value: function formatFields(collection, serializerOptions) {
collection.fields = collection.fields || [];
collection.fields = collection.fields.map(function (field) {
var fieldFormatted = SchemaFileUpdater.formatObject(field, serializerOptions.fields.attributes);
fieldFormatted.validations = fieldFormatted.validations.map(function (validation) {
return SchemaFileUpdater.formatObject(validation, serializerOptions.validations.attributes);
});
return fieldFormatted;
});
collection.fields = _.sortBy(collection.fields, ['field', 'type']);
}
}, {
key: "formatSegments",
value: function formatSegments(collection, serializerOptions) {
collection.segments = collection.segments || [];
collection.segments = collection.segments.map(function (segment) {
return SchemaFileUpdater.formatObject(segment, serializerOptions.segments.attributes);
});
collection.segments = _.sortBy(collection.segments, ['name']);
}
}, {
key: "formatActions",
value: function formatActions(collection, serializerOptions) {
collection.actions = collection.actions || [];
collection.actions = collection.actions.map(function (action) {
var actionFormatted = SchemaFileUpdater.formatObject(action, serializerOptions.actions.attributes);
actionFormatted.fields = actionFormatted.fields || [];
actionFormatted.fields = actionFormatted.fields.map(function (field) {
return SchemaFileUpdater.formatObject(field, serializerOptions.actions.fields.attributes);
});
actionFormatted.hooks = SchemaFileUpdater.formatActionHooks(actionFormatted.hooks, action, serializerOptions);
return actionFormatted;
});
collection.actions = _.sortBy(collection.actions, ['name']);
}
}, {
key: "formatActionHooks",
value: function formatActionHooks(hooks, action, serializerOptions) {
hooks = SchemaFileUpdater.formatObject(hooks || {}, serializerOptions.actions.hooks.attributes);
hooks.load = Boolean(action.hooks && typeof action.hooks.load === 'function');
hooks.change = action.hooks && action.hooks.change && (0, _typeof2["default"])(action.hooks.change) === 'object' ? Object.keys(action.hooks.change) : [];
return hooks;
}
}]);
return SchemaFileUpdater;
}();
module.exports = SchemaFileUpdater;