@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
59 lines • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpaceExport = void 0;
const tslib_1 = require("tslib");
// eslint-disable-next-line node/no-missing-import
const schema = tslib_1.__importStar(require("contentful-import/dist/utils/schema"));
const fs_1 = tslib_1.__importDefault(require("fs"));
/**
* Allows modifying contentful spaces exported with "contentful space export"
*/
class SpaceExport {
constructor(jsonObject) {
SpaceExport.validate(jsonObject);
this.payload = jsonObject;
}
getLocale(locale) {
if (!this.payload.locales) {
return undefined;
}
return this.payload.locales.find(loc => loc.code == locale);
}
getDefaultLocale() {
if (!this.payload.locales) {
return undefined;
}
for (const loc of this.payload.locales) {
if (loc.default) {
return loc;
}
}
return undefined;
}
static validate(jsonObject) {
const { error } = schema.payloadSchema.validate(SpaceExport.hideFieldsWithBadSchema(jsonObject));
if (error) {
throw new Error(error.details[0].message);
}
}
static hideFieldsWithBadSchema(jsonObject) {
// contentTypes fails with types withs items
// see https://github.com/contentful/contentful-import/issues/262
const clone = Object.assign({}, jsonObject);
for (const field of ['contentTypes', 'assets', 'editorInterfaces']) {
delete clone[field];
}
return clone;
}
static fromJsonFile(filename) {
//we could use Joi schemas in node_modules/contentful-import/dist/utils/schema.js
const json = JSON.parse(fs_1.default.readFileSync(filename, 'utf8'));
return new SpaceExport(json);
}
write(filename) {
const json = JSON.stringify(this.payload, undefined, 2);
fs_1.default.writeFileSync(filename, json, 'utf8');
}
}
exports.SpaceExport = SpaceExport;
//# sourceMappingURL=space-export.js.map