@griddo/cx 11.7.5 → 11.7.6-rc.0

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 (300) hide show
  1. package/README.md +25 -228
  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 +12 -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 +9 -0
  11. package/build/adapters/gatsby/actions/init.js.map +1 -0
  12. package/build/adapters/gatsby/actions/meta.js +14 -0
  13. package/build/adapters/gatsby/actions/meta.js.map +1 -0
  14. package/build/adapters/gatsby/actions/prepare.js +9 -0
  15. package/build/adapters/gatsby/actions/prepare.js.map +1 -0
  16. package/build/adapters/gatsby/actions/relocation.js +15 -0
  17. package/build/adapters/gatsby/actions/relocation.js.map +1 -0
  18. package/build/adapters/gatsby/actions/restore.js +26 -0
  19. package/build/adapters/gatsby/actions/restore.js.map +1 -0
  20. package/build/adapters/gatsby/actions/ssg.js +9 -0
  21. package/build/adapters/gatsby/actions/ssg.js.map +1 -0
  22. package/build/adapters/gatsby/actions/sync.js +50 -0
  23. package/build/adapters/gatsby/actions/sync.js.map +1 -0
  24. package/build/adapters/gatsby/index.js +74 -0
  25. package/build/adapters/gatsby/index.js.map +1 -0
  26. package/build/adapters/gatsby/shared/context.js +25 -0
  27. package/build/adapters/gatsby/shared/context.js.map +1 -0
  28. package/build/adapters/gatsby/shared/extract-assets.js +58 -0
  29. package/build/adapters/gatsby/shared/extract-assets.js.map +1 -0
  30. package/build/adapters/gatsby/shared/gatsby-build.js +49 -0
  31. package/build/adapters/gatsby/shared/gatsby-build.js.map +1 -0
  32. package/build/adapters/gatsby/shared/sync-render.js +208 -0
  33. package/build/adapters/gatsby/shared/sync-render.js.map +1 -0
  34. package/build/adapters/gatsby/shared/types.js +3 -0
  35. package/build/adapters/gatsby/shared/types.js.map +1 -0
  36. package/build/commands/end-render.js +63 -0
  37. package/build/commands/end-render.js.map +1 -0
  38. package/build/commands/prepare-assets-directory.js +32 -0
  39. package/build/commands/prepare-assets-directory.js.map +1 -0
  40. package/build/commands/prepare-domains-render.js +138 -0
  41. package/build/commands/prepare-domains-render.js.map +1 -0
  42. package/build/commands/reset-render.js +22 -0
  43. package/build/commands/reset-render.js.map +1 -0
  44. package/build/commands/start-render.js +35 -0
  45. package/build/commands/start-render.js.map +1 -0
  46. package/build/commands/upload-search-content.js +189 -0
  47. package/build/commands/upload-search-content.js.map +1 -0
  48. package/build/constants/endpoints.js +42 -0
  49. package/build/constants/endpoints.js.map +1 -0
  50. package/build/constants/envs.js +73 -0
  51. package/build/constants/envs.js.map +1 -0
  52. package/build/constants/errors.js +89 -0
  53. package/build/constants/errors.js.map +1 -0
  54. package/build/exporter/adapters/gatsby/actions/clean.d.ts +3 -0
  55. package/build/exporter/adapters/gatsby/actions/close.d.ts +3 -0
  56. package/build/exporter/adapters/gatsby/actions/data.d.ts +2 -0
  57. package/build/exporter/adapters/gatsby/actions/healthCheck.d.ts +2 -0
  58. package/build/exporter/adapters/gatsby/actions/init.d.ts +2 -0
  59. package/build/exporter/adapters/gatsby/actions/meta.d.ts +2 -0
  60. package/build/exporter/adapters/gatsby/actions/prepare.d.ts +2 -0
  61. package/build/exporter/adapters/gatsby/actions/relocation.d.ts +2 -0
  62. package/build/exporter/adapters/gatsby/actions/restore.d.ts +3 -0
  63. package/build/exporter/adapters/gatsby/actions/ssg.d.ts +3 -0
  64. package/build/exporter/adapters/gatsby/actions/sync.d.ts +3 -0
  65. package/build/exporter/adapters/gatsby/index.d.ts +9 -0
  66. package/build/exporter/adapters/gatsby/shared/context.d.ts +38 -0
  67. package/build/exporter/adapters/gatsby/shared/extract-assets.d.ts +7 -0
  68. package/build/exporter/adapters/gatsby/shared/gatsby-build.d.ts +7 -0
  69. package/build/exporter/adapters/gatsby/shared/sync-render.d.ts +36 -0
  70. package/build/exporter/adapters/gatsby/shared/types.d.ts +34 -0
  71. package/build/exporter/commands/start-render.d.ts +2 -0
  72. package/build/exporter/commands/upload-search-content.d.ts +2 -0
  73. package/build/{constants → exporter/constants}/envs.d.ts +1 -7
  74. package/build/{errors/errors-data.d.ts → exporter/constants/errors.d.ts} +5 -3
  75. package/build/{services → exporter/services}/auth.d.ts +1 -1
  76. package/build/exporter/services/db.d.ts +4 -0
  77. package/build/{services → exporter/services}/robots.d.ts +0 -6
  78. package/build/{services → exporter/services}/sites.d.ts +5 -2
  79. package/build/exporter/services/store.d.ts +15 -0
  80. package/build/{types → exporter/types}/api.d.ts +3 -4
  81. package/build/{types → exporter/types}/global.d.ts +6 -6
  82. package/build/{types → exporter/types}/navigation.d.ts +1 -1
  83. package/build/exporter/types/render.d.ts +50 -0
  84. package/build/{types → exporter/types}/sites.d.ts +1 -2
  85. package/build/exporter/utils/artifacts.d.ts +6 -0
  86. package/build/exporter/utils/brush.d.ts +18 -0
  87. package/build/{utils → exporter/utils}/cache.d.ts +7 -12
  88. package/build/{utils/health-checks.d.ts → exporter/utils/check-health.d.ts} +2 -2
  89. package/build/exporter/utils/core-utils.d.ts +77 -0
  90. package/build/{utils → exporter/utils}/domains.d.ts +1 -1
  91. package/build/{errors/index.d.ts → exporter/utils/errors.d.ts} +3 -3
  92. package/build/exporter/utils/folders.d.ts +66 -0
  93. package/build/{utils → exporter/utils}/loggin.d.ts +2 -16
  94. package/build/{utils → exporter/utils}/pages.d.ts +2 -2
  95. package/build/exporter/utils/render.d.ts +49 -0
  96. package/build/{utils → exporter/utils}/sites.d.ts +3 -3
  97. package/build/{utils → exporter/utils}/store.d.ts +10 -38
  98. package/build/services/auth.js +39 -0
  99. package/build/services/auth.js.map +1 -0
  100. package/build/services/db.js +34 -0
  101. package/build/services/db.js.map +1 -0
  102. package/build/services/navigation.js +93 -0
  103. package/build/services/navigation.js.map +1 -0
  104. package/build/services/reference-fields.js +131 -0
  105. package/build/services/reference-fields.js.map +1 -0
  106. package/build/services/robots.js +58 -0
  107. package/build/services/robots.js.map +1 -0
  108. package/build/services/sites.js +109 -0
  109. package/build/services/sites.js.map +1 -0
  110. package/build/services/store.js +282 -0
  111. package/build/services/store.js.map +1 -0
  112. package/build/types/api.js +3 -0
  113. package/build/types/api.js.map +1 -0
  114. package/build/types/global.js +3 -0
  115. package/build/types/global.js.map +1 -0
  116. package/build/types/navigation.js +3 -0
  117. package/build/types/navigation.js.map +1 -0
  118. package/build/types/pages.js +3 -0
  119. package/build/types/pages.js.map +1 -0
  120. package/build/types/render.js +10 -0
  121. package/build/types/render.js.map +1 -0
  122. package/build/types/sites.js +3 -0
  123. package/build/types/sites.js.map +1 -0
  124. package/build/types/templates.js +3 -0
  125. package/build/types/templates.js.map +1 -0
  126. package/build/utils/api.js +161 -0
  127. package/build/utils/api.js.map +1 -0
  128. package/build/utils/artifacts.js +34 -0
  129. package/build/utils/artifacts.js.map +1 -0
  130. package/build/utils/brush.js +30 -0
  131. package/build/utils/brush.js.map +1 -0
  132. package/build/utils/cache.js +106 -0
  133. package/build/utils/cache.js.map +1 -0
  134. package/build/utils/check-health.js +68 -0
  135. package/build/utils/check-health.js.map +1 -0
  136. package/build/utils/core-utils.js +251 -0
  137. package/build/utils/core-utils.js.map +1 -0
  138. package/build/utils/domains.js +37 -0
  139. package/build/utils/domains.js.map +1 -0
  140. package/build/utils/errors.js +30 -0
  141. package/build/utils/errors.js.map +1 -0
  142. package/build/utils/folders.js +339 -0
  143. package/build/utils/folders.js.map +1 -0
  144. package/build/utils/images.js +45 -0
  145. package/build/utils/images.js.map +1 -0
  146. package/build/utils/instance.js +66 -0
  147. package/build/utils/instance.js.map +1 -0
  148. package/build/utils/loggin.js +102 -0
  149. package/build/utils/loggin.js.map +1 -0
  150. package/build/utils/pages.js +359 -0
  151. package/build/utils/pages.js.map +1 -0
  152. package/build/utils/render.js +144 -0
  153. package/build/utils/render.js.map +1 -0
  154. package/build/utils/sites.js +239 -0
  155. package/build/utils/sites.js.map +1 -0
  156. package/build/utils/store.js +193 -0
  157. package/build/utils/store.js.map +1 -0
  158. package/exporter/adapters/gatsby/actions/clean.ts +14 -0
  159. package/exporter/adapters/gatsby/actions/close.ts +17 -0
  160. package/exporter/adapters/gatsby/actions/data.ts +25 -0
  161. package/exporter/adapters/gatsby/actions/healthCheck.ts +10 -0
  162. package/exporter/adapters/gatsby/actions/init.ts +12 -0
  163. package/exporter/adapters/gatsby/actions/meta.ts +18 -0
  164. package/exporter/adapters/gatsby/actions/prepare.ts +9 -0
  165. package/exporter/adapters/gatsby/actions/relocation.ts +15 -0
  166. package/exporter/adapters/gatsby/actions/restore.ts +36 -0
  167. package/exporter/adapters/gatsby/actions/ssg.ts +12 -0
  168. package/exporter/adapters/gatsby/actions/sync.ts +70 -0
  169. package/exporter/adapters/gatsby/index.ts +60 -146
  170. package/exporter/adapters/gatsby/shared/context.ts +50 -0
  171. package/exporter/adapters/gatsby/shared/extract-assets.ts +61 -0
  172. package/exporter/adapters/gatsby/shared/gatsby-build.ts +54 -0
  173. package/exporter/adapters/gatsby/shared/sync-render.ts +294 -0
  174. package/exporter/adapters/gatsby/shared/types.ts +35 -0
  175. package/exporter/build-esbuild.noop +42 -0
  176. package/exporter/build.sh +5 -38
  177. package/exporter/commands/end-render.ts +56 -81
  178. package/exporter/commands/prepare-assets-directory.ts +33 -0
  179. package/exporter/commands/prepare-domains-render.ts +149 -31
  180. package/exporter/commands/reset-render.ts +12 -3
  181. package/exporter/commands/start-render.ts +18 -47
  182. package/exporter/commands/upload-search-content.ts +206 -21
  183. package/exporter/constants/envs.ts +59 -51
  184. package/exporter/{errors/errors-data.ts → constants/errors.ts} +24 -15
  185. package/exporter/services/auth.ts +7 -6
  186. package/exporter/services/db.ts +32 -0
  187. package/exporter/services/navigation.ts +4 -10
  188. package/exporter/services/reference-fields.ts +8 -23
  189. package/exporter/services/robots.ts +8 -15
  190. package/exporter/services/sites.ts +35 -24
  191. package/exporter/services/store.ts +118 -76
  192. package/exporter/types/api.ts +24 -27
  193. package/exporter/types/global.ts +8 -10
  194. package/exporter/types/navigation.ts +1 -1
  195. package/exporter/types/pages.ts +2 -3
  196. package/exporter/types/render.ts +59 -0
  197. package/exporter/types/sites.ts +1 -2
  198. package/exporter/utils/api.ts +46 -82
  199. package/exporter/{artifacts/index.ts → utils/artifacts.ts} +9 -8
  200. package/exporter/utils/brush.ts +34 -0
  201. package/exporter/utils/cache.ts +34 -59
  202. package/exporter/utils/check-health.ts +79 -0
  203. package/exporter/utils/core-utils.ts +86 -247
  204. package/exporter/utils/domains.ts +10 -7
  205. package/exporter/{errors/index.ts → utils/errors.ts} +10 -9
  206. package/exporter/utils/folders.ts +161 -92
  207. package/exporter/utils/images.ts +1 -6
  208. package/exporter/utils/instance.ts +9 -12
  209. package/exporter/utils/loggin.ts +24 -89
  210. package/exporter/utils/pages.ts +23 -88
  211. package/exporter/utils/render.ts +147 -48
  212. package/exporter/utils/sites.ts +67 -77
  213. package/exporter/utils/store.ts +85 -178
  214. package/gatsby-browser.tsx +41 -58
  215. package/gatsby-config.ts +6 -12
  216. package/gatsby-node.ts +30 -89
  217. package/gatsby-ssr.tsx +2 -1
  218. package/package.json +32 -73
  219. package/plugins/gatsby-plugin-svgr-loader/gatsby-node.js +55 -0
  220. package/plugins/gatsby-plugin-svgr-loader/package.json +8 -0
  221. package/{exporter/react → react}/Favicon/index.tsx +1 -7
  222. package/{exporter/react → react}/GriddoIntegrations/index.tsx +6 -14
  223. package/{exporter/react → react}/GriddoIntegrations/utils.ts +4 -9
  224. package/{build/react/index.d.ts → react/index.tsx} +1 -0
  225. package/src/components/Head.tsx +13 -46
  226. package/src/components/template.tsx +8 -30
  227. package/src/gatsby-node-utils.ts +91 -2
  228. package/src/html.tsx +2 -11
  229. package/src/types.ts +5 -5
  230. package/start-render.js +1 -3
  231. package/tsconfig.json +3 -2
  232. package/build/adapters/gatsby/index.d.ts +0 -4
  233. package/build/adapters/gatsby/utils.d.ts +0 -22
  234. package/build/artifacts/index.d.ts +0 -6
  235. package/build/commands/move-assets.d.ts +0 -1
  236. package/build/commands/prepare-domains-render.d.ts +0 -1
  237. package/build/constants/index.d.ts +0 -57
  238. package/build/end-render.js +0 -74
  239. package/build/end-render.js.map +0 -7
  240. package/build/index.d.ts +0 -29
  241. package/build/index.js +0 -73
  242. package/build/index.js.map +0 -7
  243. package/build/prepare-domains-render.js +0 -73
  244. package/build/prepare-domains-render.js.map +0 -7
  245. package/build/react/Favicon/index.d.ts +0 -5
  246. package/build/react/Favicon/utils.d.ts +0 -9
  247. package/build/react/GriddoIntegrations/index.d.ts +0 -20
  248. package/build/react/GriddoIntegrations/utils.d.ts +0 -26
  249. package/build/react/index.js +0 -3
  250. package/build/registers/api.d.ts +0 -9
  251. package/build/registers/gatsby.d.ts +0 -9
  252. package/build/registers/index.d.ts +0 -3
  253. package/build/reset-render.js +0 -74
  254. package/build/reset-render.js.map +0 -7
  255. package/build/services/domains.d.ts +0 -6
  256. package/build/services/register.d.ts +0 -36
  257. package/build/services/settings.d.ts +0 -4
  258. package/build/services/store.d.ts +0 -6
  259. package/build/start-render.js +0 -100
  260. package/build/start-render.js.map +0 -7
  261. package/build/upload-search-content.js +0 -74
  262. package/build/upload-search-content.js.map +0 -7
  263. package/build/utils/alerts.d.ts +0 -3
  264. package/build/utils/core-utils.d.ts +0 -107
  265. package/build/utils/create-build-data.d.ts +0 -8
  266. package/build/utils/folders.d.ts +0 -53
  267. package/build/utils/render.d.ts +0 -13
  268. package/build/utils/searches.d.ts +0 -15
  269. package/cx.config.d.ts +0 -5
  270. package/cx.config.js +0 -36
  271. package/exporter/adapters/gatsby/utils.ts +0 -161
  272. package/exporter/artifacts/README.md +0 -34
  273. package/exporter/commands/move-assets.ts +0 -11
  274. package/exporter/constants/index.ts +0 -129
  275. package/exporter/index.ts +0 -82
  276. package/exporter/react/index.tsx +0 -11
  277. package/exporter/registers/api.ts +0 -14
  278. package/exporter/registers/gatsby.ts +0 -14
  279. package/exporter/registers/index.ts +0 -4
  280. package/exporter/services/domains.ts +0 -16
  281. package/exporter/services/register.ts +0 -113
  282. package/exporter/services/settings.ts +0 -17
  283. package/exporter/utils/alerts.ts +0 -29
  284. package/exporter/utils/create-build-data.ts +0 -17
  285. package/exporter/utils/health-checks.ts +0 -64
  286. package/exporter/utils/searches.ts +0 -179
  287. package/src/README.md +0 -7
  288. package/build/{commands → exporter/commands}/end-render.d.ts +0 -0
  289. package/build/{commands/reset-render.d.ts → exporter/commands/prepare-assets-directory.d.ts} +0 -0
  290. package/build/{commands/start-render.d.ts → exporter/commands/prepare-domains-render.d.ts} +0 -0
  291. package/build/{commands/upload-search-content.d.ts → exporter/commands/reset-render.d.ts} +0 -0
  292. package/build/{constants → exporter/constants}/endpoints.d.ts +0 -0
  293. package/build/{services → exporter/services}/navigation.d.ts +0 -0
  294. package/build/{services → exporter/services}/reference-fields.d.ts +0 -0
  295. package/build/{types → exporter/types}/pages.d.ts +1 -1
  296. /package/build/{types → exporter/types}/templates.d.ts +0 -0
  297. /package/build/{utils → exporter/utils}/api.d.ts +0 -0
  298. /package/build/{utils → exporter/utils}/images.d.ts +0 -0
  299. /package/build/{utils → exporter/utils}/instance.d.ts +0 -0
  300. /package/{exporter/react → react}/Favicon/utils.ts +0 -0
@@ -1,50 +1,16 @@
1
1
  import type { APIResponses } from "../types/api";
2
- import type {
3
- CXConfig,
4
- LifeCyclesNames,
5
- LifeCycleSteps,
6
- } from "../types/global";
7
- import type { APIPageObject } from "../types/pages";
8
-
9
- import fs from "node:fs";
10
- import path from "node:path";
11
-
12
- import fsx from "fs-extra";
13
- import kleur from "kleur";
14
- import { bgRed, black } from "kleur/colors";
15
- import pkgDir from "pkg-dir";
2
+ import type { LifeCycleAction, LifeCyclesNames } from "../types/global";
16
3
 
17
- import { envs } from "../constants";
18
- import { throwError } from "../errors";
19
- import { prependFileSync } from "./folders";
20
- import { boxLog, infoLog, successLog } from "./loggin";
21
- import { generateBuildReport, generateSitemaps } from "./sites";
22
- import { LifecycleExecutionError } from "../errors/errors-data";
23
- import { RobotsService } from "../services/robots";
24
-
25
- const config = getConfig();
4
+ import fsp from "node:fs/promises";
5
+ import path from "node:path";
26
6
 
27
- const instanceRootDir = pkgDir.sync()!; // instance root dir
28
-
29
- /**
30
- * Returns the configuration file content.
31
- *
32
- * @example
33
- * const config = getConfig()
34
- * const { __cx } = config.paths()
35
- * const { griddoVersion, proDomain } = config
36
- */
37
- function getConfig(): CXConfig {
38
- try {
39
- const configModule = require("../../cx.config.js");
40
- const config = configModule.default;
41
-
42
- return config;
43
- } catch (error) {
44
- console.log(error);
45
- throw new Error("Error while reading configuration file");
46
- }
47
- }
7
+ import { LifecycleExecutionError } from "../constants/errors";
8
+ import { readDB } from "../services/db";
9
+ import { brush } from "./brush";
10
+ import { throwError } from "./errors";
11
+ import { pathExists, prependFileSync } from "./folders";
12
+ import { infoLog, successLog } from "./loggin";
13
+ import { getRenderPathsFromDB } from "./render";
48
14
 
49
15
  /**
50
16
  * Returns true for "true", "on", true and positive numbers.
@@ -90,53 +56,25 @@ function isTruthy(value: any): boolean {
90
56
  return false;
91
57
  }
92
58
 
93
- /**
94
- * Walk a directory and returns the file pathts.
95
- *
96
- * @param dir A directory path.
97
- */
98
- function walk(dir: string) {
99
- const results: Array<string> = [];
100
- const list = fs.readdirSync(dir);
101
- for (const file of list) {
102
- results.push(path.join(dir, file));
103
- }
104
-
105
- return results;
106
- }
107
-
108
59
  /**
109
60
  * Walk a directory and returns the JSON file absolute paths with one level of depth.
110
- * Bypass the `metadata` folder.
111
61
  * /abs/.../sotre/<siteId>/jsonfile.json
112
62
  * /abs/.../sotre/<siteId>/jsonfile.json
113
63
  * /abs/.../sotre/<siteId>/jsonfile.json
114
64
  * /abs/.../sotre/<siteId>/jsonfile.json
115
65
  */
116
- function walkStore(dir: string): Array<string> {
117
- const results: Array<string> = [];
118
-
119
- // Listamos todas las subcarpetas y evitamos entrar en 'metadata'
120
- const subdirs = fs.readdirSync(dir).filter((file) => {
121
- const fullPath = path.join(dir, file);
122
- return fs.statSync(fullPath).isDirectory() && file !== "metadata";
123
- });
124
-
125
- // Iteramos sobre las subcarpetas
126
- for (const subdir of subdirs) {
127
- const subdirPath = path.join(dir, subdir);
128
-
129
- // Listamos los archivos en cada subcarpeta y filtramos los .json
130
- const files = fs
131
- .readdirSync(subdirPath)
132
- .filter((file) => file.endsWith(".json"));
133
-
134
- for (const file of files) {
135
- results.push(path.join(subdirPath, file));
66
+ async function* walkStore(storeDir: string): AsyncGenerator<string> {
67
+ const rootDirHandle = await fsp.opendir(storeDir);
68
+ for await (const siteDirent of rootDirHandle) {
69
+ if (siteDirent.isDirectory()) {
70
+ const siteDirPath = path.join(storeDir, siteDirent.name);
71
+ const siteDirHandle = await fsp.opendir(siteDirPath);
72
+ for await (const jsonFileDirent of siteDirHandle) {
73
+ const jsonFilePath = path.join(siteDirPath, jsonFileDirent.name);
74
+ yield jsonFilePath;
75
+ }
136
76
  }
137
77
  }
138
-
139
- return results;
140
78
  }
141
79
 
142
80
  /**
@@ -187,70 +125,6 @@ function removeProperties(obj: Record<string, unknown>, props: Array<string>) {
187
125
  }
188
126
  }
189
127
 
190
- /**
191
- * Remove unused files (old) inside the `apiCache` dir
192
- *
193
- * @todo remove other file types: sites, socials, etc..
194
- */
195
- function sanitizeAPICacheDir() {
196
- const { __cx } = config.paths();
197
- const dirPath = path.join(__cx, "apiCache");
198
- const allCachedFiles = fs.readdirSync(dirPath);
199
-
200
- // Object to store the the more rencent file names
201
- // Record<string, string> = { "234856872634", "3268746238747238.json"};
202
- const filesByIdMap: Record<string, string> = {};
203
- // ^id ^path
204
-
205
- // Page files.
206
- // We only need files that describes a page object.
207
- const pageFilePaths = allCachedFiles.filter((fileName) => {
208
- const filePath = `${dirPath}/${fileName}`;
209
- const fileObject = fsx.readJSONSync(filePath, "utf-8") as APIPageObject;
210
- const { id, entity, fullUrl } = fileObject;
211
-
212
- // Is a page file if has id, entity and fullUrl
213
- return !!(id && entity && fullUrl);
214
- });
215
-
216
- // Fill the filesById object
217
- for (const fileName of pageFilePaths) {
218
- const filePath = `${dirPath}/${fileName}`;
219
- const fileObject = fsx.readJSONSync(filePath, "utf-8") as APIPageObject;
220
- const fileCreationDate = fs.statSync(filePath).mtimeMs;
221
-
222
- const { id } = fileObject;
223
-
224
- // Is a valid page if doesn't exists in the store object or is newer
225
- // that the stored one.
226
- const validPageFile =
227
- !filesByIdMap[id] ||
228
- fileCreationDate > fs.statSync(`${dirPath}/${filesByIdMap[id]}`).mtimeMs;
229
-
230
- if (validPageFile) {
231
- filesByIdMap[id] = fileName;
232
- }
233
- }
234
-
235
- let counter = 0;
236
-
237
- // Delete files using the store object filesById as reference map.
238
- for (const fileName of pageFilePaths) {
239
- const filePath = `${dirPath}/${fileName}`;
240
- const fileObject = fsx.readJSONSync(filePath, "utf-8") as APIPageObject;
241
-
242
- const { id } = fileObject;
243
-
244
- // If the filename is not present in the map, remove it!
245
- if (fileName !== filesByIdMap[id]) {
246
- fs.unlinkSync(filePath);
247
- counter++;
248
- }
249
- }
250
-
251
- console.log(`Sanitize apiCache dir for ${counter} files`);
252
- }
253
-
254
128
  /**
255
129
  * Measures the execution time of a series of sync or async functions.
256
130
  *
@@ -258,84 +132,55 @@ function sanitizeAPICacheDir() {
258
132
  * @param functions - Functions to be executed to measure their execution time.
259
133
  * @returns A promise that resolves with the total execution time in seconds.
260
134
  */
261
- async function measureExecutionTime(
262
- functions: Array<(...args: Array<unknown>) => unknown | Promise<unknown>>,
263
- ) {
264
- const start = process.hrtime();
265
-
266
- for (const func of functions) {
267
- await func();
268
- }
269
-
270
- const [seconds, miliseconds] = process.hrtime(start);
271
- const timeInSeconds = seconds + miliseconds / 1e9;
272
-
273
- return +timeInSeconds.toFixed(3);
274
- }
275
-
276
- /**
277
- * Pause the process until a key is pressed.
278
- *
279
- * @param title
280
- * @returns
281
- */
282
- function pause(title: string) {
283
- const isPauseEnabled = !!envs.GRIDDO_RENDER_BREAKPOINTS_FEATURE;
284
-
285
- if (!isPauseEnabled) {
286
- return;
287
- }
288
-
289
- return new Promise<void>((resolve) => {
290
- console.log("\n");
291
- boxLog(`⌛️ ${title}`, "", 1, 0);
292
- process.stdin.once("data", () => {
293
- resolve();
294
- });
295
- });
135
+ async function executeAndReturnExecutionTime<T>(func: () => T | Promise<T>) {
136
+ const start = process.hrtime.bigint();
137
+ await Promise.resolve(func());
138
+ const end = process.hrtime.bigint();
139
+ return Number((Number(end - start) / 1e9).toFixed(3));
296
140
  }
297
141
 
298
142
  /**
299
- * Executes a life cycle process, which involves executing an array of
300
- * functions, printing to the console, and handling errors with optional
301
- * retries.
143
+ * Executes a life cycle process, which involves executing a callback
144
+ * function, printing to the console, and handling errors with optional retries.
302
145
  *
303
146
  * @async
304
147
  * @param name - The name of the life cycle.
305
- * @param options - The arguments object.
306
- * @param options.steps - An array of functions to execute.
148
+ * @param action - A callback function to execute.
149
+ * @param options - Optional configuration for the life cycle process.
150
+ * @param options.maxRetrys - The maximum number of retries for the life cycle.
151
+ * @param options.enabled - Whether the life cycle process should be executed.
307
152
  * @returns - A promise that resolves when the life cycle process is completed.
308
153
  */
309
- async function doLifeCycle(name: LifeCyclesNames, steps: LifeCycleSteps) {
310
- const attemptsByLifeCycleName: Record<LifeCyclesNames, number> = {
311
- Init: envs.GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS,
312
- Archive: envs.GRIDDO_ARCHIVE_LIFECYCLE_MAX_ATTEMPTS,
313
- Data: envs.GRIDDO_DATA_LIFECYCLE_MAX_ATTEMPTS,
314
- Meta: envs.GRIDDO_META_LIFECYCLE_MAX_ATTEMPTS,
315
- Relocation: envs.GRIDDO_RELOCATION_LIFECYCLE_MAX_ATTEMPTS,
316
- Clean: envs.GRIDDO_CLEAN_LIFECYCLE_MAX_ATTEMPTS,
317
- Restore: envs.GRIDDO_RESTORE_LIFECYCLE_MAX_ATTEMPTS,
318
- Prepare: envs.GRIDDO_PREPARE_LIFECYCLE_MAX_ATTEMPTS,
319
- Close: envs.GRIDDO_CLOSE_LIFECYCLE_MAX_ATTEMPTS,
320
- SSG: envs.GRIDDO_SSG_LIFECYCLE_MAX_ATTEMPTS,
321
- HealthCheck: 1,
322
- __DEBUG__: 1,
323
- };
154
+ async function doLifeCycle(
155
+ name: string,
156
+ action: LifeCycleAction,
157
+ options?: { maxRetrys?: number; spinner?: boolean },
158
+ ) {
159
+ const { maxRetrys = 3 } = options || {};
324
160
 
325
161
  let trysCount = 0;
326
- const maxTrysAccepted = attemptsByLifeCycleName[name] || 1;
327
162
 
328
- while (trysCount < maxTrysAccepted) {
163
+ while (trysCount < maxRetrys) {
329
164
  try {
330
165
  infoLog(`start ${name} life-cycle`);
331
- const exeTime = await measureExecutionTime(steps);
166
+
167
+ // let counter = 0;
168
+ // process.stdout.write("\x1B[?25l");
169
+ // const spinnerChars = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
170
+ // const interval = setInterval(() => {
171
+ // const spinner = spinnerChars[counter % spinnerChars.length];
172
+ // process.stdout.write(`\r${spinner} ${name} life-cycle...`);
173
+ // counter++;
174
+ // }, 60);
175
+
176
+ const exeTime = await executeAndReturnExecutionTime(action);
177
+ // clearInterval(interval);
178
+ // process.stdout.write("\x1B[?25h");
332
179
  successLog(`${name} - ${exeTime}s\n`);
333
- // if no errors, print and go out
334
- await pause(`${name} LifeCycle`);
335
180
  break;
336
181
  } catch (error) {
337
- const errorString = bgRed(black(` Error in ${name} LifeCycle `));
338
- const attemptsString = kleur.yellow(`Attempt (${trysCount + 1})`);
182
+ const errorString = brush.red(` Error in ${name} LifeCycle `);
183
+ const attemptsString = brush.yellow(`Attempt (${trysCount + 1})`);
339
184
  console.log(`\n${errorString} ${attemptsString}\n`);
340
185
  console.log(error);
341
186
  console.log();
@@ -344,38 +189,23 @@ async function doLifeCycle(name: LifeCyclesNames, steps: LifeCycleSteps) {
344
189
  }
345
190
  }
346
191
 
347
- if (trysCount === maxTrysAccepted) {
348
- throwError(LifecycleExecutionError(maxTrysAccepted, name));
192
+ if (trysCount === maxRetrys) {
193
+ throwError(LifecycleExecutionError(maxRetrys, name));
349
194
  }
350
195
  }
351
196
 
352
- /**
353
- * Creates additional files after the render: sitemaps, robots and a report of
354
- * the finished render.
355
- *
356
- * @async
357
- * @param domain
358
- */
359
- async function createDomainMetadata(domain: string) {
360
- await generateBuildReport();
361
- await generateSitemaps();
362
- await RobotsService.writeFiles(domain);
363
- }
364
-
365
- function removeAllSiteDirsFromStore() {
366
- const { __cx } = config.paths();
197
+ async function removeAllSiteDirsFromStore() {
198
+ const { __cx } = await getRenderPathsFromDB();
367
199
  const storeDir = path.join(__cx, "store");
368
- const allDirs = fs.readdirSync(storeDir);
200
+ const allDirs = await fsp.readdir(storeDir);
369
201
 
370
202
  const allSiteDirs = allDirs.filter((dirname) => dirname !== "metadata");
371
203
 
372
- const allSiteDirsFullPath = allSiteDirs.map((name) =>
373
- path.join(storeDir, name),
374
- );
204
+ const allSiteDirsFullPath = allSiteDirs.map((name) => path.join(storeDir, name));
375
205
 
376
206
  // Remove all site directories except "metadata" directory.
377
207
  for (const site of allSiteDirsFullPath) {
378
- fs.rmSync(site, { recursive: true, force: true });
208
+ await fsp.rm(site, { recursive: true, force: true });
379
209
  }
380
210
  }
381
211
 
@@ -401,21 +231,21 @@ function getFormattedDateTime() {
401
231
  *
402
232
  * This information will **not** be sent to any API.
403
233
  */
404
- function saveBuildEndLogs() {
405
- const { __cx } = config.paths();
406
- const { sitesToPublish, createdPages, buildProcessData } = fsx.readJSONSync(
407
- path.join(__cx, "render-metadata.json"),
408
- );
234
+ async function saveBuildEndLogs(options: { basePath: string; domain: string }) {
235
+ const { basePath, domain } = options;
236
+
237
+ const db = await readDB();
238
+ const { sitesToPublish, createdPages, buildProcessData } = db.domains[domain].renderInfo || {};
409
239
 
410
240
  const dateString = getFormattedDateTime();
411
- const buildEndLogsDir = path.join(__cx, "debug", "build-end");
241
+ const buildEndLogsDir = path.join(basePath, "debug", "build-end");
412
242
 
413
- if (!fs.existsSync(buildEndLogsDir)) {
414
- fs.mkdirSync(buildEndLogsDir, { recursive: true });
243
+ if (!(await pathExists(buildEndLogsDir))) {
244
+ await fsp.mkdir(buildEndLogsDir, { recursive: true });
415
245
  }
416
246
 
417
247
  // Log
418
- fs.writeFileSync(
248
+ await fsp.writeFile(
419
249
  path.join(buildEndLogsDir, `${dateString}.json`),
420
250
  JSON.stringify(
421
251
  {
@@ -432,27 +262,36 @@ function saveBuildEndLogs() {
432
262
  );
433
263
 
434
264
  // Update index
435
- prependFileSync(
265
+ await prependFileSync(
436
266
  path.join(buildEndLogsDir, "index.html"),
437
267
  `<pre><a href="${dateString}.json">${dateString}</a></pre>\n`,
438
268
  );
439
269
  }
440
270
 
271
+ const lifeCycleNames: Record<LifeCyclesNames, LifeCyclesNames> = {
272
+ Sync: "Sync",
273
+ Clean: "Clean",
274
+ Close: "Close",
275
+ Data: "Data",
276
+ HealthCheck: "HealthCheck",
277
+ Init: "Init",
278
+ Meta: "Meta",
279
+ Prepare: "Prepare",
280
+ Relocation: "Relocation",
281
+ Restore: "Restore",
282
+ SSG: "SSG",
283
+ } as const;
284
+
441
285
  export {
442
- createDomainMetadata,
443
286
  delay,
444
287
  doLifeCycle,
445
- getConfig,
288
+ executeAndReturnExecutionTime,
446
289
  getSafeSiteId,
447
- instanceRootDir,
448
290
  isTruthy,
449
- measureExecutionTime,
291
+ lifeCycleNames,
450
292
  msToSec,
451
- pause,
452
293
  removeAllSiteDirsFromStore,
453
294
  removeProperties,
454
- sanitizeAPICacheDir,
455
295
  saveBuildEndLogs,
456
- walk,
457
296
  walkStore,
458
297
  };
@@ -1,17 +1,20 @@
1
1
  import type { Domains } from "../types/global";
2
2
 
3
+ import { DOMAINS } from "../constants/endpoints";
4
+ import { NoDomainsFoundError } from "../constants/errors";
5
+ import { get } from "./api";
6
+ import { throwError } from "./errors";
3
7
  import { verboseLog } from "./loggin";
4
- import { throwError } from "../errors";
5
- import { NoDomainsFoundError } from "../errors/errors-data";
6
- import { AuthService } from "../services/auth";
7
- import { getAllDomains } from "../services/domains";
8
8
 
9
9
  /**
10
10
  * Return an array of domains name (string) of the current instance.
11
11
  */
12
12
  async function getInstanceDomains() {
13
- await AuthService.login();
14
- const domains = await getAllDomains();
13
+ // await AuthService.login();
14
+ const domains = await get<Domains>({
15
+ endpoint: DOMAINS,
16
+ useApiCacheDir: false,
17
+ });
15
18
 
16
19
  if (!domains.length) {
17
20
  throwError(NoDomainsFoundError);
@@ -36,4 +39,4 @@ function getDomainSlugs(domains: Domains) {
36
39
  return [...new Set(filteredDomains)];
37
40
  }
38
41
 
39
- export { getInstanceDomains, getDomainSlugs };
42
+ export { getDomainSlugs, getInstanceDomains };
@@ -1,6 +1,6 @@
1
- import type { ErrorsType } from "./errors-data";
1
+ import type { ErrorsType } from "../constants/errors";
2
2
 
3
- import kleur from "kleur";
3
+ import { brush } from "./brush";
4
4
 
5
5
  export type ErrorData = {
6
6
  error: ErrorsType;
@@ -10,20 +10,21 @@ export type ErrorData = {
10
10
  };
11
11
 
12
12
  export class RenderError extends Error {
13
- constructor() {
14
- super();
13
+ constructor(originalError?: unknown) {
14
+ super(originalError instanceof Error ? originalError.message : String(originalError));
15
+
15
16
  this.name = "InternalCXError";
16
- this.stack = "";
17
+ this.stack = originalError instanceof Error ? originalError.stack : "";
17
18
  }
18
19
  }
19
20
 
20
21
  /**
21
22
  * Throws an error with the provided error message, expected value, and hint.
22
23
  */
23
- function throwError(options: ErrorData, stack?: unknown) {
24
+ function throwError(options: ErrorData, stack?: unknown): never {
24
25
  const { error, message, expected, hint } = options;
25
26
 
26
- const errorColor = kleur.red("[ " + error + " ]");
27
+ const errorColor = console.log(brush.red(`[ ${error} ]`));
27
28
  const extraText = [expected, hint].filter(Boolean).join("\n");
28
29
 
29
30
  console.log(`
@@ -31,10 +32,10 @@ ${errorColor}
31
32
  ${message}
32
33
  ${extraText}
33
34
 
34
- ${kleur.red("stack")}
35
+ ${brush.red("stack")}
35
36
  ${JSON.stringify(stack, null, 2)}`);
36
37
 
37
- throw new RenderError();
38
+ throw new RenderError(stack);
38
39
  }
39
40
 
40
41
  export { throwError };