@stackbit/cms-core 1.0.2-develop.2 → 1.0.3-develop.1

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.
@@ -81,6 +81,7 @@ import {
81
81
  } from './utils/user-log-utils';
82
82
  import { ContentEngine, PluginRef, contentEngine } from 'content-engine';
83
83
  import { mapDocumentVersionsToApiDocumentVersions } from './utils/csi-to-api-docs-converter';
84
+ import { NoopFileCache, FileCache } from './utils/file-cache';
84
85
 
85
86
  export type HandleConfigAssets = <T extends Model>({ models, presets }: { models?: T[]; presets?: PresetMap }) => Promise<{ models: T[]; presets: PresetMap }>;
86
87
 
@@ -749,6 +750,12 @@ export class ContentStore {
749
750
  return contentSourceData;
750
751
  };
751
752
 
753
+ const fileCache = this.localDev
754
+ ? new FileCache({
755
+ dirPath: this.stackbitConfig!.dirPath,
756
+ keyPrefix: `${contentSourceId}/src.`
757
+ })
758
+ : new NoopFileCache();
752
759
  const cache: CSITypes.Cache = {
753
760
  getSchema: () => {
754
761
  const contentSourceData = getContentSourceDataForCurrentInstance('getSchema');
@@ -846,6 +853,28 @@ export class ContentStore {
846
853
  contentSourceId: contentSourceId
847
854
  });
848
855
  await this.processContentStoreEvents();
856
+ },
857
+
858
+ get: async (key) => {
859
+ const contentSourceData = getContentSourceDataForCurrentInstance('get');
860
+ if (!contentSourceData) {
861
+ return;
862
+ }
863
+ return fileCache.get(key);
864
+ },
865
+ set: async (key, value) => {
866
+ const contentSourceData = getContentSourceDataForCurrentInstance('set');
867
+ if (!contentSourceData) {
868
+ return;
869
+ }
870
+ return fileCache.set(key, value);
871
+ },
872
+ remove: async (key) => {
873
+ const contentSourceData = getContentSourceDataForCurrentInstance('remove');
874
+ if (!contentSourceData) {
875
+ return;
876
+ }
877
+ return fileCache.remove(key);
849
878
  }
850
879
  };
851
880
 
@@ -0,0 +1,66 @@
1
+ import path from 'path';
2
+ import * as fse from 'fs-extra';
3
+
4
+ export interface FileCacheInterface {
5
+ get(key: string): Promise<unknown>;
6
+ set(key: string, value: unknown): Promise<void>;
7
+ remove(key: string): Promise<void>;
8
+ }
9
+
10
+ export class FileCache implements FileCacheInterface {
11
+ private readonly dirPath: string;
12
+ private readonly keyPrefix: string;
13
+ constructor({ dirPath, keyPrefix }: { dirPath: string; keyPrefix?: string }) {
14
+ this.dirPath = dirPath;
15
+ this.keyPrefix = keyPrefix ?? '';
16
+ }
17
+ async get(key: string): Promise<unknown> {
18
+ const fileName = this.filePathKey(key);
19
+ try {
20
+ const exists = await fse.pathExists(fileName);
21
+ if (!exists) {
22
+ return;
23
+ }
24
+ return fse.readJson(fileName);
25
+ } catch (error) {
26
+ return;
27
+ }
28
+ }
29
+ async set(key: string, value: unknown): Promise<void> {
30
+ const fileName = this.filePathKey(key);
31
+ try {
32
+ return fse.outputJson(fileName, value);
33
+ } catch (error) {
34
+ return;
35
+ }
36
+ }
37
+ async remove(key: string): Promise<void> {
38
+ const fileName = this.filePathKey(key);
39
+ try {
40
+ return await fse.remove(fileName);
41
+ } catch (error) {
42
+ return;
43
+ }
44
+ }
45
+
46
+ private filePathKey(key: string) {
47
+ return path.join(this.dirPath, '.stackbit/cache', `${this.keyPrefix}${key}.json`);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * This no-op file cache is used to replace FileCache when Stackbit runs in Cloud.
53
+ */
54
+ export class NoopFileCache implements FileCacheInterface {
55
+ async get(): Promise<unknown> {
56
+ return;
57
+ }
58
+
59
+ async set(): Promise<void> {
60
+ return;
61
+ }
62
+
63
+ async remove(): Promise<void> {
64
+ return;
65
+ }
66
+ }
@@ -1,12 +0,0 @@
1
- import * as ContentStoreTypes from '../types';
2
- import { SearchFilterItem } from '../types';
3
- import { ScheduledAction } from '@stackbit/types';
4
- import { SchemaForSearch } from './search-utils';
5
- declare type CustomFilterMethod = (filter: SearchFilterItem, document: ContentStoreTypes.Document, opts: {
6
- locale?: string;
7
- schema: SchemaForSearch;
8
- activeScheduledActionsByDocumentId: Record<string, ScheduledAction[]>;
9
- }) => boolean;
10
- export declare const CUSTOM_FILTERS: Record<string, CustomFilterMethod>;
11
- export {};
12
- //# sourceMappingURL=custom-search-filters.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"custom-search-filters.d.ts","sourceRoot":"","sources":["../../src/utils/custom-search-filters.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,iBAAiB,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAqB,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,EAAiE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEhH,aAAK,kBAAkB,GAAG,CACtB,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,iBAAiB,CAAC,QAAQ,EACpC,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,eAAe,CAAC;IAAC,kCAAkC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,EAAE,CAAC,CAAA;CAAE,KACxH,OAAO,CAAC;AAEb,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAwC7D,CAAC"}
@@ -1,46 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CUSTOM_FILTERS = void 0;
4
- const search_utils_1 = require("./search-utils");
5
- exports.CUSTOM_FILTERS = {
6
- hasSchedules: (filter, document, opts) => {
7
- var _a;
8
- const field = {
9
- type: 'boolean',
10
- value: !!((_a = opts.activeScheduledActionsByDocumentId[document.srcObjectId]) === null || _a === void 0 ? void 0 : _a.length)
11
- };
12
- return (0, search_utils_1.isBooleanFieldMatches)({ field, filter, locale: opts.locale });
13
- },
14
- scheduledActionId: (filter, document, opts) => {
15
- var _a, _b;
16
- const field = {
17
- type: 'list',
18
- items: (_b = (_a = opts.activeScheduledActionsByDocumentId[document.srcObjectId]) === null || _a === void 0 ? void 0 : _a.map((scheduledAction) => ({
19
- type: 'string',
20
- value: scheduledAction.id
21
- }))) !== null && _b !== void 0 ? _b : []
22
- };
23
- const model = {
24
- type: 'object',
25
- name: '',
26
- fields: [
27
- {
28
- name: '',
29
- type: 'list',
30
- items: {
31
- type: 'string'
32
- }
33
- }
34
- ]
35
- };
36
- return (0, search_utils_1.isListFieldMatches)({ field, filter, model, locale: opts.locale, document });
37
- },
38
- scheduledActionDate: (filter, document, opts) => {
39
- var _a, _b;
40
- return ((_b = (_a = opts.activeScheduledActionsByDocumentId[document.srcObjectId]) === null || _a === void 0 ? void 0 : _a.some((scheduledAction) => {
41
- const field = { type: 'date', value: scheduledAction.executeAt };
42
- return (0, search_utils_1.isDateFieldMatches)({ field, filter, locale: opts.locale });
43
- })) !== null && _b !== void 0 ? _b : false);
44
- }
45
- };
46
- //# sourceMappingURL=custom-search-filters.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"custom-search-filters.js","sourceRoot":"","sources":["../../src/utils/custom-search-filters.ts"],"names":[],"mappings":";;;AAIA,iDAAgH;AAQnG,QAAA,cAAc,GAAuC;IAC9D,YAAY,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;;QACrC,MAAM,KAAK,GAAG;YACV,IAAI,EAAE,SAAkB;YACxB,KAAK,EAAE,CAAC,CAAC,CAAA,MAAA,IAAI,CAAC,kCAAkC,CAAC,QAAQ,CAAC,WAAW,CAAC,0CAAE,MAAM,CAAA;SACjF,CAAC;QACF,OAAO,IAAA,oCAAqB,EAAC,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACzE,CAAC;IACD,iBAAiB,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;;QAC1C,MAAM,KAAK,GAAsB;YAC7B,IAAI,EAAE,MAAM;YACZ,KAAK,EACD,MAAA,MAAA,IAAI,CAAC,kCAAkC,CAAC,Q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