UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

93 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostProcessor = void 0; const tslib_1 = require("tslib"); const cms_1 = require("../../cms"); const cms_contentful_1 = require("../../contentful/cms-contentful"); const manage_cms_1 = require("../../manage-cms"); const fields_1 = require("../../manage-cms/fields"); const nlp_1 = require("../../nlp"); const csv_export_1 = require("./csv-export"); class PostProcessor { constructor(targetLocaleOrCountry) { this.targetLocaleOrCountry = targetLocaleOrCountry; } urlAutoTrans(field) { if (!this.targetLocaleOrCountry) { return field; } if (field.name != manage_cms_1.ContentFieldType.URL) { return field; } const convertCountry = (urlChunk) => { if (urlChunk.length != 2) { return urlChunk; } const path = () => { const lang = this.targetLocaleOrCountry.substr(0, 2).toLowerCase(); if (this.targetLocaleOrCountry.length == 2) { return lang; } const country = this.targetLocaleOrCountry.substr(3).toLowerCase(); return `${country}/${lang}`; }; const newChunk = path(); console.log(`Replacing part of URL '${field.value}': from '${urlChunk}' to '${newChunk}'`); return newChunk; }; return new fields_1.I18nField(field.name, field.value.split('/').map(convertCountry).join('/')); } } exports.PostProcessor = PostProcessor; /** * @param targetLocale if set, it converts URLs which contain the * locale (eg. a.com/es/) into targetLocale */ function writeCsvForTranslators(options, locale, fileName, targetLocaleOrCountry) { return tslib_1.__awaiter(this, void 0, void 0, function* () { (0, nlp_1.checkLocale)(locale); const cms = new cms_1.ErrorReportingCMS(new cms_contentful_1.Contentful(options)); const postProcess = targetLocaleOrCountry ? new PostProcessor(targetLocaleOrCountry) : undefined; const exporter = new csv_export_1.CsvExport({ stringFilter: csv_export_1.skipEmptyStrings, }, postProcess ? (field) => postProcess.urlAutoTrans(field) : undefined); return yield exporter.write(fileName, cms, locale); }); } if (process.argv.length < 7 || process.argv[2] == '--help') { console.log(`Usage: space_id environment access_token locale filename [target_locale]`); console.log('If target_locale specified, it converts URLs which contains the ' + 'locale (eg. a.com/es/) into target_locale'); // eslint-disable-next-line no-process-exit process.exit(1); } const spaceId = process.argv[2]; const environment = process.argv[3]; const accessToken = process.argv[4]; const locale = process.argv[5]; const fileName = process.argv[6]; const targetLocaleOrCountry = process.argv[7]; function main() { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield writeCsvForTranslators({ spaceId, accessToken, environment, resumeErrors: true, }, locale, fileName, targetLocaleOrCountry); console.log('done'); } catch (e) { console.error(e); } }); } // void tells linters that we don't want to wait for promise // await in main requires esnext void main(); //# sourceMappingURL=export-csv-for-translators.js.map