@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
63 lines • 2.88 kB
JavaScript
import { __awaiter } from "tslib";
import { AssetId, ContentId, ContentType } from '../../cms';
import { ContentFieldType } from '../../manage-cms';
import { asyncEach } from '../../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
*/
export class ReferenceFieldDuplicator {
constructor(cms, info, manageCms, manageContext) {
this.cms = cms;
this.info = info;
this.manageCms = manageCms;
this.manageContext = manageContext;
}
duplicateReferenceFields() {
return __awaiter(this, void 0, void 0, function* () {
const defaultLocale = yield this.info.defaultLocale();
const fields = {
[ContentType.TEXT]: [ContentFieldType.BUTTONS],
[ContentType.STARTUP]: [ContentFieldType.BUTTONS],
[ContentType.ELEMENT]: [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 __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 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 __awaiter(this, void 0, void 0, function* () {
const contents = yield this.cms.contents(contentType, Object.assign(Object.assign({}, this.manageContext), { locale: defaultLocale }));
yield asyncEach({ concurrency: 5 }, contents, (content) => __awaiter(this, void 0, void 0, function* () {
console.log(` *Duplicating ${content.id} (${content.name})`);
yield this.manageCms.copyField(this.manageContext, ContentId.create(contentType, content.id), field, defaultLocale, true);
}));
});
}
}
//# sourceMappingURL=reference-field-duplicator.js.map