UNPKG

@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

167 lines 6.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.create_stringifier = exports.ContentToCsvLines = exports.CsvExport = exports.skipEmptyStrings = void 0; const tslib_1 = require("tslib"); const csv_stringify_1 = tslib_1.__importDefault(require("csv-stringify")); const fs = tslib_1.__importStar(require("fs")); const sort_stream_1 = tslib_1.__importDefault(require("sort-stream")); const stream = tslib_1.__importStar(require("stream")); const util_1 = require("util"); const cms_1 = require("../../cms"); const exceptions_1 = require("../../cms/exceptions"); const index_1 = require("../../index"); const fields_1 = require("../../manage-cms/fields"); const finished = (0, util_1.promisify)(stream.finished); const skipEmptyStrings = (str) => Boolean(str && str.trim()); exports.skipEmptyStrings = skipEmptyStrings; /*** * @see I18nEntryTraverser limitations. * It should be reimplemented without traversing field references. * Uses https://csv.js.org/stringify/api/ */ class CsvExport { constructor(options, postprocessor = (field) => field) { this.options = options; this.toFields = new ContentToCsvLines(options, postprocessor); } static sortRows(a, b) { for (const i in a) { const cmp = a[i].localeCompare(b[i]); if (cmp != 0) { return cmp; } } return 0; } write(fname, cms, locale) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const stringifier = create_stringifier(); const readable = stream.Readable.from(this.generate(cms, locale)); const writable = readable .pipe((0, sort_stream_1.default)(CsvExport.sortRows)) .pipe(stringifier) .pipe(fs.createWriteStream(fname)); return this.toPromise(writable); }); } toPromise(writable) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return finished(writable); }); } generate(cms, from) { return tslib_1.__asyncGenerator(this, arguments, function* generate_1() { // TODO use CmsInfo to restrict the types for (const model of [ ...cms_1.BOTONIC_CONTENT_TYPES, cms_1.ContentType.URL, cms_1.ContentType.HANDOFF, ]) { console.log(`Exporting contents of type ${model}`); try { const contents = yield tslib_1.__await(cms.contents(model, { locale: from, ignoreFallbackLocale: true, })); for (const content of contents) { if (this.options.nameFilter && !this.options.nameFilter(content.name)) { continue; } console.log('Exporting content', content.name.trim() || content.id); for (const field of this.toFields.getCsvLines(content)) { const TO_COLUMN = ''; yield yield tslib_1.__await([...field, TO_COLUMN]); } } } catch (e) { if (e instanceof exceptions_1.ResourceTypeNotFoundCmsException) { console.error(`The space has no model '${e.resourceType}'. Skipping it.`); } } } }); } } exports.CsvExport = CsvExport; class ContentToCsvLines { constructor(options, postprocessor) { this.options = options; this.postprocessor = postprocessor; } getCsvLines(content) { const columns = [content.contentType, content.name, content.id]; let fields = this.getFields(content); if (this.options.stringFilter) { fields = fields.filter(f => this.options.stringFilter(f.value)); } const lines = fields.map(f => [...columns, f.name, f.value]); if (this.postprocessor) { const processed = fields.map(this.postprocessor); for (let i = 0; i < processed.length; i++) { if (processed[i].value != fields[i].value) { lines[i].push(processed[i].value); } } } return lines; } getFields(content) { if (content instanceof index_1.Button) { return [new fields_1.I18nField(fields_1.ContentFieldType.TEXT, content.text)]; } else if (content instanceof index_1.StartUp) { return [ ...this.getCommonFields(content.common), new fields_1.I18nField(fields_1.ContentFieldType.TEXT, content.text), ]; } else if (content instanceof cms_1.Text) { return [ ...this.getCommonFields(content.common), new fields_1.I18nField(fields_1.ContentFieldType.TEXT, content.text), ]; } else if (content instanceof index_1.Element) { return [ new fields_1.I18nField(fields_1.ContentFieldType.TITLE, content.title), new fields_1.I18nField(fields_1.ContentFieldType.SUBTITLE, content.subtitle), ]; } else if (content instanceof index_1.Url) { return [ ...this.getCommonFields(content.common), new fields_1.I18nField(fields_1.ContentFieldType.URL, content.url), ]; } else if (content instanceof cms_1.Handoff) { return [...this.getCommonFields(content.common)]; } else if (content instanceof index_1.TopContent) { return this.getCommonFields(content.common); } return []; } getCommonFields(common) { return [ new fields_1.I18nField(fields_1.ContentFieldType.SHORT_TEXT, common.shortText), // TODO process value based on ContentField.valueType new fields_1.I18nField(fields_1.ContentFieldType.KEYWORDS, common.keywords.join(fields_1.ContentField.STRING_ARRAY_SEPARATOR)), ]; } } exports.ContentToCsvLines = ContentToCsvLines; function create_stringifier() { return (0, csv_stringify_1.default)({ escape: '"', delimiter: ';', quote: '"', quoted: true, record_delimiter: 'windows', header: true, columns: ['Model', 'Code', 'Id', 'Field', 'From', 'To'], }); } exports.create_stringifier = create_stringifier; //# sourceMappingURL=csv-export.js.map