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

111 lines 4.88 kB
import { __awaiter } from "tslib"; import { MessageContentInverseTraverser } from '../../cms/visitors/message-visitors'; import { createCms, createCmsInfo, createManageCms, } from '../../contentful/factories'; import { ContentDeleter } from '../../manage-cms/content-deleter'; import { isOfType } from '../../util/enums'; import { CsvImport } from './csv-import'; import { ImportContentUpdater, ImportRecordReducer } from './import-updater'; import { ReferenceFieldDuplicator } from './reference-field-duplicator'; function readCsvForTranslators(manageCms, cms, info, context, options) { return __awaiter(this, void 0, void 0, function* () { const reachableFromButtons = new MessageContentInverseTraverser(cms, info, context, { ignoreFollowUps: true }); const deleter = new ContentDeleter(manageCms, reachableFromButtons, context); const updater = new ImportContentUpdater(manageCms, cms, info, context, deleter); const fieldImporter = new ImportRecordReducer(updater, { resumeErrors: options.resumeErrors, }); const importer = new CsvImport(fieldImporter); yield importer.import(options); }); } export var ImportType; (function (ImportType) { ImportType["DRY"] = "DRY"; ImportType["NO_OVERWRITE"] = "NO_OVERWRITE"; ImportType["OVERWRITE"] = "OVERWRITE"; ImportType["OVERWRITE_AND_PUBLISH"] = "OVERWRITE_AND_PUBLISH"; })(ImportType || (ImportType = {})); if (process.argv.length < 10 || process.argv[2] == '--help') { console.log(`Usage: space_id environment delivery_token mgmnt_token locale filename [${Object.values(ImportType).join('|')}] duplicate_references`); console.log(`duplicate_references: if 'true', it will copy also the buttons, element images and assets of all contents ` + `(not only the ones in the input file)`); // eslint-disable-next-line no-process-exit process.exit(1); } const spaceId = process.argv[2]; const environment = process.argv[3]; const deliverAccessToken = process.argv[4]; const manageAccessToken = process.argv[5]; const locale = process.argv[6]; const fileName = process.argv[7]; const importType = String(process.argv[8]); if (!isOfType(importType, ImportType)) { throw Error(`${importType} is not a valid value`); } const duplicateReferences = process.argv[9].toLowerCase() == 'true' ? true : process.argv[9].toLowerCase() == 'false' ? false : undefined; if (duplicateReferences == undefined) { throw Error("duplicateReferences argument must be 'true' or 'false'"); } function main() { return __awaiter(this, void 0, void 0, function* () { try { if (importType == ImportType.NO_OVERWRITE) { console.warn('Contents will be left in preview mode. Publish them from contentful.com'); } const manageContext = { locale, preview: importType == ImportType.OVERWRITE, dryRun: importType == ImportType.DRY, allowOverwrites: [ ImportType.OVERWRITE, ImportType.OVERWRITE_AND_PUBLISH, ImportType.DRY, ].includes(importType), }; const cmsOptions = { spaceId, accessToken: deliverAccessToken, environment, resumeErrors: true, }; const cms = createCms(cmsOptions); const info = createCmsInfo(cmsOptions); const manageCms = createManageCms({ spaceId, accessToken: manageAccessToken, environment, }); if (duplicateReferences) { console.log('Duplicating reference fields'); yield duplicateReferenceFields(manageCms, cms, info, manageContext); } yield readCsvForTranslators(manageCms, cms, info, manageContext, { fname: fileName, resumeErrors: true, ignoreContentIds: [], }); console.log('done'); } catch (e) { console.error(e); } if (importType == ImportType.OVERWRITE) { console.log("Remember that you'll need to publish the changed contents from contentful.com"); } }); } function duplicateReferenceFields(manageCms, cms, info, manageContext) { return __awaiter(this, void 0, void 0, function* () { const referenceDuplicator = new ReferenceFieldDuplicator(cms, info, manageCms, manageContext); yield referenceDuplicator.duplicateReferenceFields(); yield referenceDuplicator.duplicateAssetFiles(); }); } // void tells linters that we don't want to wait for promise // await in main requires esnext void main(); //# sourceMappingURL=import-csv-from-translators.js.map