@griddo/cx 11.7.11 → 11.7.12-rc.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.
Files changed (302) hide show
  1. package/README.md +27 -226
  2. package/build/adapters/gatsby/actions/clean.js +10 -0
  3. package/build/adapters/gatsby/actions/clean.js.map +1 -0
  4. package/build/adapters/gatsby/actions/close.js +14 -0
  5. package/build/adapters/gatsby/actions/close.js.map +1 -0
  6. package/build/adapters/gatsby/actions/data.js +18 -0
  7. package/build/adapters/gatsby/actions/data.js.map +1 -0
  8. package/build/adapters/gatsby/actions/healthCheck.js +10 -0
  9. package/build/adapters/gatsby/actions/healthCheck.js.map +1 -0
  10. package/build/adapters/gatsby/actions/init.js +22 -0
  11. package/build/adapters/gatsby/actions/init.js.map +1 -0
  12. package/build/adapters/gatsby/actions/logs.js +9 -0
  13. package/build/adapters/gatsby/actions/logs.js.map +1 -0
  14. package/build/adapters/gatsby/actions/meta.js +13 -0
  15. package/build/adapters/gatsby/actions/meta.js.map +1 -0
  16. package/build/adapters/gatsby/actions/prepare.js +9 -0
  17. package/build/adapters/gatsby/actions/prepare.js.map +1 -0
  18. package/build/adapters/gatsby/actions/relocation.js +15 -0
  19. package/build/adapters/gatsby/actions/relocation.js.map +1 -0
  20. package/build/adapters/gatsby/actions/restore.js +26 -0
  21. package/build/adapters/gatsby/actions/restore.js.map +1 -0
  22. package/build/adapters/gatsby/actions/ssg.js +9 -0
  23. package/build/adapters/gatsby/actions/ssg.js.map +1 -0
  24. package/build/adapters/gatsby/actions/sync.js +51 -0
  25. package/build/adapters/gatsby/actions/sync.js.map +1 -0
  26. package/build/adapters/gatsby/index.js +78 -0
  27. package/build/adapters/gatsby/index.js.map +1 -0
  28. package/build/adapters/gatsby/shared/artifacts.js +20 -0
  29. package/build/adapters/gatsby/shared/artifacts.js.map +1 -0
  30. package/build/adapters/gatsby/shared/context.js +31 -0
  31. package/build/adapters/gatsby/shared/context.js.map +1 -0
  32. package/build/adapters/gatsby/shared/diff-assets.js +101 -0
  33. package/build/adapters/gatsby/shared/diff-assets.js.map +1 -0
  34. package/build/adapters/gatsby/shared/extract-assets.js +58 -0
  35. package/build/adapters/gatsby/shared/extract-assets.js.map +1 -0
  36. package/build/adapters/gatsby/shared/gatsby-build.js +55 -0
  37. package/build/adapters/gatsby/shared/gatsby-build.js.map +1 -0
  38. package/build/adapters/gatsby/shared/sync-render.js +210 -0
  39. package/build/adapters/gatsby/shared/sync-render.js.map +1 -0
  40. package/build/adapters/gatsby/shared/types.js +3 -0
  41. package/build/adapters/gatsby/shared/types.js.map +1 -0
  42. package/build/commands/end-render.js +87 -0
  43. package/build/commands/end-render.js.map +1 -0
  44. package/build/commands/prepare-assets-directory.js +30 -0
  45. package/build/commands/prepare-assets-directory.js.map +1 -0
  46. package/build/commands/prepare-domains-render.js +133 -0
  47. package/build/commands/prepare-domains-render.js.map +1 -0
  48. package/build/commands/reset-render.js +21 -0
  49. package/build/commands/reset-render.js.map +1 -0
  50. package/build/commands/start-render.js +44 -0
  51. package/build/commands/start-render.js.map +1 -0
  52. package/build/commands/upload-search-content.js +180 -0
  53. package/build/commands/upload-search-content.js.map +1 -0
  54. package/build/constants/endpoints.js +43 -0
  55. package/build/constants/endpoints.js.map +1 -0
  56. package/build/constants/envs.js +68 -0
  57. package/build/constants/envs.js.map +1 -0
  58. package/build/constants/errors.js +81 -0
  59. package/build/constants/errors.js.map +1 -0
  60. package/build/services/auth.js +40 -0
  61. package/build/services/auth.js.map +1 -0
  62. package/build/services/db-class.js +49 -0
  63. package/build/services/db-class.js.map +1 -0
  64. package/build/services/db.js +34 -0
  65. package/build/services/db.js.map +1 -0
  66. package/build/services/navigation.js +96 -0
  67. package/build/services/navigation.js.map +1 -0
  68. package/build/services/reference-fields.js +131 -0
  69. package/build/services/reference-fields.js.map +1 -0
  70. package/build/services/robots.js +60 -0
  71. package/build/services/robots.js.map +1 -0
  72. package/build/services/sites.js +110 -0
  73. package/build/services/sites.js.map +1 -0
  74. package/build/services/store.js +257 -0
  75. package/build/services/store.js.map +1 -0
  76. package/build/types/api.js +3 -0
  77. package/build/types/api.js.map +1 -0
  78. package/build/types/global.js +3 -0
  79. package/build/types/global.js.map +1 -0
  80. package/build/types/navigation.js +3 -0
  81. package/build/types/navigation.js.map +1 -0
  82. package/build/types/pages.js +3 -0
  83. package/build/types/pages.js.map +1 -0
  84. package/build/types/render.js +10 -0
  85. package/build/types/render.js.map +1 -0
  86. package/build/types/sites.js +3 -0
  87. package/build/types/sites.js.map +1 -0
  88. package/build/types/templates.js +3 -0
  89. package/build/types/templates.js.map +1 -0
  90. package/build/utils/api.js +165 -0
  91. package/build/utils/api.js.map +1 -0
  92. package/build/utils/artifacts.js +38 -0
  93. package/build/utils/artifacts.js.map +1 -0
  94. package/build/utils/brush.js +30 -0
  95. package/build/utils/brush.js.map +1 -0
  96. package/build/utils/cache.js +106 -0
  97. package/build/utils/cache.js.map +1 -0
  98. package/build/utils/check-environment-health.js +69 -0
  99. package/build/utils/check-environment-health.js.map +1 -0
  100. package/build/utils/core-utils.js +270 -0
  101. package/build/utils/core-utils.js.map +1 -0
  102. package/build/utils/domains.js +37 -0
  103. package/build/utils/domains.js.map +1 -0
  104. package/build/utils/errors.js +30 -0
  105. package/build/utils/errors.js.map +1 -0
  106. package/build/utils/folders.js +341 -0
  107. package/build/utils/folders.js.map +1 -0
  108. package/build/utils/images.js +45 -0
  109. package/build/utils/images.js.map +1 -0
  110. package/build/utils/instance.js +66 -0
  111. package/build/utils/instance.js.map +1 -0
  112. package/build/utils/loggin.js +119 -0
  113. package/build/utils/loggin.js.map +1 -0
  114. package/build/utils/npm-deps/find-up-simple.js +75 -0
  115. package/build/utils/npm-deps/find-up-simple.js.map +1 -0
  116. package/build/utils/npm-deps/pkg-dir.js +20 -0
  117. package/build/utils/npm-deps/pkg-dir.js.map +1 -0
  118. package/build/utils/npm-deps/xml-parser.js +49 -0
  119. package/build/utils/npm-deps/xml-parser.js.map +1 -0
  120. package/build/utils/pages.js +359 -0
  121. package/build/utils/pages.js.map +1 -0
  122. package/build/utils/render.js +168 -0
  123. package/build/utils/render.js.map +1 -0
  124. package/build/utils/sitemaps.js +110 -0
  125. package/build/utils/sitemaps.js.map +1 -0
  126. package/build/utils/sites.js +105 -0
  127. package/build/utils/sites.js.map +1 -0
  128. package/build/utils/store.js +193 -0
  129. package/build/utils/store.js.map +1 -0
  130. package/exporter/adapters/gatsby/actions/clean.ts +14 -0
  131. package/exporter/adapters/gatsby/actions/close.ts +20 -0
  132. package/exporter/adapters/gatsby/actions/data.ts +25 -0
  133. package/exporter/adapters/gatsby/actions/healthCheck.ts +10 -0
  134. package/exporter/adapters/gatsby/actions/init.ts +26 -0
  135. package/exporter/adapters/gatsby/actions/logs.ts +10 -0
  136. package/exporter/adapters/gatsby/actions/meta.ts +16 -0
  137. package/exporter/adapters/gatsby/actions/prepare.ts +9 -0
  138. package/exporter/adapters/gatsby/actions/relocation.ts +15 -0
  139. package/exporter/adapters/gatsby/actions/restore.ts +36 -0
  140. package/exporter/adapters/gatsby/actions/ssg.ts +12 -0
  141. package/exporter/adapters/gatsby/actions/sync.ts +71 -0
  142. package/exporter/adapters/gatsby/index.ts +73 -153
  143. package/exporter/adapters/gatsby/shared/artifacts.ts +16 -0
  144. package/exporter/adapters/gatsby/shared/context.ts +50 -0
  145. package/exporter/adapters/gatsby/shared/diff-assets.ts +113 -0
  146. package/exporter/adapters/gatsby/shared/extract-assets.ts +61 -0
  147. package/exporter/adapters/gatsby/shared/gatsby-build.ts +58 -0
  148. package/exporter/adapters/gatsby/shared/sync-render.ts +276 -0
  149. package/exporter/adapters/gatsby/shared/types.ts +35 -0
  150. package/exporter/build-esbuild.noop +42 -0
  151. package/exporter/build.sh +4 -38
  152. package/exporter/commands/end-render.ts +72 -70
  153. package/exporter/commands/prepare-assets-directory.ts +30 -0
  154. package/exporter/commands/prepare-domains-render.ts +143 -31
  155. package/exporter/commands/reset-render.ts +10 -3
  156. package/exporter/commands/start-render.ts +22 -45
  157. package/exporter/commands/upload-search-content.ts +194 -21
  158. package/exporter/constants/endpoints.ts +2 -1
  159. package/exporter/constants/envs.ts +57 -55
  160. package/exporter/{errors/errors-data.ts → constants/errors.ts} +24 -14
  161. package/exporter/services/auth.ts +7 -6
  162. package/exporter/services/db-class.ts +54 -0
  163. package/exporter/services/db.ts +32 -0
  164. package/exporter/services/navigation.ts +4 -10
  165. package/exporter/services/reference-fields.ts +9 -24
  166. package/exporter/services/robots.ts +9 -16
  167. package/exporter/services/sites.ts +36 -24
  168. package/exporter/services/store.ts +107 -96
  169. package/exporter/types/api.ts +27 -28
  170. package/exporter/types/global.ts +8 -11
  171. package/exporter/types/navigation.ts +1 -1
  172. package/exporter/types/pages.ts +2 -3
  173. package/exporter/types/render.ts +59 -0
  174. package/exporter/types/sites.ts +1 -2
  175. package/exporter/utils/api.ts +55 -75
  176. package/exporter/utils/artifacts.ts +38 -0
  177. package/exporter/utils/brush.ts +34 -0
  178. package/exporter/utils/cache.ts +37 -62
  179. package/exporter/utils/check-environment-health.ts +80 -0
  180. package/exporter/utils/core-utils.ts +133 -272
  181. package/exporter/utils/domains.ts +10 -7
  182. package/exporter/{errors/index.ts → utils/errors.ts} +10 -9
  183. package/exporter/utils/folders.ts +163 -98
  184. package/exporter/utils/images.ts +1 -6
  185. package/exporter/utils/instance.ts +9 -13
  186. package/exporter/utils/loggin.ts +44 -91
  187. package/exporter/utils/npm-deps/find-up-simple.ts +100 -0
  188. package/exporter/utils/npm-deps/pkg-dir.ts +17 -0
  189. package/exporter/utils/npm-deps/xml-parser.ts +57 -0
  190. package/exporter/utils/pages.ts +23 -88
  191. package/exporter/utils/render.ts +180 -48
  192. package/exporter/utils/sitemaps.ts +129 -0
  193. package/exporter/utils/sites.ts +19 -197
  194. package/exporter/utils/store.ts +87 -180
  195. package/gatsby-browser.tsx +41 -58
  196. package/gatsby-config.ts +10 -17
  197. package/gatsby-node.ts +17 -78
  198. package/gatsby-ssr.tsx +2 -1
  199. package/package.json +35 -86
  200. package/plugins/gatsby-plugin-svgr-loader/gatsby-node.js +55 -0
  201. package/plugins/gatsby-plugin-svgr-loader/package.json +8 -0
  202. package/react/DynamicScript/index.tsx +33 -0
  203. package/{exporter/react → react}/Favicon/index.tsx +1 -7
  204. package/{exporter/react → react}/GriddoIntegrations/index.tsx +16 -22
  205. package/{exporter/react → react}/GriddoIntegrations/utils.ts +21 -9
  206. package/{build/react/index.d.ts → react/index.tsx} +1 -0
  207. package/src/components/Head.tsx +13 -46
  208. package/src/components/template.tsx +8 -30
  209. package/src/gatsby-node-utils.ts +73 -2
  210. package/src/html.tsx +2 -11
  211. package/src/types.ts +5 -5
  212. package/tsconfig.commands.json +36 -0
  213. package/tsconfig.exporter.json +19 -0
  214. package/tsconfig.json +5 -3
  215. package/build/adapters/gatsby/index.d.ts +0 -4
  216. package/build/adapters/gatsby/utils.d.ts +0 -22
  217. package/build/artifacts/index.d.ts +0 -6
  218. package/build/commands/end-render.d.ts +0 -2
  219. package/build/commands/move-assets.d.ts +0 -1
  220. package/build/commands/prepare-domains-render.d.ts +0 -1
  221. package/build/commands/reset-render.d.ts +0 -2
  222. package/build/commands/start-render.d.ts +0 -2
  223. package/build/commands/upload-search-content.d.ts +0 -2
  224. package/build/constants/endpoints.d.ts +0 -19
  225. package/build/constants/envs.d.ts +0 -37
  226. package/build/constants/index.d.ts +0 -57
  227. package/build/end-render.js +0 -74
  228. package/build/end-render.js.map +0 -7
  229. package/build/errors/errors-data.d.ts +0 -22
  230. package/build/errors/index.d.ts +0 -15
  231. package/build/index.d.ts +0 -29
  232. package/build/index.js +0 -73
  233. package/build/index.js.map +0 -7
  234. package/build/prepare-domains-render.js +0 -73
  235. package/build/prepare-domains-render.js.map +0 -7
  236. package/build/react/Favicon/index.d.ts +0 -5
  237. package/build/react/Favicon/utils.d.ts +0 -9
  238. package/build/react/GriddoIntegrations/index.d.ts +0 -20
  239. package/build/react/GriddoIntegrations/utils.d.ts +0 -26
  240. package/build/react/index.js +0 -3
  241. package/build/registers/api.d.ts +0 -9
  242. package/build/registers/gatsby.d.ts +0 -9
  243. package/build/registers/index.d.ts +0 -3
  244. package/build/reset-render.js +0 -74
  245. package/build/reset-render.js.map +0 -7
  246. package/build/services/auth.d.ts +0 -10
  247. package/build/services/domains.d.ts +0 -6
  248. package/build/services/navigation.d.ts +0 -50
  249. package/build/services/reference-fields.d.ts +0 -20
  250. package/build/services/register.d.ts +0 -36
  251. package/build/services/robots.d.ts +0 -19
  252. package/build/services/settings.d.ts +0 -4
  253. package/build/services/sites.d.ts +0 -29
  254. package/build/services/store.d.ts +0 -6
  255. package/build/start-render.js +0 -100
  256. package/build/start-render.js.map +0 -7
  257. package/build/types/api.d.ts +0 -142
  258. package/build/types/global.d.ts +0 -84
  259. package/build/types/navigation.d.ts +0 -28
  260. package/build/types/pages.d.ts +0 -144
  261. package/build/types/sites.d.ts +0 -57
  262. package/build/types/templates.d.ts +0 -8
  263. package/build/upload-search-content.js +0 -74
  264. package/build/upload-search-content.js.map +0 -7
  265. package/build/utils/alerts.d.ts +0 -3
  266. package/build/utils/api.d.ts +0 -23
  267. package/build/utils/cache.d.ts +0 -35
  268. package/build/utils/core-utils.d.ts +0 -107
  269. package/build/utils/create-build-data.d.ts +0 -8
  270. package/build/utils/domains.d.ts +0 -13
  271. package/build/utils/folders.d.ts +0 -53
  272. package/build/utils/health-checks.d.ts +0 -7
  273. package/build/utils/images.d.ts +0 -16
  274. package/build/utils/instance.d.ts +0 -21
  275. package/build/utils/loggin.d.ts +0 -51
  276. package/build/utils/pages.d.ts +0 -34
  277. package/build/utils/render.d.ts +0 -13
  278. package/build/utils/searches.d.ts +0 -15
  279. package/build/utils/sites.d.ts +0 -31
  280. package/build/utils/store.d.ts +0 -81
  281. package/cx.config.d.ts +0 -5
  282. package/cx.config.js +0 -36
  283. package/exporter/adapters/gatsby/utils.ts +0 -161
  284. package/exporter/artifacts/README.md +0 -34
  285. package/exporter/artifacts/index.ts +0 -33
  286. package/exporter/commands/move-assets.ts +0 -11
  287. package/exporter/constants/index.ts +0 -129
  288. package/exporter/index.ts +0 -82
  289. package/exporter/react/index.tsx +0 -11
  290. package/exporter/registers/api.ts +0 -14
  291. package/exporter/registers/gatsby.ts +0 -14
  292. package/exporter/registers/index.ts +0 -4
  293. package/exporter/services/domains.ts +0 -16
  294. package/exporter/services/register.ts +0 -113
  295. package/exporter/services/settings.ts +0 -17
  296. package/exporter/utils/alerts.ts +0 -29
  297. package/exporter/utils/create-build-data.ts +0 -17
  298. package/exporter/utils/health-checks.ts +0 -64
  299. package/exporter/utils/searches.ts +0 -156
  300. package/src/README.md +0 -7
  301. package/start-render.js +0 -7
  302. /package/{exporter/react → react}/Favicon/utils.ts +0 -0
@@ -1,38 +1,211 @@
1
- #!/usr/bin/env node
1
+ import type { GatsbyPageData } from "../adapters/gatsby/shared/types";
2
+ import type { PostSearchInfoResponse } from "../types/api";
3
+ import type { AIEmbeddingsResponse, PostSearchInfoProps } from "../types/global";
2
4
 
3
- import fs from "node:fs";
5
+ import fsp from "node:fs/promises";
6
+ import path from "node:path";
4
7
 
5
- import { envs } from "../constants";
6
- import { getConfig } from "../utils/core-utils";
8
+ import { AI_EMBEDDINGS, SEARCH } from "../constants/endpoints";
9
+ import { GRIDDO_AI_EMBEDDINGS, GRIDDO_SEARCH_FEATURE } from "../constants/envs";
10
+ import { ReadFromStoreError, UploadSearchError } from "../constants/errors";
11
+ import { AuthService } from "../services/auth";
12
+ import { post } from "../utils/api";
7
13
  import { getInstanceDomains } from "../utils/domains";
8
- import {
9
- startAIEmbeddings,
10
- uploadRenderedSearchContentToAPI,
11
- } from "../utils/searches";
14
+ import { throwError } from "../utils/errors";
15
+ import { pathExists } from "../utils/folders";
16
+ import { getRenderPathsFromDB } from "../utils/render";
17
+
18
+ /**
19
+ * Save in the BBDD the content of a page parsed without HTML tags.
20
+ *
21
+ * @param props Object with parts of the final page object to be saved in the BBDD.
22
+ */
23
+ async function postSearchInfo(props: PostSearchInfoProps) {
24
+ const { title, description, image, pageId, languageId, siteId, url, content, template } = props;
25
+
26
+ const response = await post<PostSearchInfoResponse>({
27
+ endpoint: SEARCH,
28
+ body: {
29
+ title,
30
+ description,
31
+ image,
32
+ pageId,
33
+ languageId,
34
+ siteId,
35
+ url,
36
+ template,
37
+ content,
38
+ },
39
+ useApiCacheDir: false,
40
+ logToFile: false,
41
+ });
42
+
43
+ return response;
44
+ }
45
+
46
+ function prepareHTMLContentForSearch(content: string): string {
47
+ // 1. Remove script, style, and other unwanted block tags and their content.
48
+ // The regex looks for <tag...>...</tag> where tag is one of the specified ones.
49
+ const tagsToRemove = ["meta", "link", "style", "script", "noscript", "nav", "header", "footer"];
50
+ const removeTagsRegex = new RegExp(`<(${tagsToRemove.join("|")})\\b[^>]*>.*?<\\/\\1>`, "gis");
51
+ let processedContent = content.replace(removeTagsRegex, "");
52
+
53
+ // 2. Strip all remaining HTML tags.
54
+ processedContent = processedContent.replace(/<[^>]+>/g, " ");
55
+
56
+ // 3. Normalize whitespace: replace multiple spaces/newlines with a single space and trim.
57
+ processedContent = processedContent.replace(/\s+/g, " ").trim();
58
+
59
+ return processedContent;
60
+ }
61
+
62
+ /**
63
+ * Function that search in the `/public` dir the content info of the pages and
64
+ * send it to the search table in the ddbb using the API.
65
+ * @todo Utilizar la carpeta page-data en lugar de la carpeta store puesto que
66
+ * esta ya no es persistente.
67
+ */
68
+ async function uploadRenderedSearchContentToAPI(options: {
69
+ htmlContentDir: string;
70
+ jsonContentDir: string;
71
+ }) {
72
+ const { htmlContentDir, jsonContentDir } = options;
73
+
74
+ if (!(await pathExists(jsonContentDir)) || !(await pathExists(htmlContentDir))) {
75
+ console.log(
76
+ `Skipping uploading content to the search endpoint because it has not exported sites.`,
77
+ );
12
78
 
13
- async function main() {
14
- if (!envs.GRIDDO_SEARCH_FEATURE) {
15
79
  return;
16
80
  }
17
81
 
18
- const domains = await getInstanceDomains();
19
- const config = getConfig();
20
- for (const domain of domains) {
21
- const { __exports_dist } = config.paths(domain);
82
+ // Get pages from gatsby page-data dir
83
+ const gatsbyPageDataPages = getPageDataPagesFromExports(jsonContentDir);
84
+
85
+ for await (const pageData of gatsbyPageDataPages) {
86
+ const { result } = pageData;
87
+ const { pageContext } = result;
88
+ const { page, openGraph, pageMetadata } = pageContext;
89
+
90
+ const { compose } = page.fullPath;
91
+
92
+ const htmlPath = path.resolve(`${htmlContentDir}/${compose}/index.html`);
93
+ const htmlContent = await fsp.readFile(htmlPath, "utf-8");
94
+
95
+ const pageObject: PostSearchInfoProps = {
96
+ siteId: page.site,
97
+ pageId: page.id,
98
+ // `pageMetadata.title` has already a fallback `metatitle ||
99
+ // pageTitle` so probably `title` never will be take the
100
+ // `openGraph?.title` value. Only when the `metatitle` and
101
+ // `pageTitle` are empty.
102
+ title: pageMetadata?.title || openGraph?.title,
103
+ languageId: page.language,
104
+ url: page.fullUrl,
105
+ template: page.template.templateType || page.templateId,
106
+ description: pageMetadata?.description || openGraph?.description,
107
+ image: openGraph.image,
108
+ // _content: prepareHTMLContentForSearch(htmlContent),
109
+ content: prepareHTMLContentForSearch(htmlContent),
110
+ };
111
+
112
+ try {
113
+ await postSearchInfo(pageObject);
114
+ } catch (error) {
115
+ throwError(UploadSearchError, error);
116
+ }
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Walk recursively in a basePath and return an array of pages with json
122
+ * extension with the full absolute path and the path includes "page-data".
123
+ *
124
+ * @param basePath The path to walk recursively.
125
+ * @returns An array of pages with json extension with the full absolute path
126
+ * and the path includes "page-data".
127
+ */
128
+ async function* walkRecursively(basePath: string): AsyncGenerator<string> {
129
+ const filesHandle = await fsp.opendir(basePath);
22
130
 
23
- // If __exports_dist has not exported sites (directories), it does not
24
- // upload search content.
25
- if (fs.existsSync(__exports_dist)) {
26
- await uploadRenderedSearchContentToAPI(__exports_dist, domain);
131
+ for await (const fileDirent of filesHandle) {
132
+ if (fileDirent.isDirectory()) {
133
+ yield* walkRecursively(path.join(basePath, fileDirent.name));
134
+ } else if (
135
+ fileDirent.isFile() &&
136
+ path.extname(fileDirent.name) === ".json" &&
137
+ fileDirent.name.includes("page-data")
138
+ ) {
139
+ yield path.join(basePath, fileDirent.name);
27
140
  }
28
141
  }
142
+ }
143
+
144
+ /**
145
+ * Walk recursively in a basePath and return an array of pages with json
146
+ * extension with the full absolute path and the path includes "page-data".
147
+ *
148
+ * @param basePath The path to walk recursively.
149
+ * @returns An array of pages with json extension with the full absolute path
150
+ * and the path includes "page-data".
151
+ */
152
+ async function* getPageDataPagesFromExports<PageType extends GatsbyPageData>(
153
+ basePath: string,
154
+ ): AsyncGenerator<PageType> {
155
+ const jsonFiles = walkRecursively(basePath);
156
+
157
+ for await (const filePath of jsonFiles) {
158
+ try {
159
+ const fileContent = await fsp.readFile(filePath, "utf8");
160
+ const page = JSON.parse(fileContent) as PageType;
29
161
 
30
- // Solo una vez en cada render de todos los dominios...
31
- if (envs.GRIDDO_AI_EMBEDDINGS) {
32
- await startAIEmbeddings();
162
+ if (page.path) {
163
+ yield page;
164
+ }
165
+ } catch (error) {
166
+ throwError(ReadFromStoreError, error);
167
+ }
33
168
  }
34
169
  }
35
170
 
171
+ async function getContentDirectories(domain: string) {
172
+ const { __exports } = await getRenderPathsFromDB({ domain });
173
+
174
+ return {
175
+ htmlContentDir: path.join(__exports, "dist"),
176
+ jsonContentDir: path.join(__exports, "dist", "page-data"),
177
+ };
178
+ }
179
+
180
+ async function uploadSearchContent() {
181
+ if (GRIDDO_SEARCH_FEATURE) {
182
+ await AuthService.login();
183
+ const domains = await getInstanceDomains();
184
+ for (const domain of domains) {
185
+ const { htmlContentDir, jsonContentDir } = await getContentDirectories(domain);
186
+ await uploadRenderedSearchContentToAPI({
187
+ htmlContentDir,
188
+ jsonContentDir,
189
+ });
190
+ }
191
+ }
192
+ }
193
+
194
+ async function aiEmbeddings() {
195
+ if (GRIDDO_SEARCH_FEATURE && GRIDDO_AI_EMBEDDINGS) {
196
+ await AuthService.login();
197
+ await post<AIEmbeddingsResponse>({
198
+ endpoint: AI_EMBEDDINGS,
199
+ useApiCacheDir: false,
200
+ });
201
+ }
202
+ }
203
+
204
+ async function main() {
205
+ await uploadSearchContent();
206
+ await aiEmbeddings();
207
+ }
208
+
36
209
  main().catch((err) => {
37
210
  console.error("Error", err?.stdout?.toString() || err);
38
211
  process.exit(1);
@@ -1,4 +1,5 @@
1
- import { GRIDDO_API_URL, GRIDDO_PUBLIC_API_URL } from "./envs";
1
+ const GRIDDO_API_URL = process.env.GRIDDO_API_URL;
2
+ const GRIDDO_PUBLIC_API_URL = process.env.GRIDDO_PUBLIC_API_URL;
2
3
 
3
4
  const WITH_URI = `${GRIDDO_API_URL}/site/`;
4
5
 
@@ -1,67 +1,72 @@
1
- /**
2
- * Here are all the environment variables that the CX code uses.
3
- */
4
-
5
- import dotenv from "dotenv";
1
+ // Here are all the environment variables that the CX code uses.
6
2
 
7
3
  import { isTruthy } from "../utils/core-utils";
8
4
 
9
- dotenv.config();
10
-
11
- //
12
5
  // Credentials
13
- //
14
- /* prettier-ignore */ const GRIDDO_API_URL = process.env.GRIDDO_API_URL || process.env.API_URL;
15
- /* prettier-ignore */ const GRIDDO_PUBLIC_API_URL = process.env.GRIDDO_PUBLIC_API_URL || process.env.PUBLIC_API_URL;
16
- /* prettier-ignore */ const GRIDDO_BOT_USER = process.env.botEmail || process.env.GRIDDO_BOT_USER;
17
- /* prettier-ignore */ const GRIDDO_BOT_PASSWORD = process.env.botPassword|| process.env.GRIDDO_BOT_PASSWORD;
6
+ const GRIDDO_API_URL = process.env.GRIDDO_API_URL || process.env.API_URL;
7
+ const GRIDDO_PUBLIC_API_URL = process.env.GRIDDO_PUBLIC_API_URL || process.env.PUBLIC_API_URL;
8
+ const GRIDDO_BOT_USER = process.env.botEmail || process.env.GRIDDO_BOT_USER;
9
+ const GRIDDO_BOT_PASSWORD = process.env.botPassword || process.env.GRIDDO_BOT_PASSWORD;
18
10
 
19
- //
20
11
  // Rendering
21
- //
22
- /* prettier-ignore */ const GRIDDO_API_CONCURRENCY_COUNT = Number.parseInt( process.env.GRIDDO_API_CONCURRENCY_COUNT || "10");
23
- /* prettier-ignore */ const GRIDDO_RENDER_ALL_SITES = isTruthy(process.env.GRIDDO_RENDER_ALL_SITES || process.env.updateAllSites);
24
- /* prettier-ignore */ const GRIDDO_RENDER_SITE = Number.parseInt(process.env.GRIDDO_RENDER_SITE || "")
25
- /* prettier-ignore */ const GRIDDO_RENDER_PAGES = (process.env.GRIDDO_RENDER_PAGES || "").split(",").map((item) => Number.parseInt(item)).filter(Boolean);
26
- /* prettier-ignore */ const GRIDDO_SKIP_BUILD_CHECKS = isTruthy(process.env.GRIDDO_SKIP_BUILD_CHECKS)
27
- /* prettier-ignore */ const GRIDDO_DEBUG_LOGS = isTruthy(process.env.GRIDDO_DEBUG_LOGS);
28
- /* prettier-ignore */ const GRIDDO_BUILD_LOGS = isTruthy(process.env.GRIDDO_BUILD_LOGS);
29
- /* prettier-ignore */ const GRIDDO_RENDER_BREAKPOINTS_FEATURE = isTruthy(process.env.GRIDDO_RENDER_BREAKPOINTS_FEATURE);
30
- /* prettier-ignore */ const GRIDDO_SSG_VERBOSE_LOGS = isTruthy(process.env.GRIDDO_SSG_VERBOSE_LOGS)
31
- /* prettier-ignore */ const GRIDDO_SEARCH_FEATURE = isTruthy(process.env.GRIDDO_SEARCH_FEATURE);
32
- /* prettier-ignore */ const GRIDDO_ASSET_PREFIX = process.env.GRIDDO_ASSET_PREFIX || process.env.ASSET_PREFIX;
33
- /* prettier-ignore */ const GRIDDO_REACT_APP_INSTANCE = process.env.GRIDDO_REACT_APP_INSTANCE || process.env.REACT_APP_INSTANCE;
34
- /* prettier-ignore */ const GRIDDO_AI_EMBEDDINGS = isTruthy(process.env.GRIDDO_AI_EMBEDDINGS);
35
- /* prettier-ignore */ const GRIDDO_VERBOSE_LOGS = isTruthy(process.env.GRIDDO_VERBOSE_LOGS);
36
- /* prettier-ignore */ const GRIDDO_ALERT_FEATURE = isTruthy(process.env.GRIDDO_ALERT_FEATURE);
37
- /* prettier-ignore */ const GRIDDO_API_MAX_RESPONSE_SIZE = Number.parseInt(process.env.GRIDDO_API_MAX_RESPONSE_SIZE || "81920") // 80KB
38
- /* prettier-ignore */ const GRIDDO_SSG_MAX_PAGE_SIZE = Number.parseInt(process.env.GRIDDO_SSG_MAX_PAGE_SIZE || "524288") // 512KB
12
+ const GRIDDO_API_CONCURRENCY_COUNT = Number.parseInt(
13
+ process.env.GRIDDO_API_CONCURRENCY_COUNT || "10",
14
+ );
15
+ const GRIDDO_RENDER_ALL_SITES = isTruthy(
16
+ process.env.GRIDDO_RENDER_ALL_SITES || process.env.updateAllSites,
17
+ );
18
+ const GRIDDO_RENDER_SITE = Number.parseInt(process.env.GRIDDO_RENDER_SITE || "");
19
+ const GRIDDO_RENDER_PAGES = (process.env.GRIDDO_RENDER_PAGES || "")
20
+ .split(",")
21
+ .map((item) => Number.parseInt(item))
22
+ .filter(Boolean);
23
+ const GRIDDO_SKIP_BUILD_CHECKS = isTruthy(process.env.GRIDDO_SKIP_BUILD_CHECKS);
24
+ const GRIDDO_DEBUG_LOGS = isTruthy(process.env.GRIDDO_DEBUG_LOGS);
25
+ const GRIDDO_BUILD_LOGS = isTruthy(process.env.GRIDDO_BUILD_LOGS);
26
+ const GRIDDO_RENDER_BREAKPOINTS_FEATURE = isTruthy(process.env.GRIDDO_RENDER_BREAKPOINTS_FEATURE);
27
+ const GRIDDO_SSG_VERBOSE_LOGS = isTruthy(process.env.GRIDDO_SSG_VERBOSE_LOGS);
28
+ const GRIDDO_SEARCH_FEATURE = isTruthy(process.env.GRIDDO_SEARCH_FEATURE);
29
+ const GRIDDO_ASSET_PREFIX = process.env.GRIDDO_ASSET_PREFIX || process.env.ASSET_PREFIX;
30
+ const GRIDDO_REACT_APP_INSTANCE =
31
+ process.env.GRIDDO_REACT_APP_INSTANCE || process.env.REACT_APP_INSTANCE;
32
+ const GRIDDO_AI_EMBEDDINGS = isTruthy(process.env.GRIDDO_AI_EMBEDDINGS);
33
+ const GRIDDO_VERBOSE_LOGS = isTruthy(process.env.GRIDDO_VERBOSE_LOGS);
39
34
 
40
- //
41
35
  // LifeCycle
42
- //
43
- /* prettier-ignore */ const GRIDDO_INIT_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_INIT_LIFECYCLE_MAX_ATTEMPTS || "1");
44
- /* prettier-ignore */ const GRIDDO_CLEAN_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_CLEAN_LIFECYCLE_MAX_ATTEMPTS || "1");
45
- /* prettier-ignore */ const GRIDDO_CLOSE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_CLOSE_LIFECYCLE_MAX_ATTEMPTS || "1");
46
- /* prettier-ignore */ const GRIDDO_PREPARE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_PREPARE_LIFECYCLE_MAX_ATTEMPTS || "1");
47
- /* prettier-ignore */ const GRIDDO_RESTORE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_RESTORE_LIFECYCLE_MAX_ATTEMPTS || "1");
48
- /* prettier-ignore */ const GRIDDO_DATA_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_DATA_LIFECYCLE_MAX_ATTEMPTS || "1");
49
- /* prettier-ignore */ const GRIDDO_SSG_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_SSG_LIFECYCLE_MAX_ATTEMPTS || "2");
50
- /* prettier-ignore */ const GRIDDO_RELOCATION_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_RELOCATION_LIFECYCLE_MAX_ATTEMPTS || "1");
51
- /* prettier-ignore */ const GRIDDO_META_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_META_LIFECYCLE_MAX_ATTEMPTS || "4");
52
- /* prettier-ignore */ const GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(process.env.GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS || "1");
53
-
54
- //
55
- // Testing
56
- //
57
- /* prettier-ignore */ const GRIDDO_FIXTURES_DOMAIN_NAMES = process.env.GRIDDO_CX_FIXTURES_DOMAIN_NAMES;
58
- /* prettier-ignore */ const GRIDDO_FIXTURES_SITE_NAMES = process.env.GRIDDO_CX_FIXTURES_SITE_NAMES;
36
+ const GRIDDO_INIT_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
37
+ process.env.GRIDDO_INIT_LIFECYCLE_MAX_ATTEMPTS || "1",
38
+ );
39
+ const GRIDDO_CLEAN_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
40
+ process.env.GRIDDO_CLEAN_LIFECYCLE_MAX_ATTEMPTS || "1",
41
+ );
42
+ const GRIDDO_CLOSE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
43
+ process.env.GRIDDO_CLOSE_LIFECYCLE_MAX_ATTEMPTS || "1",
44
+ );
45
+ const GRIDDO_PREPARE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
46
+ process.env.GRIDDO_PREPARE_LIFECYCLE_MAX_ATTEMPTS || "1",
47
+ );
48
+ const GRIDDO_RESTORE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
49
+ process.env.GRIDDO_RESTORE_LIFECYCLE_MAX_ATTEMPTS || "1",
50
+ );
51
+ const GRIDDO_DATA_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
52
+ process.env.GRIDDO_DATA_LIFECYCLE_MAX_ATTEMPTS || "1",
53
+ );
54
+ const GRIDDO_SSG_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
55
+ process.env.GRIDDO_SSG_LIFECYCLE_MAX_ATTEMPTS || "2",
56
+ );
57
+ const GRIDDO_RELOCATION_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
58
+ process.env.GRIDDO_RELOCATION_LIFECYCLE_MAX_ATTEMPTS || "1",
59
+ );
60
+ const GRIDDO_META_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
61
+ process.env.GRIDDO_META_LIFECYCLE_MAX_ATTEMPTS || "4",
62
+ );
63
+ const GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS = Number.parseInt(
64
+ process.env.GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS || "1",
65
+ );
59
66
 
60
67
  export {
61
68
  GRIDDO_AI_EMBEDDINGS,
62
- GRIDDO_ALERT_FEATURE,
63
69
  GRIDDO_API_CONCURRENCY_COUNT,
64
- GRIDDO_API_MAX_RESPONSE_SIZE,
65
70
  GRIDDO_API_URL,
66
71
  GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS,
67
72
  GRIDDO_ASSET_PREFIX,
@@ -72,8 +77,6 @@ export {
72
77
  GRIDDO_CLOSE_LIFECYCLE_MAX_ATTEMPTS,
73
78
  GRIDDO_DATA_LIFECYCLE_MAX_ATTEMPTS,
74
79
  GRIDDO_DEBUG_LOGS,
75
- GRIDDO_FIXTURES_DOMAIN_NAMES,
76
- GRIDDO_FIXTURES_SITE_NAMES,
77
80
  GRIDDO_INIT_LIFECYCLE_MAX_ATTEMPTS,
78
81
  GRIDDO_META_LIFECYCLE_MAX_ATTEMPTS,
79
82
  GRIDDO_PREPARE_LIFECYCLE_MAX_ATTEMPTS,
@@ -88,7 +91,6 @@ export {
88
91
  GRIDDO_SEARCH_FEATURE,
89
92
  GRIDDO_SKIP_BUILD_CHECKS,
90
93
  GRIDDO_SSG_LIFECYCLE_MAX_ATTEMPTS,
91
- GRIDDO_SSG_MAX_PAGE_SIZE,
92
94
  GRIDDO_SSG_VERBOSE_LOGS,
93
95
  GRIDDO_VERBOSE_LOGS,
94
96
  };
@@ -7,11 +7,13 @@
7
7
  * obligatorily.
8
8
  */
9
9
 
10
- import type { ErrorData } from ".";
11
10
  import type { SpawnSyncReturns } from "node:child_process";
11
+ import type { ErrorData } from "../utils/errors";
12
12
 
13
13
  type ErrorsType =
14
14
  | "ArtifactError"
15
+ | "BundlesInconsistencyError"
16
+ | "CheckHealthError"
15
17
  | "ErrorInSSGBuildProcess"
16
18
  | "LifecycleExecutionError"
17
19
  | "LoginError"
@@ -20,6 +22,7 @@ type ErrorsType =
20
22
  | "ReadFromStoreError"
21
23
  | "ReferenceFieldSourcesNotFoundError"
22
24
  | "RenderUUIDError"
25
+ | "UploadSearchError"
23
26
  | "WriteToStoreError";
24
27
 
25
28
  const ArtifactError: ErrorData = {
@@ -30,19 +33,13 @@ const ArtifactError: ErrorData = {
30
33
  hint: "Have there been any recent deployments? These can delete directories from the current render.",
31
34
  };
32
35
 
33
- const ErrorInSSGBuildProcess = (
34
- command: SpawnSyncReturns<string>,
35
- ): ErrorData => ({
36
+ const ErrorInSSGBuildProcess = (command: SpawnSyncReturns<string>): ErrorData => ({
36
37
  error: "ErrorInSSGBuildProcess",
37
38
  message: `Error in SSG build process: ${JSON.stringify(command)}`,
38
- expected:
39
- "This can happen if there was a problem with the SSG build process.",
39
+ expected: "This can happen if there was a problem with the SSG build process.",
40
40
  });
41
41
 
42
- const LifecycleExecutionError = (
43
- attempts: number,
44
- name: string,
45
- ): ErrorData => ({
42
+ const LifecycleExecutionError = (attempts: number, name: string): ErrorData => ({
46
43
  error: "LifecycleExecutionError",
47
44
  message: `Exceeded maximum retry attempts (${attempts}) for ${name} LifeCycle`,
48
45
  });
@@ -50,14 +47,12 @@ const LifecycleExecutionError = (
50
47
  const LoginError: ErrorData = {
51
48
  error: "LoginError",
52
49
  message: "There was a problem logging in to the API",
53
- expected:
54
- "This happens if the API is currently not working or the credentials are incorrect.",
50
+ expected: "This happens if the API is currently not working or the credentials are incorrect.",
55
51
  };
56
52
 
57
53
  const NoDomainsFoundError: ErrorData = {
58
54
  error: "NoDomainsFoundError",
59
- message:
60
- "No domains were found in this instance. The process cannot continue.",
55
+ message: "No domains were found in this instance. The process cannot continue.",
61
56
  expected:
62
57
  "This may happen if the API is not functioning, or the site is not properly configured, or the domains are not registered.",
63
58
  hint: "You can contact the instance administrator.",
@@ -98,8 +93,22 @@ const WriteToStoreError: ErrorData = {
98
93
  hint: "There may be an issue such as lack of space or permissions preventing the file from being written.",
99
94
  };
100
95
 
96
+ const UploadSearchError: ErrorData = {
97
+ error: "UploadSearchError",
98
+ message: "There was an error uploading content to API for search",
99
+ hint: "This happens if the API is currently not working or the credentials are incorrect.",
100
+ };
101
+
102
+ const CheckHealthError: ErrorData = {
103
+ error: "CheckHealthError",
104
+ message: "There was a problem with environment vars configuration.",
105
+ expected: "Some of the required environment variables are not set correctly or are missing",
106
+ hint: "Are the environment variables correctly set?",
107
+ };
108
+
101
109
  export {
102
110
  ArtifactError,
111
+ CheckHealthError,
103
112
  ErrorInSSGBuildProcess,
104
113
  LifecycleExecutionError,
105
114
  LoginError,
@@ -108,6 +117,7 @@ export {
108
117
  ReadFromStoreError,
109
118
  ReferenceFieldSourcesNotFoundError,
110
119
  RenderUUIDError,
120
+ UploadSearchError,
111
121
  WriteToStoreError,
112
122
  type ErrorsType,
113
123
  };
@@ -1,8 +1,9 @@
1
1
  import type { AuthHeaders } from "../types/api";
2
2
 
3
- import { endpoints, envs } from "../constants";
4
- import { throwError } from "../errors";
5
- import { LoginError } from "../errors/errors-data";
3
+ import { LOGIN } from "../constants/endpoints";
4
+ import { GRIDDO_BOT_PASSWORD, GRIDDO_BOT_USER } from "../constants/envs";
5
+ import { LoginError } from "../constants/errors";
6
+ import { throwError } from "../utils/errors";
6
7
 
7
8
  /**
8
9
  * Service for authentication in the Griddo Private API
@@ -12,12 +13,12 @@ class AuthService {
12
13
 
13
14
  async login() {
14
15
  try {
15
- const response = await fetch(endpoints.LOGIN, {
16
+ const response = await fetch(LOGIN, {
16
17
  method: "POST",
17
18
  headers: { "Content-Type": "application/json", Connection: "close" },
18
19
  body: JSON.stringify({
19
- username: envs.GRIDDO_BOT_USER,
20
- password: envs.GRIDDO_BOT_PASSWORD,
20
+ username: GRIDDO_BOT_USER,
21
+ password: GRIDDO_BOT_PASSWORD,
21
22
  }),
22
23
  });
23
24
 
@@ -0,0 +1,54 @@
1
+ import type { RenderDB } from "../types/render";
2
+
3
+ import fsp from "node:fs/promises";
4
+ import path from "node:path";
5
+
6
+ import { pkgDirSync } from "../utils/npm-deps/pkg-dir";
7
+
8
+ // Interfaz para abstraer la DB
9
+ export interface Database {
10
+ read(): Promise<RenderDB>;
11
+ write(renderDB: RenderDB): Promise<void>;
12
+ }
13
+
14
+ export class JsonDatabase implements Database {
15
+ private readonly dbFilePath: string;
16
+
17
+ constructor(customDBPath?: string) {
18
+ this.dbFilePath =
19
+ customDBPath ??
20
+ path.join(
21
+ pkgDirSync({ cwd: path.resolve(__dirname, "../../..") }) ?? "",
22
+ ".cx-cache",
23
+ "db.json",
24
+ );
25
+ }
26
+
27
+ async read(customDBFilePath?: string): Promise<RenderDB> {
28
+ try {
29
+ const raw = await fsp.readFile(customDBFilePath || this.dbFilePath, "utf-8");
30
+ return JSON.parse(raw) as RenderDB;
31
+ } catch (error) {
32
+ if (error instanceof Error) {
33
+ console.error(`Error reading DB file at ${this.dbFilePath}:`, error.message);
34
+ } else {
35
+ console.error(`Unknown error reading DB file at ${this.dbFilePath}:`, error);
36
+ }
37
+ throw error;
38
+ }
39
+ }
40
+
41
+ async write(renderDB: RenderDB): Promise<void> {
42
+ try {
43
+ await fsp.mkdir(path.dirname(this.dbFilePath), { recursive: true });
44
+ await fsp.writeFile(this.dbFilePath, JSON.stringify(renderDB, null, 2));
45
+ } catch (error) {
46
+ if (error instanceof Error) {
47
+ console.error(`Error writing to DB file at ${this.dbFilePath}:`, error.message);
48
+ } else {
49
+ console.error(`Unknown error writing to DB file at ${this.dbFilePath}:`, error);
50
+ }
51
+ throw error;
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,32 @@
1
+ import type { RenderDB } from "../types/render";
2
+
3
+ import fsp from "node:fs/promises";
4
+ import path from "node:path";
5
+
6
+ import { pkgDirSync } from "../utils/npm-deps/pkg-dir";
7
+
8
+ const root = pkgDirSync({ cwd: path.resolve(__dirname, "../../..") }) || "";
9
+ const cache = path.join(root, ".cx-cache");
10
+ const dbFilePath = path.join(cache, "db.json");
11
+
12
+ async function readDB(customDBPath = "") {
13
+ const file = customDBPath || dbFilePath;
14
+ try {
15
+ return JSON.parse(await fsp.readFile(file, "utf-8")) as RenderDB;
16
+ } catch (error) {
17
+ console.error(`Failed to read DB file at ${file}:`, error);
18
+ throw error;
19
+ }
20
+ }
21
+
22
+ async function writeDB(renderDB: RenderDB, customDBPath = "") {
23
+ const file = customDBPath || dbFilePath;
24
+ try {
25
+ await fsp.writeFile(file, JSON.stringify(renderDB));
26
+ } catch (error) {
27
+ console.error(`Failed to write DB file at ${file}:`, error);
28
+ throw error;
29
+ }
30
+ }
31
+
32
+ export { readDB, writeDB };
@@ -68,9 +68,7 @@ class NavigationService {
68
68
  const rightLanguageItem = list.find(
69
69
  (item) =>
70
70
  item.language === language &&
71
- item.navigationLanguages?.find(
72
- (version) => version.navigationId === id,
73
- ),
71
+ item.navigationLanguages?.find((version) => version.navigationId === id),
74
72
  );
75
73
 
76
74
  const result = rightLanguageItem || list.find((item) => item.id === id);
@@ -87,13 +85,9 @@ class NavigationService {
87
85
  }
88
86
 
89
87
  getPageNavigations(page: APIPageObject) {
90
- const {
91
- header: pageHeader,
92
- footer: pageFooter,
93
- language,
94
- template: { templateType },
95
- templateConfig: { defaultHeader, defaultFooter, templates },
96
- } = page;
88
+ const { header: pageHeader, footer: pageFooter, language, template, templateConfig } = page;
89
+ const { templateType } = template;
90
+ const { defaultHeader, defaultFooter, templates } = templateConfig;
97
91
 
98
92
  // The navigations would be:
99
93
  // - The one with the page or ...