@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
184 lines • 6.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecordFixer = exports.CsvImport = exports.recordId = exports.PARSE_OPTIONS = void 0;
const tslib_1 = require("tslib");
const csv_parse_1 = tslib_1.__importDefault(require("csv-parse"));
const fs = tslib_1.__importStar(require("fs"));
const cms_1 = require("../../cms");
const fields_1 = require("../../manage-cms/fields");
const strings_1 = require("../../nlp/util/strings");
exports.PARSE_OPTIONS = {
escape: '"',
delimiter: ';',
quote: '"',
columns: ['Model', 'Code', 'Id', 'Field', 'from', 'to'],
relaxColumnCountMore: true,
bom: true,
};
function recordId(record) {
return `'${record.Id}/${record.Code}'`;
}
exports.recordId = recordId;
class CsvImport {
constructor(importer, options = {}) {
this.importer = importer;
this.options = options;
}
import(options) {
var _a, e_1, _b, _c;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const parse = new csv_parse_1.default.Parser(exports.PARSE_OPTIONS);
const reader = fs.createReadStream(options.fname).pipe(parse);
const header = new CsvImport.HeaderSkipper();
const records = [];
try {
for (var _d = true, _e = tslib_1.__asyncValues(reader), _f; _f = yield _e.next(), _a = _f.done, !_a;) {
_c = _f.value;
_d = false;
try {
const record = _c;
if (header.skipFirst(record)) {
this.extraColumnsMustBeEmpty(record);
continue;
}
records.push(record);
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
this.sortRecords(records);
yield this.importRecords(options, records);
});
}
sortRecords(records) {
records.sort((r1, r2) => {
const comp = r1.Model.localeCompare(r2.Model);
if (comp !== 0)
return comp;
return r1.Code.localeCompare(r2.Code);
});
}
importRecords(options, records) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
for (const record of records) {
const recordId = `'${record.Model}' field '${record.Code.trim() || record.Id}.${record.Field}'`;
if ((_a = options.ignoreContentIds) === null || _a === void 0 ? void 0 : _a.includes(record.Id)) {
console.info(`Skipping ${recordId}`);
continue;
}
if (record.from &&
!record.to &&
record.Field != fields_1.ContentFieldType.SHORT_TEXT //see ImportContentUpdater.mustBeDeleted
) {
console.warn(`Missing translation for ${recordId}`);
console.warn('To remove the content for a locale, remove the keywords and any link to it');
}
if (this.options.nameFilter && !this.options.nameFilter(record.Code)) {
continue;
}
console.log(`Importing ${recordId}`);
yield this.importer.consume(record);
}
yield this.importer.flush();
});
}
extraColumnsMustBeEmpty(record) {
const cols = exports.PARSE_OPTIONS.columns;
if (Object.keys(record).length == cols.length) {
return;
}
for (let i = cols.length; i < Object.keys(record).length; i++) {
if (Object.values(i)) {
throw new cms_1.CmsException('Too many columns');
}
}
}
}
exports.CsvImport = CsvImport;
CsvImport.HeaderSkipper = class {
constructor() {
this.first = true;
}
skipFirst(record) {
if (this.first) {
this.first = false;
if (record.Model == 'Model') {
console.log('Skipping header');
return true;
}
console.error('No header found. Parsing fist line as a normal line');
}
return false;
}
};
class RecordFixer {
constructor(record) {
this.record = record;
this.field = fields_1.CONTENT_FIELDS.get(record.Field);
}
fix() {
this.fixCapitals();
this.fixExcelNewLine();
if (this.field.valueType == fields_1.ContentFieldValueType.STRING_ARRAY) {
this.checkKeywordLen();
this.removeLastEmptyItem();
}
}
fixCapitals() {
const firstChar = this.record.Model[0];
if (firstChar === firstChar.toLowerCase()) {
return;
}
this.record.Model = this.record.Model.toLowerCase();
}
fixTrimmingQuotes() {
//it's typical to forget a " at the first character of the value
this.record.to = (0, strings_1.trim)(this.record.to, '" ');
}
fixExcelNewLine() {
//https://bugs.documentfoundation.org/show_bug.cgi?id=118470
this.record.to = (0, strings_1.replaceAll)(this.record.to, '_x000D_', '');
}
fixStringArraySeparators(stringArray) {
for (const c of ',.?') {
if (stringArray.indexOf(c) >= 0) {
this.complain(`... Replacing '${c}' with ;`);
stringArray = (0, strings_1.replaceAll)(stringArray, c, ';');
}
}
return stringArray;
}
checkKeywordLen() {
if (this.field.fieldType == fields_1.ContentFieldType.KEYWORDS) {
const fromLen = this.field.parse(this.record.from).length;
const toLen = this.field.parse(this.record.to).length;
if (toLen != fromLen || fromLen == 1) {
this.complain(`'From' has ${fromLen} keywords:\n${this.record.from}\n ` +
`but 'to' has ${toLen}:\n${this.record.to}`);
this.record.to = this.fixStringArraySeparators(this.record.to);
}
}
}
removeLastEmptyItem() {
const emptyLastItem = /; *$/;
if (emptyLastItem.exec(this.record.to)) {
this.complain('Removing final empty keyword');
this.record.to = this.record.to.replace(emptyLastItem, '');
}
}
complain(msg) {
console.error(`Problem in ${this.record.Id} / ${this.record.Code}: ${msg}`);
}
}
exports.RecordFixer = RecordFixer;
//# sourceMappingURL=csv-import.js.map