@stackbit/cms-core 0.8.6-develop.2 → 0.8.6-develop.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,6 +70,7 @@ import {
70
70
  pluralize
71
71
  } from './utils/user-log-utils';
72
72
  import { getSanitizedTreeViews } from './utils/tree-views';
73
+ import { ContentEnginePublicAPI, PluginRef, contentEngine } from 'content-engine';
73
74
 
74
75
  export type HandleConfigAssets = <T extends Model>({ models, presets }: { models?: T[]; presets?: PresetMap }) => Promise<{ models: T[]; presets: PresetMap }>;
75
76
 
@@ -147,6 +148,7 @@ export class ContentStore {
147
148
  private contentStoreEventQueue: ContentStoreEventQueue = [];
148
149
  private treeViews: CSITypes.TreeViewNode[] = [];
149
150
  private customActionRunStateMap: ContentStoreTypes.CustomActionRunStateMap = {};
151
+ private contentEngine?: ContentEnginePublicAPI | null;
150
152
 
151
153
  constructor(options: ContentSourceOptions) {
152
154
  this.logger = options.logger.createLogger({ label: 'content-store' });
@@ -388,6 +390,39 @@ export class ContentStore {
388
390
  return presets;
389
391
  }
390
392
 
393
+ /**
394
+ * If any content sources implement the `getContentEngineConfig` method then this function gets all
395
+ * of these plugin definitions and creates the content engine.
396
+ */
397
+ private async createContentEngineIfNeeded() {
398
+ const logger = this.userLogger.createLogger({ label: 'content-engine' });
399
+ if (this.contentEngine) {
400
+ logger.info('stopping content engine');
401
+ this.contentEngine.clearListeners();
402
+ await this.contentEngine.stop();
403
+ this.contentEngine = null;
404
+ }
405
+ const plugins = this.contentSources?.flatMap((contentSource) => contentSource.getContentEngineConfig?.()).filter(Boolean) as PluginRef[];
406
+ if (plugins.length < 1) {
407
+ return;
408
+ }
409
+ logger.info('creating content engine');
410
+ this.contentEngine = contentEngine({
411
+ directory: this.stackbitConfig?.dirPath,
412
+ engineConfig: {
413
+ plugins
414
+ }
415
+ });
416
+
417
+ this.contentEngine?.onStdOut((data) => {
418
+ logger.info(data.toString());
419
+ });
420
+
421
+ this.contentEngine?.onStdErr((data) => {
422
+ logger.error(data.toString());
423
+ });
424
+ }
425
+
391
426
  /**
392
427
  * This function reloads the data of the specified content-sources, while
393
428
  * reusing the cached data of the rest of the content-sources, then processes
@@ -432,6 +467,14 @@ export class ContentStore {
432
467
 
433
468
  this.contentSources = contentSources;
434
469
 
470
+ if (init) {
471
+ this.logger.debug('init content sources');
472
+ await this.createContentEngineIfNeeded();
473
+ await this.contentEngine?.sync({
474
+ runServer: true
475
+ });
476
+ }
477
+
435
478
  const promises = contentSources.map((contentSourceInstance): Promise<ContentSourceRawData> => {
436
479
  const contentSourceId = getContentSourceIdForContentSource(contentSourceInstance);
437
480
  if (init || !contentSourceIds || contentSourceIds.includes(contentSourceId)) {
@@ -744,6 +787,10 @@ export class ContentStore {
744
787
  return;
745
788
  }
746
789
  this.logger.debug('content source called updateContent', { contentSourceId });
790
+ await this.contentEngine?.sync({
791
+ buildSchema: false
792
+ });
793
+
747
794
  this.pushContentSourceEvent({
748
795
  eventName: ContentStoreEventType.ContentSourceContentChange,
749
796
  contentSourceId: contentSourceId,
@@ -758,6 +805,9 @@ export class ContentStore {
758
805
  return;
759
806
  }
760
807
  this.logger.debug('content source called invalidateSchema', { contentSourceId });
808
+ await this.contentEngine?.sync({
809
+ buildSchema: true
810
+ });
761
811
  this.pushContentSourceEvent({
762
812
  eventName: ContentStoreEventType.ContentSourceInvalidateSchema,
763
813
  contentSourceId: contentSourceId
@@ -844,13 +844,10 @@ function findCustomActionById({
844
844
  if (!model || (model.type !== 'page' && model.type !== 'data')) {
845
845
  return undefined;
846
846
  }
847
- if (!('actions' in model) || !Array.isArray(model.actions)) {
848
- return undefined;
849
- }
850
847
  if (typeof fieldPath === 'undefined') {
851
848
  // fieldPath was not provided, therefore the model must be of type "page" or "data",
852
849
  // and the action type must be 'document'
853
- const action = model.actions.find((action) => action.name === actionName);
850
+ const action = model.actions?.find((action) => action.name === actionName);
854
851
  if (!action) {
855
852
  return undefined;
856
853
  }