UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

67 lines 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReferenceFieldDuplicator = void 0; const tslib_1 = require("tslib"); const cms_1 = require("../../cms"); const manage_cms_1 = require("../../manage-cms"); const async_1 = require("../../util/async"); /** * TODO duplicate non-text fields which don't have fallback * instead of harcoding them. * Does not duplicate CommonFields.followup * Only duplicates if target field is empty */ class ReferenceFieldDuplicator { constructor(cms, info, manageCms, manageContext) { this.cms = cms; this.info = info; this.manageCms = manageCms; this.manageContext = manageContext; } duplicateReferenceFields() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const defaultLocale = yield this.info.defaultLocale(); const fields = { [cms_1.ContentType.TEXT]: [manage_cms_1.ContentFieldType.BUTTONS], [cms_1.ContentType.STARTUP]: [manage_cms_1.ContentFieldType.BUTTONS], [cms_1.ContentType.ELEMENT]: [manage_cms_1.ContentFieldType.IMAGE], }; for (const contentType of Object.keys(fields)) { console.log(`***Duplicating reference field of type '${contentType}'`); for (const fieldType of fields[contentType]) { console.log(` **Duplicating '${contentType}' fields`); yield this.duplicate(defaultLocale.code, contentType, fieldType); } } this.warning(); }); } duplicateAssetFiles() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const defaultLocale = yield this.info.defaultLocale(); console.log(`***Duplicating assets`); const assets = yield this.cms.assets({ locale: defaultLocale.code }); console.log(` **Duplicating ${assets.length} assets`); for (const a of assets) { yield this.manageCms.copyAssetFile(this.manageContext, new cms_1.AssetId(a.id, undefined), defaultLocale.code); } this.warning(); }); } warning() { if (this.manageContext.preview) { console.warn('Remember to publish the entries from contentful.com'); } } duplicate(defaultLocale, contentType, field) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const contents = yield this.cms.contents(contentType, Object.assign(Object.assign({}, this.manageContext), { locale: defaultLocale })); yield (0, async_1.asyncEach)({ concurrency: 5 }, contents, (content) => tslib_1.__awaiter(this, void 0, void 0, function* () { console.log(` *Duplicating ${content.id} (${content.name})`); yield this.manageCms.copyField(this.manageContext, cms_1.ContentId.create(contentType, content.id), field, defaultLocale, true); })); }); } } exports.ReferenceFieldDuplicator = ReferenceFieldDuplicator; //# sourceMappingURL=reference-field-duplicator.js.map