@blinkk/root-cms 3.0.1-beta.2 → 3.0.1-beta.4

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.
@@ -1,10 +1,6 @@
1
1
  import {
2
- RootCMSClient,
3
2
  getCmsPlugin
4
- } from "./chunk-F4SODS5S.js";
5
-
6
- // core/cron.ts
7
- import { Timestamp as Timestamp3 } from "firebase-admin/firestore";
3
+ } from "./chunk-2BSW7SIH.js";
8
4
 
9
5
  // core/search-index.ts
10
6
  import fs from "fs";
@@ -677,8 +673,8 @@ var SearchIndexService = class {
677
673
  async listAllDocs(collectionIds) {
678
674
  const all = [];
679
675
  for (const collectionId of collectionIds) {
680
- const path3 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
681
- const snap = await this.db.collection(path3).get();
676
+ const path2 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
677
+ const snap = await this.db.collection(path2).get();
682
678
  snap.forEach((d) => {
683
679
  const docId = `${collectionId}/${d.id}`;
684
680
  if (!this.isDocIndexable(docId)) {
@@ -700,8 +696,8 @@ var SearchIndexService = class {
700
696
  async listChangedDocs(collectionIds, lastRun) {
701
697
  const all = [];
702
698
  for (const collectionId of collectionIds) {
703
- const path3 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
704
- const q = this.db.collection(path3).where("sys.modifiedAt", ">=", lastRun);
699
+ const path2 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
700
+ const q = this.db.collection(path2).where("sys.modifiedAt", ">=", lastRun);
705
701
  const snap = await q.get();
706
702
  snap.forEach((d) => {
707
703
  const docId = `${collectionId}/${d.id}`;
@@ -725,8 +721,8 @@ var SearchIndexService = class {
725
721
  async hasChangesSince(lastRun, docMap = null) {
726
722
  const collectionIds = await this.listCollectionIds();
727
723
  for (const collectionId of collectionIds) {
728
- const path3 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
729
- const q = this.db.collection(path3).where("sys.modifiedAt", ">=", lastRun).limit(1);
724
+ const path2 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
725
+ const q = this.db.collection(path2).where("sys.modifiedAt", ">=", lastRun).limit(1);
730
726
  const snap = await q.get();
731
727
  if (!snap.empty) {
732
728
  return true;
@@ -739,8 +735,8 @@ var SearchIndexService = class {
739
735
  }
740
736
  const liveIds = /* @__PURE__ */ new Set();
741
737
  for (const collectionId of collectionIds) {
742
- const path3 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
743
- const snap = await this.db.collection(path3).select().get();
738
+ const path2 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
739
+ const snap = await this.db.collection(path2).select().get();
744
740
  snap.forEach((d) => {
745
741
  const docId = `${collectionId}/${d.id}`;
746
742
  if (this.isDocIndexable(docId)) {
@@ -807,8 +803,8 @@ var SearchIndexService = class {
807
803
  );
808
804
  continue;
809
805
  }
810
- const path3 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
811
- const snap = await this.db.collection(path3).get();
806
+ const path2 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
807
+ const snap = await this.db.collection(path2).get();
812
808
  snap.forEach((d) => {
813
809
  const data = d.data() || {};
814
810
  const slug = data.slug || d.id;
@@ -884,8 +880,8 @@ var SearchIndexService = class {
884
880
  }
885
881
  const liveIds = /* @__PURE__ */ new Set();
886
882
  for (const collectionId of collectionIds) {
887
- const path3 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
888
- const snap = await this.db.collection(path3).select().get();
883
+ const path2 = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
884
+ const snap = await this.db.collection(path2).select().get();
889
885
  snap.forEach((d) => {
890
886
  const docId = `${collectionId}/${d.id}`;
891
887
  if (this.isDocIndexable(docId)) {
@@ -1001,180 +997,6 @@ var SearchIndexService = class {
1001
997
  }
1002
998
  };
1003
999
 
1004
- // core/versions.ts
1005
- import fs2 from "fs";
1006
- import path2 from "path";
1007
- import { Timestamp as Timestamp2 } from "firebase-admin/firestore";
1008
- import glob2 from "tiny-glob";
1009
- var DOCUMENT_SAVE_OFFSET = 5 * 60 * 1e3;
1010
- var VersionsService = class {
1011
- constructor(rootConfig) {
1012
- this.rootConfig = rootConfig;
1013
- const cmsPlugin = getCmsPlugin(rootConfig);
1014
- const cmsPluginOptions = cmsPlugin.getConfig();
1015
- const projectId = cmsPluginOptions.id || "default";
1016
- this.projectId = projectId;
1017
- this.db = cmsPlugin.getFirestore();
1018
- }
1019
- /**
1020
- * Saves a version of all documents that have been edited since the last run.
1021
- */
1022
- async saveVersions() {
1023
- const lastRun = await this.getLastRun();
1024
- const lastRunWithOffset = lastRun === 0 ? lastRun : lastRun - DOCUMENT_SAVE_OFFSET;
1025
- const changedDocs = await this.getDocsModifiedAfter(lastRunWithOffset);
1026
- const now = Timestamp2.now().toMillis();
1027
- const versions = changedDocs.filter((doc) => {
1028
- const modifiedAt = doc.sys.modifiedAt.toMillis();
1029
- if (modifiedAt > now - DOCUMENT_SAVE_OFFSET) {
1030
- return false;
1031
- }
1032
- const publishedAt = doc.sys.publishedAt?.toMillis?.();
1033
- if (publishedAt && Math.abs(publishedAt - modifiedAt) < 5e3) {
1034
- return false;
1035
- }
1036
- return true;
1037
- });
1038
- if (versions.length > 0) {
1039
- this.saveVersionsToFirestore(versions);
1040
- }
1041
- this.saveLastRun(now);
1042
- }
1043
- async saveVersionsToFirestore(versions) {
1044
- const batch = this.db.batch();
1045
- versions.forEach((version) => {
1046
- if (!version.collection || !version.slug || !version.sys?.modifiedAt) {
1047
- return;
1048
- }
1049
- const modifiedAtMillis = version.sys.modifiedAt.toMillis();
1050
- const versionPath = `Projects/${this.projectId}/Collections/${version.collection}/Drafts/${version.slug}/Versions/${modifiedAtMillis}`;
1051
- console.log(versionPath);
1052
- const versionRef = this.db.doc(versionPath);
1053
- batch.set(versionRef, version);
1054
- });
1055
- await batch.commit();
1056
- console.log(`versions: saved ${versions.length} versions`);
1057
- }
1058
- /**
1059
- * Returns the last time (in millis) saveVersions() was run, or 0 if has
1060
- * never been run.
1061
- */
1062
- async getLastRun() {
1063
- const projectDocRef = this.db.collection("Projects").doc(this.projectId);
1064
- const projectDoc = await projectDocRef.get();
1065
- if (projectDoc.exists) {
1066
- const data = projectDoc.data() || {};
1067
- const ts = data.versionsLastRun;
1068
- if (ts) {
1069
- return ts.toMillis();
1070
- }
1071
- }
1072
- return 0;
1073
- }
1074
- /**
1075
- * Saves {versionLastRun: <timestamp>} to the Projects/<projectId> doc.
1076
- */
1077
- async saveLastRun(millis) {
1078
- const ts = Timestamp2.fromMillis(millis);
1079
- const projectDocRef = this.db.collection("Projects").doc(this.projectId);
1080
- await projectDocRef.set({ versionsLastRun: ts }, { merge: true });
1081
- }
1082
- /**
1083
- * Returns a list of all docs that were edited after a certain time.
1084
- */
1085
- async getDocsModifiedAfter(millis) {
1086
- const ts = Timestamp2.fromMillis(millis);
1087
- const results = [];
1088
- const collectionIds = await this.listCollections();
1089
- for (const collectionId of collectionIds) {
1090
- const collectionPath = `Projects/${this.projectId}/Collections/${collectionId}/Drafts`;
1091
- const query = this.db.collection(collectionPath).where("sys.modifiedAt", ">=", ts);
1092
- const querySnapshot = await query.get();
1093
- querySnapshot.forEach((doc) => {
1094
- results.push(doc.data());
1095
- });
1096
- }
1097
- return results;
1098
- }
1099
- /**
1100
- * Returns a list of collection ids for the Root project.
1101
- */
1102
- async listCollections() {
1103
- const collectionsDir = path2.join(this.rootConfig.rootDir, "collections");
1104
- if (!fs2.existsSync(collectionsDir)) {
1105
- return [];
1106
- }
1107
- const collectionIds = [];
1108
- const collectionFileNames = await glob2("*.schema.ts", {
1109
- cwd: collectionsDir
1110
- });
1111
- collectionFileNames.forEach((filename) => {
1112
- collectionIds.push(filename.slice(0, -10));
1113
- });
1114
- return collectionIds;
1115
- }
1116
- };
1117
-
1118
- // core/cron.ts
1119
- var SEARCH_INDEX_MIN_INTERVAL_MS = 5 * 60 * 1e3;
1120
- async function runCronJobs(rootConfig, options = {}) {
1121
- await Promise.all([
1122
- runCronJob("publishScheduledDocs", runPublishScheduledDocs, rootConfig),
1123
- runCronJob(
1124
- "syncScheduledDataSources",
1125
- runSyncScheduledDataSources,
1126
- rootConfig
1127
- ),
1128
- runCronJob("saveVersions", runSaveVersions, rootConfig),
1129
- runCronJob(
1130
- "incrementalSearchIndex",
1131
- runIncrementalSearchIndex,
1132
- rootConfig,
1133
- options.loadSchema
1134
- )
1135
- ]);
1136
- }
1137
- async function runCronJob(name, fn, ...args) {
1138
- try {
1139
- await fn(...args);
1140
- } catch (err) {
1141
- console.log(`cron failed: ${name}`);
1142
- console.error(String(err.stack || err));
1143
- throw err;
1144
- }
1145
- }
1146
- async function runPublishScheduledDocs(rootConfig) {
1147
- const cmsClient = new RootCMSClient(rootConfig);
1148
- await cmsClient.publishScheduledDocs();
1149
- await cmsClient.publishScheduledReleases();
1150
- }
1151
- async function runSyncScheduledDataSources(rootConfig) {
1152
- const cmsClient = new RootCMSClient(rootConfig);
1153
- await cmsClient.syncScheduledDataSources();
1154
- }
1155
- async function runSaveVersions(rootConfig) {
1156
- const service = new VersionsService(rootConfig);
1157
- await service.saveVersions();
1158
- }
1159
- async function runIncrementalSearchIndex(rootConfig, loadSchema) {
1160
- const service = new SearchIndexService(rootConfig, loadSchema);
1161
- const status = await service.getStatus();
1162
- if (status.lastRun !== null) {
1163
- const elapsed = Date.now() - status.lastRun;
1164
- if (elapsed < SEARCH_INDEX_MIN_INTERVAL_MS) {
1165
- return;
1166
- }
1167
- const hasChanges = await service.hasChangesSince(
1168
- Timestamp3.fromMillis(status.lastRun)
1169
- );
1170
- if (!hasChanges) {
1171
- return;
1172
- }
1173
- }
1174
- await service.rebuildIndex({ force: false });
1175
- }
1176
-
1177
1000
  export {
1178
- SearchIndexService,
1179
- runCronJobs
1001
+ SearchIndexService
1180
1002
  };