UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

54 lines 1.84 kB
// eslint-disable-next-line node/no-missing-import import * as schema from 'contentful-import/dist/utils/schema'; import fs from 'fs'; /** * Allows modifying contentful spaces exported with "contentful space export" */ 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.readFileSync(filename, 'utf8')); return new SpaceExport(json); } write(filename) { const json = JSON.stringify(this.payload, undefined, 2); fs.writeFileSync(filename, json, 'utf8'); } } //# sourceMappingURL=space-export.js.map