@griddo/cx 11.7.6 → 11.7.7-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 (280) 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 +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 +51 -0
  23. package/build/adapters/gatsby/actions/sync.js.map +1 -0
  24. package/build/adapters/gatsby/index.js +79 -0
  25. package/build/adapters/gatsby/index.js.map +1 -0
  26. package/build/adapters/gatsby/shared/context.js +31 -0
  27. package/build/adapters/gatsby/shared/context.js.map +1 -0
  28. package/build/adapters/gatsby/shared/diff-assets.js +101 -0
  29. package/build/adapters/gatsby/shared/diff-assets.js.map +1 -0
  30. package/build/adapters/gatsby/shared/extract-assets.js +58 -0
  31. package/build/adapters/gatsby/shared/extract-assets.js.map +1 -0
  32. package/build/adapters/gatsby/shared/gatsby-build.js +49 -0
  33. package/build/adapters/gatsby/shared/gatsby-build.js.map +1 -0
  34. package/build/adapters/gatsby/shared/sync-render.js +210 -0
  35. package/build/adapters/gatsby/shared/sync-render.js.map +1 -0
  36. package/build/adapters/gatsby/shared/types.js +3 -0
  37. package/build/adapters/gatsby/shared/types.js.map +1 -0
  38. package/build/commands/end-render.js +88 -0
  39. package/build/commands/end-render.js.map +1 -0
  40. package/build/commands/prepare-assets-directory.js +32 -0
  41. package/build/commands/prepare-assets-directory.js.map +1 -0
  42. package/build/commands/prepare-domains-render.js +133 -0
  43. package/build/commands/prepare-domains-render.js.map +1 -0
  44. package/build/commands/reset-render.js +22 -0
  45. package/build/commands/reset-render.js.map +1 -0
  46. package/build/commands/start-render.js +45 -0
  47. package/build/commands/start-render.js.map +1 -0
  48. package/build/commands/upload-search-content.js +180 -0
  49. package/build/commands/upload-search-content.js.map +1 -0
  50. package/build/constants/endpoints.js +42 -0
  51. package/build/constants/endpoints.js.map +1 -0
  52. package/build/constants/envs.js +68 -0
  53. package/build/constants/envs.js.map +1 -0
  54. package/build/constants/errors.js +81 -0
  55. package/build/constants/errors.js.map +1 -0
  56. package/build/services/auth.js +40 -0
  57. package/build/services/auth.js.map +1 -0
  58. package/build/services/db-class.js +49 -0
  59. package/build/services/db-class.js.map +1 -0
  60. package/build/services/db.js +34 -0
  61. package/build/services/db.js.map +1 -0
  62. package/build/services/navigation.js +96 -0
  63. package/build/services/navigation.js.map +1 -0
  64. package/build/services/reference-fields.js +131 -0
  65. package/build/services/reference-fields.js.map +1 -0
  66. package/build/services/robots.js +60 -0
  67. package/build/services/robots.js.map +1 -0
  68. package/build/services/sites.js +109 -0
  69. package/build/services/sites.js.map +1 -0
  70. package/build/services/store.js +282 -0
  71. package/build/services/store.js.map +1 -0
  72. package/build/types/api.js +3 -0
  73. package/build/types/api.js.map +1 -0
  74. package/build/types/global.js +3 -0
  75. package/build/types/global.js.map +1 -0
  76. package/build/types/navigation.js +3 -0
  77. package/build/types/navigation.js.map +1 -0
  78. package/build/types/pages.js +3 -0
  79. package/build/types/pages.js.map +1 -0
  80. package/build/types/render.js +10 -0
  81. package/build/types/render.js.map +1 -0
  82. package/build/types/sites.js +3 -0
  83. package/build/types/sites.js.map +1 -0
  84. package/build/types/templates.js +3 -0
  85. package/build/types/templates.js.map +1 -0
  86. package/build/utils/api.js +161 -0
  87. package/build/utils/api.js.map +1 -0
  88. package/build/utils/artifacts.js +34 -0
  89. package/build/utils/artifacts.js.map +1 -0
  90. package/build/utils/brush.js +30 -0
  91. package/build/utils/brush.js.map +1 -0
  92. package/build/utils/cache.js +106 -0
  93. package/build/utils/cache.js.map +1 -0
  94. package/build/utils/check-health.js +68 -0
  95. package/build/utils/check-health.js.map +1 -0
  96. package/build/utils/core-utils.js +283 -0
  97. package/build/utils/core-utils.js.map +1 -0
  98. package/build/utils/domains.js +37 -0
  99. package/build/utils/domains.js.map +1 -0
  100. package/build/utils/errors.js +30 -0
  101. package/build/utils/errors.js.map +1 -0
  102. package/build/utils/folders.js +362 -0
  103. package/build/utils/folders.js.map +1 -0
  104. package/build/utils/images.js +45 -0
  105. package/build/utils/images.js.map +1 -0
  106. package/build/utils/instance.js +66 -0
  107. package/build/utils/instance.js.map +1 -0
  108. package/build/utils/loggin.js +108 -0
  109. package/build/utils/loggin.js.map +1 -0
  110. package/build/utils/pages.js +359 -0
  111. package/build/utils/pages.js.map +1 -0
  112. package/build/utils/render.js +144 -0
  113. package/build/utils/render.js.map +1 -0
  114. package/build/utils/sites.js +240 -0
  115. package/build/utils/sites.js.map +1 -0
  116. package/build/utils/store.js +193 -0
  117. package/build/utils/store.js.map +1 -0
  118. package/exporter/adapters/gatsby/actions/clean.ts +14 -0
  119. package/exporter/adapters/gatsby/actions/close.ts +17 -0
  120. package/exporter/adapters/gatsby/actions/data.ts +25 -0
  121. package/exporter/adapters/gatsby/actions/healthCheck.ts +10 -0
  122. package/exporter/adapters/gatsby/actions/init.ts +12 -0
  123. package/exporter/adapters/gatsby/actions/meta.ts +18 -0
  124. package/exporter/adapters/gatsby/actions/prepare.ts +9 -0
  125. package/exporter/adapters/gatsby/actions/relocation.ts +15 -0
  126. package/exporter/adapters/gatsby/actions/restore.ts +36 -0
  127. package/exporter/adapters/gatsby/actions/ssg.ts +12 -0
  128. package/exporter/adapters/gatsby/actions/sync.ts +71 -0
  129. package/exporter/adapters/gatsby/index.ts +67 -147
  130. package/exporter/adapters/gatsby/shared/context.ts +50 -0
  131. package/exporter/adapters/gatsby/shared/diff-assets.ts +113 -0
  132. package/exporter/adapters/gatsby/shared/extract-assets.ts +61 -0
  133. package/exporter/adapters/gatsby/shared/gatsby-build.ts +54 -0
  134. package/exporter/adapters/gatsby/shared/sync-render.ts +276 -0
  135. package/exporter/adapters/gatsby/shared/types.ts +35 -0
  136. package/exporter/build-esbuild.noop +42 -0
  137. package/exporter/build.sh +4 -38
  138. package/exporter/commands/end-render.ts +73 -69
  139. package/exporter/commands/prepare-assets-directory.ts +33 -0
  140. package/exporter/commands/prepare-domains-render.ts +144 -31
  141. package/exporter/commands/reset-render.ts +10 -1
  142. package/exporter/commands/start-render.ts +23 -44
  143. package/exporter/commands/upload-search-content.ts +194 -20
  144. package/exporter/constants/envs.ts +57 -55
  145. package/exporter/{errors/errors-data.ts → constants/errors.ts} +24 -14
  146. package/exporter/services/auth.ts +7 -6
  147. package/exporter/services/db-class.ts +54 -0
  148. package/exporter/services/db.ts +32 -0
  149. package/exporter/services/navigation.ts +4 -10
  150. package/exporter/services/reference-fields.ts +8 -23
  151. package/exporter/services/robots.ts +9 -16
  152. package/exporter/services/sites.ts +35 -24
  153. package/exporter/services/store.ts +120 -78
  154. package/exporter/types/api.ts +24 -27
  155. package/exporter/types/global.ts +7 -10
  156. package/exporter/types/navigation.ts +1 -1
  157. package/exporter/types/pages.ts +2 -3
  158. package/exporter/types/render.ts +59 -0
  159. package/exporter/types/sites.ts +1 -2
  160. package/exporter/utils/api.ts +46 -82
  161. package/exporter/utils/artifacts.ts +34 -0
  162. package/exporter/utils/brush.ts +34 -0
  163. package/exporter/utils/cache.ts +37 -62
  164. package/exporter/utils/check-health.ts +79 -0
  165. package/exporter/utils/core-utils.ts +129 -248
  166. package/exporter/utils/domains.ts +10 -7
  167. package/exporter/{errors/index.ts → utils/errors.ts} +10 -9
  168. package/exporter/utils/folders.ts +187 -95
  169. package/exporter/utils/images.ts +1 -6
  170. package/exporter/utils/instance.ts +9 -12
  171. package/exporter/utils/loggin.ts +32 -91
  172. package/exporter/utils/pages.ts +23 -88
  173. package/exporter/utils/render.ts +149 -48
  174. package/exporter/utils/sites.ts +81 -90
  175. package/exporter/utils/store.ts +87 -180
  176. package/gatsby-browser.tsx +41 -58
  177. package/gatsby-config.ts +6 -12
  178. package/gatsby-node.ts +15 -77
  179. package/gatsby-ssr.tsx +2 -1
  180. package/package.json +36 -78
  181. package/plugins/gatsby-plugin-svgr-loader/gatsby-node.js +55 -0
  182. package/plugins/gatsby-plugin-svgr-loader/package.json +8 -0
  183. package/{exporter/react → react}/Favicon/index.tsx +1 -7
  184. package/{exporter/react → react}/GriddoIntegrations/index.tsx +6 -14
  185. package/{exporter/react → react}/GriddoIntegrations/utils.ts +4 -9
  186. package/{build/react/index.d.ts → react/index.tsx} +1 -0
  187. package/src/components/Head.tsx +13 -46
  188. package/src/components/template.tsx +8 -30
  189. package/src/gatsby-node-utils.ts +91 -2
  190. package/src/html.tsx +2 -11
  191. package/src/types.ts +5 -5
  192. package/tsconfig.json +5 -3
  193. package/build/adapters/gatsby/index.d.ts +0 -4
  194. package/build/adapters/gatsby/utils.d.ts +0 -22
  195. package/build/artifacts/index.d.ts +0 -6
  196. package/build/commands/end-render.d.ts +0 -2
  197. package/build/commands/move-assets.d.ts +0 -1
  198. package/build/commands/prepare-domains-render.d.ts +0 -1
  199. package/build/commands/reset-render.d.ts +0 -2
  200. package/build/commands/start-render.d.ts +0 -2
  201. package/build/commands/upload-search-content.d.ts +0 -2
  202. package/build/constants/endpoints.d.ts +0 -19
  203. package/build/constants/envs.d.ts +0 -37
  204. package/build/constants/index.d.ts +0 -57
  205. package/build/end-render.js +0 -74
  206. package/build/end-render.js.map +0 -7
  207. package/build/errors/errors-data.d.ts +0 -22
  208. package/build/errors/index.d.ts +0 -15
  209. package/build/index.d.ts +0 -29
  210. package/build/index.js +0 -73
  211. package/build/index.js.map +0 -7
  212. package/build/prepare-domains-render.js +0 -73
  213. package/build/prepare-domains-render.js.map +0 -7
  214. package/build/react/Favicon/index.d.ts +0 -5
  215. package/build/react/Favicon/utils.d.ts +0 -9
  216. package/build/react/GriddoIntegrations/index.d.ts +0 -20
  217. package/build/react/GriddoIntegrations/utils.d.ts +0 -26
  218. package/build/react/index.js +0 -3
  219. package/build/registers/api.d.ts +0 -9
  220. package/build/registers/gatsby.d.ts +0 -9
  221. package/build/registers/index.d.ts +0 -3
  222. package/build/reset-render.js +0 -74
  223. package/build/reset-render.js.map +0 -7
  224. package/build/services/auth.d.ts +0 -10
  225. package/build/services/domains.d.ts +0 -6
  226. package/build/services/navigation.d.ts +0 -50
  227. package/build/services/reference-fields.d.ts +0 -20
  228. package/build/services/register.d.ts +0 -36
  229. package/build/services/robots.d.ts +0 -19
  230. package/build/services/settings.d.ts +0 -4
  231. package/build/services/sites.d.ts +0 -29
  232. package/build/services/store.d.ts +0 -6
  233. package/build/start-render.js +0 -100
  234. package/build/start-render.js.map +0 -7
  235. package/build/types/api.d.ts +0 -142
  236. package/build/types/global.d.ts +0 -84
  237. package/build/types/navigation.d.ts +0 -28
  238. package/build/types/pages.d.ts +0 -144
  239. package/build/types/sites.d.ts +0 -57
  240. package/build/types/templates.d.ts +0 -8
  241. package/build/upload-search-content.js +0 -74
  242. package/build/upload-search-content.js.map +0 -7
  243. package/build/utils/alerts.d.ts +0 -3
  244. package/build/utils/api.d.ts +0 -23
  245. package/build/utils/cache.d.ts +0 -35
  246. package/build/utils/core-utils.d.ts +0 -107
  247. package/build/utils/create-build-data.d.ts +0 -8
  248. package/build/utils/domains.d.ts +0 -13
  249. package/build/utils/folders.d.ts +0 -53
  250. package/build/utils/health-checks.d.ts +0 -7
  251. package/build/utils/images.d.ts +0 -16
  252. package/build/utils/instance.d.ts +0 -21
  253. package/build/utils/loggin.d.ts +0 -51
  254. package/build/utils/pages.d.ts +0 -34
  255. package/build/utils/render.d.ts +0 -13
  256. package/build/utils/searches.d.ts +0 -15
  257. package/build/utils/sites.d.ts +0 -31
  258. package/build/utils/store.d.ts +0 -81
  259. package/cx.config.d.ts +0 -5
  260. package/cx.config.js +0 -36
  261. package/exporter/adapters/gatsby/utils.ts +0 -161
  262. package/exporter/artifacts/README.md +0 -34
  263. package/exporter/artifacts/index.ts +0 -33
  264. package/exporter/commands/move-assets.ts +0 -11
  265. package/exporter/constants/index.ts +0 -129
  266. package/exporter/index.ts +0 -82
  267. package/exporter/react/index.tsx +0 -11
  268. package/exporter/registers/api.ts +0 -14
  269. package/exporter/registers/gatsby.ts +0 -14
  270. package/exporter/registers/index.ts +0 -4
  271. package/exporter/services/domains.ts +0 -16
  272. package/exporter/services/register.ts +0 -113
  273. package/exporter/services/settings.ts +0 -17
  274. package/exporter/utils/alerts.ts +0 -29
  275. package/exporter/utils/create-build-data.ts +0 -17
  276. package/exporter/utils/health-checks.ts +0 -64
  277. package/exporter/utils/searches.ts +0 -156
  278. package/src/README.md +0 -7
  279. package/start-render.js +0 -7
  280. /package/{exporter/react → react}/Favicon/utils.ts +0 -0
@@ -0,0 +1,71 @@
1
+ import type { RenderContext } from "../shared/context";
2
+ import type { SSG } from "../shared/types";
3
+
4
+ import fsp from "node:fs/promises";
5
+ import path from "node:path";
6
+
7
+ import { RENDER_MODE } from "../../../types/render";
8
+ import {
9
+ deleteDisposableSiteDirs,
10
+ deleteEmptyDirectories,
11
+ moveDirs,
12
+ removeVirtualPages,
13
+ } from "../../../utils/folders";
14
+ import { extractAssetsFromDist } from "../shared/extract-assets";
15
+ import { SyncRender } from "../shared/sync-render";
16
+
17
+ export async function syncAction(context: RenderContext<SSG>) {
18
+ const {
19
+ renderMode,
20
+ cxArtifacts,
21
+ domain,
22
+ pagesToCreate,
23
+ pagesToDelete,
24
+ paths: { __root, __ssg, __cache, __exports },
25
+ ssg: { ssgArtifacts, assetPrefix },
26
+ } = context;
27
+
28
+ const needsAssetPrefix = !!assetPrefix && assetPrefix !== "";
29
+ const distDir = path.join(__root, "dist");
30
+ const distRestoredDir = path.join(__root, "dist-restored");
31
+ const storeDir = path.join(__root, "store");
32
+
33
+ // ESTO YA NO TIENE SENTIDO??? EL STORE???
34
+ await removeVirtualPages(storeDir);
35
+ // caching...
36
+ await moveDirs(__root, __cache, cxArtifacts.cacheables);
37
+ await moveDirs(__ssg, __cache, ssgArtifacts.cacheables);
38
+
39
+ if (renderMode === RENDER_MODE.FROM_SCRATCH) {
40
+ await deleteDisposableSiteDirs(distDir);
41
+ }
42
+
43
+ if (renderMode === RENDER_MODE.INCREMENTAL) {
44
+ await deleteDisposableSiteDirs(distRestoredDir);
45
+
46
+ const syncRender = new SyncRender({
47
+ src: distDir,
48
+ dst: distRestoredDir,
49
+ pagesToCreate,
50
+ pagesToDelete,
51
+ artifactsToCopyToExports: ["build-report.json"],
52
+ });
53
+
54
+ await syncRender.execute();
55
+
56
+ // elimina el dist recién creado, ya no hace falta porque lo hemos utilizado en syncRender.execute()
57
+ await fsp.rm(distDir, { force: true, recursive: true });
58
+ // renombra el nuevo dist-temporal que es el que aglutina todos los cambios -> dist
59
+ await fsp.rename(distRestoredDir, distDir);
60
+ await deleteEmptyDirectories(distDir);
61
+ }
62
+
63
+ if (needsAssetPrefix) {
64
+ await extractAssetsFromDist(domain);
65
+ }
66
+
67
+ // archiving...
68
+ await moveDirs(__root, __exports, cxArtifacts.archivables, {
69
+ withBackup: true,
70
+ });
71
+ }
@@ -1,162 +1,82 @@
1
- import type {
2
- Artifacts,
3
- LifeCyclesNames,
4
- LifeCycleSteps,
5
- } from "../../types/global";
1
+ import type { SSG } from "./shared/types";
6
2
 
7
3
  import path from "node:path";
8
4
 
9
- import {
10
- createDistFromGatsbyPublic,
11
- getGatsbyAssetPrefixWithDomain,
12
- runGatsbyBuildCommand,
13
- } from "./utils";
14
- import getCxArtifacts from "../../artifacts";
15
- import { AuthService } from "../../services/auth";
16
- import {
17
- createDomainMetadata,
18
- doLifeCycle,
19
- getConfig,
20
- saveBuildEndLogs,
21
- } from "../../utils/core-utils";
22
- import { createDomainRenderData } from "../../utils/create-build-data";
23
- import {
24
- copyArtifacts,
25
- createArtifacts,
26
- deleteDisposableSiteDirs,
27
- moveArtifacts,
28
- removeArtifacts,
29
- removeVirtualPagesFromStore,
30
- renameArtifact,
31
- } from "../../utils/folders";
5
+ import { GRIDDO_ASSET_PREFIX } from "../../constants/envs";
6
+ import { getCxArtifacts } from "../../utils/artifacts";
7
+ import { doLifeCycle } from "../../utils/core-utils";
8
+ import { pathExists } from "../../utils/folders";
32
9
  import { infoLog } from "../../utils/loggin";
33
- import {
34
- createSentinelRenderFile,
35
- deleteSentinelRenderFile,
36
- isValidRender,
37
- } from "../../utils/render";
10
+ import { getRenderMetadataFromDB, getRenderMode, getRenderPathsFromDB } from "../../utils/render";
11
+ import { cleanAction } from "./actions/clean";
12
+ import { closeAction } from "./actions/close";
13
+ import { dataAction } from "./actions/data";
14
+ import { healthCheckAction } from "./actions/healthCheck";
15
+ import { initAction } from "./actions/init";
16
+ import { metaAction } from "./actions/meta";
17
+ import { prepareAction } from "./actions/prepare";
18
+ import { relocationAction } from "./actions/relocation";
19
+ import { restoreAction } from "./actions/restore";
20
+ import { ssgAction } from "./actions/ssg";
21
+ import { syncAction } from "./actions/sync";
22
+ import { RenderContext } from "./shared/context";
38
23
 
39
24
  /**
40
- * Render every instance domain with the Gatsby adapter.
25
+ * Executes a complete Gatsby render process for a given domain.
26
+ *
27
+ * The render process consists of multiple lifecycle steps that handle cleaning,
28
+ * preparing, building and syncing the site. It can run in two modes:
29
+ * - FROM_SCRATCH: Performs a complete rebuild of the site
30
+ * - INCREMENTAL: Only rebuilds changed pages while preserving the rest
41
31
  */
42
- export async function renderDomainsWithGatsbyAdapter(domain: string) {
43
- await AuthService.login();
44
- const config = getConfig();
45
-
46
- infoLog(`Initializing render for the domain ${domain}\n`);
47
-
48
- const {
49
- //
50
- __ssg,
51
- __exports,
52
- __cache,
53
- __cx,
54
- __components,
55
- } = config.paths(domain);
56
-
57
- // Griddo artifacts
58
- const cxArtifacts = getCxArtifacts(domain);
59
-
60
- // Gatsby artifacts
61
- const gatsbyArtifacts: Pick<Artifacts, "disposables" | "cacheables"> = {
32
+ export async function gatsbyRenderDomain(domain: string) {
33
+ const { renderMode, reason } = await getRenderMode(domain);
34
+
35
+ const renderMetadata = await getRenderMetadataFromDB();
36
+ const paths = await getRenderPathsFromDB({ domain });
37
+ const cxArtifacts = await getCxArtifacts(domain);
38
+ const domainNeedsAssetPrefix = GRIDDO_ASSET_PREFIX && domain.startsWith(renderMetadata.proDomain);
39
+ const assetPrefix = domainNeedsAssetPrefix ? `${GRIDDO_ASSET_PREFIX}/${domain}` : "";
40
+ const ssgArtifacts = {
62
41
  disposables: [
63
- path.join(__ssg, "public"),
64
- path.join(__ssg, "static"),
65
- path.join(__ssg, ".cache"),
42
+ path.join(paths.__ssg, "public"),
43
+ path.join(paths.__ssg, "static"),
44
+ path.join(paths.__ssg, ".cache"),
45
+ path.join(paths.__ssg, "domains.json"), // Este disposable es de griddo pero está en la ruta __ssg
46
+ path.join(paths.__ssg, "domains.txt"), // Este disposable es de griddo pero está en la ruta __ssg
66
47
  ],
67
48
  cacheables: [".cache"],
68
49
  };
69
50
 
70
- // These variables are involved in the build for create the dist directory
71
- // from public and pass to the gatsby-build command via spawnSync.
72
- const assetPrefix = getGatsbyAssetPrefixWithDomain(domain);
73
- const needsAssetPrefix = !!assetPrefix && assetPrefix !== "";
74
-
75
- // Compose artifacts
76
- const disposableArtifacts = [
77
- ...cxArtifacts.disposables,
78
- ...gatsbyArtifacts.disposables,
79
- ];
80
-
81
- // LifeCycles
82
-
83
- const allLifeCycles: Array<{
84
- name: LifeCyclesNames;
85
- steps: LifeCycleSteps;
86
- }> = [
87
- {
88
- name: "Init",
89
- steps: [() => createSentinelRenderFile()],
90
- },
91
-
92
- {
93
- name: "Clean",
94
- steps: [() => removeArtifacts(disposableArtifacts)],
95
- },
96
-
97
- {
98
- name: "Prepare",
99
- steps: [() => createArtifacts(cxArtifacts.initials)],
100
- },
51
+ const sentinelFile = await pathExists(path.join(paths.__ssg, ".render-sentinel"));
52
+ const currentRenderMode = sentinelFile ? "FROM_SCRATCH" : renderMode;
53
+ const currentReason = sentinelFile ? "previous render error" : reason;
101
54
 
102
- {
103
- name: "Restore",
104
- steps: [
105
- () => copyArtifacts(__components, __ssg, ["static"]),
106
- () => copyArtifacts(__exports, __cx, cxArtifacts.archivables),
107
- () =>
108
- renameArtifact(path.join(__cx, "dist"), path.join(__ssg, "public")),
109
- () => moveArtifacts(__cache, __cx, cxArtifacts.cacheables),
110
- () => moveArtifacts(__cache, __ssg, gatsbyArtifacts.cacheables),
111
- ],
112
- },
113
-
114
- {
115
- name: "Data",
116
- steps: [() => createDomainRenderData(domain)],
117
- },
118
-
119
- {
120
- name: "SSG",
121
- steps: [() => runGatsbyBuildCommand(assetPrefix)],
122
- },
123
-
124
- {
125
- name: "Relocation",
126
- steps: [() => createDistFromGatsbyPublic(domain, needsAssetPrefix)],
127
- },
128
-
129
- {
130
- name: "Meta",
131
- steps: [() => createDomainMetadata(domain), () => saveBuildEndLogs()],
132
- },
133
-
134
- {
135
- name: "Archive",
136
- steps: [
137
- () => removeVirtualPagesFromStore(),
138
- () => deleteDisposableSiteDirs(),
139
- () =>
140
- moveArtifacts(__cx, __exports, cxArtifacts.archivables, {
141
- withBackup: true,
142
- }),
143
- () => moveArtifacts(__cx, __cache, cxArtifacts.cacheables),
144
- () => moveArtifacts(__ssg, __cache, gatsbyArtifacts.cacheables),
145
- ],
146
- },
147
-
148
- {
149
- name: "Close",
150
- steps: [() => removeArtifacts(disposableArtifacts)],
151
- },
152
-
153
- {
154
- name: "HealthCheck",
155
- steps: [() => isValidRender(), () => deleteSentinelRenderFile()],
156
- },
157
- ];
158
-
159
- for (const { name, steps } of allLifeCycles) {
160
- await doLifeCycle(name, steps);
55
+ if (renderMode === "IDLE" && currentRenderMode === "IDLE") {
56
+ return;
161
57
  }
58
+
59
+ const context = new RenderContext<SSG>({
60
+ domain,
61
+ renderMode: currentRenderMode,
62
+ paths,
63
+ renderMetadata,
64
+ cxArtifacts,
65
+ ssg: { assetPrefix, ssgArtifacts },
66
+ });
67
+
68
+ const renderReason = currentReason ? ` <${currentReason}>` : "";
69
+ infoLog(`Init render (${currentRenderMode})${renderReason} for domain ${domain}\n`);
70
+
71
+ await doLifeCycle("Init", async () => initAction(context));
72
+ await doLifeCycle("Clean", async () => cleanAction(context));
73
+ await doLifeCycle("Prepare", async () => prepareAction(context));
74
+ await doLifeCycle("Restore", async () => restoreAction(context));
75
+ await doLifeCycle("Data", async () => dataAction(context));
76
+ await doLifeCycle("SSG", async () => ssgAction(context));
77
+ await doLifeCycle("Relocation", async () => relocationAction(context));
78
+ await doLifeCycle("Meta", async () => metaAction(context));
79
+ await doLifeCycle("Sync", async () => syncAction(context));
80
+ await doLifeCycle("Close", async () => closeAction(context));
81
+ await doLifeCycle("HealthCheck", async () => healthCheckAction(context));
162
82
  }
@@ -0,0 +1,50 @@
1
+ import type { Artifacts, PlaceholderPath } from "../../../types/global";
2
+ import type { RenderMode } from "../../../types/render";
3
+
4
+ interface RenderContextProps<T> {
5
+ domain: string;
6
+ renderMode: RenderMode;
7
+ paths: Record<string, string>;
8
+ ssg: T;
9
+ renderMetadata: {
10
+ proDomain: string;
11
+ griddoVersion: string;
12
+ buildReportFileName: string;
13
+ };
14
+ cxArtifacts: Artifacts;
15
+ }
16
+
17
+ /**
18
+ * Encapsulates the state, configuration, and artifacts for a single SSG
19
+ * render process.
20
+ *
21
+ * This class is instantiated at the beginning of a render and passed through
22
+ * each lifecycle step, providing a centralized and consistent context for all
23
+ * operations. It is agnostic to the specific SSG being used.
24
+ */
25
+ export class RenderContext<T = unknown> {
26
+ readonly domain: string;
27
+ readonly renderMode: RenderMode;
28
+ readonly paths: Record<PlaceholderPath, string>;
29
+ pagesToCreate: number[] = [];
30
+ pagesToDelete: number[] = [];
31
+ ssg: T;
32
+ renderMetadata: { proDomain: string; griddoVersion: string; buildReportFileName: string };
33
+ cxArtifacts: Artifacts;
34
+
35
+ constructor({
36
+ domain,
37
+ renderMode,
38
+ ssg,
39
+ paths,
40
+ renderMetadata,
41
+ cxArtifacts,
42
+ }: RenderContextProps<T>) {
43
+ this.domain = domain;
44
+ this.renderMode = renderMode;
45
+ this.paths = paths;
46
+ this.renderMetadata = renderMetadata;
47
+ this.cxArtifacts = cxArtifacts;
48
+ this.ssg = ssg;
49
+ }
50
+ }
@@ -0,0 +1,113 @@
1
+ import { createHash } from "node:crypto";
2
+ import fsp from "node:fs/promises";
3
+ import { join } from "node:path";
4
+
5
+ // Lista de patrones de los ficheros de assets más importantes.
6
+ // Si estos no cambian, el webpackCompilationHash no debería cambiar.
7
+ const _KEY_ASSET_PATTERNS = [/^app-.*\.js$/, /^framework-.*\.js$/, /^webpack-runtime-.*\.js$/];
8
+
9
+ const KEY_ASSET_PATTERNS = [/\.js$/];
10
+
11
+ /**
12
+ * Calcula los hashes SHA256 del contenido de los assets clave en un directorio.
13
+ * @param dir La ruta al directorio (ej. './public')
14
+ * @returns Un Map donde la clave es el nombre del fichero y el valor es su hash.
15
+ */
16
+ async function getAssetHashes(dir: string): Promise<Map<string, string>> {
17
+ const assetHashes = new Map<string, string>();
18
+
19
+ try {
20
+ const allFiles = await fsp.readdir(dir);
21
+ const keyAssetFiles = allFiles.filter((fileName) =>
22
+ KEY_ASSET_PATTERNS.some((pattern) => pattern.test(fileName)),
23
+ );
24
+
25
+ for (const fileName of keyAssetFiles) {
26
+ const filePath = join(dir, fileName);
27
+ const fileContent = await fsp.readFile(filePath);
28
+ const hash = createHash("sha256").update(fileContent).digest("hex");
29
+ assetHashes.set(fileName, hash);
30
+ }
31
+ } catch (error) {
32
+ console.error(`Error procesando el directorio ${dir}:`, error);
33
+ // Si un directorio no existe (ej. el de producción la primera vez),
34
+ // lo tratamos como si no tuviera assets.
35
+ }
36
+
37
+ return assetHashes;
38
+ }
39
+
40
+ /**
41
+ * Compara los assets clave entre dos directorios de build de Gatsby.
42
+ * @param dir1 Ruta al primer directorio (ej. el de producción)
43
+ * @param dir2 Ruta al segundo directorio (ej. el del nuevo build)
44
+ * @returns `true` si hay diferencias, `false` si son idénticos.
45
+ */
46
+ export async function getAssetsDiffBetweenRenders(dir1: string, dir2: string): Promise<boolean> {
47
+ const [hashes1, hashes2] = await Promise.all([getAssetHashes(dir1), getAssetHashes(dir2)]);
48
+
49
+ // Si el número de assets clave es diferente, hay cambios.
50
+ if (hashes1.size !== hashes2.size) {
51
+ return true;
52
+ }
53
+
54
+ // Comparamos los hashes de cada fichero.
55
+ for (const [fileName, hash1] of hashes1.entries()) {
56
+ const hash2 = hashes2.get(fileName);
57
+
58
+ // Si un fichero existe en el primero pero no en el segundo, hay cambios.
59
+ if (!hash2) {
60
+ return true;
61
+ }
62
+
63
+ // Si el contenido (hash) de un fichero ha cambiado.
64
+ if (hash1 !== hash2) {
65
+ return true;
66
+ }
67
+ }
68
+
69
+ return false;
70
+ }
71
+
72
+ /**
73
+ * Actualiza el webpackCompilationHash en un único fichero HTML.
74
+ * @param filePath Ruta al fichero HTML.
75
+ * @param newHash El nuevo hash a insertar.
76
+ */
77
+ export async function patchHtmlFile(filePath: string, newHash: string): Promise<void> {
78
+ try {
79
+ const content = await fsp.readFile(filePath, "utf-8");
80
+
81
+ // Esta expresión regular captura tres grupos:
82
+ // 1. La parte ANTES del hash (window.___webpackCompilationHash=")
83
+ // 2. El hash ANTIGUO ([^"]*)
84
+ // 3. La parte DESPUÉS del hash (";</script>)
85
+ const regex = /(window\.___webpackCompilationHash=")([^"]*)(";<\/script>)/;
86
+
87
+ // Solo reemplazamos el segundo grupo (el hash)
88
+ const newContent = content.replace(regex, `$1${newHash}$3`);
89
+
90
+ if (content === newContent) {
91
+ console.warn(`WARN: No se encontró el hash en ${filePath}. El fichero no fue modificado.`);
92
+ return;
93
+ }
94
+
95
+ await fsp.writeFile(filePath, newContent, "utf-8");
96
+ } catch (error) {
97
+ console.error(`ERROR: Fallo al procesar el fichero ${filePath}:`, error);
98
+ }
99
+ }
100
+
101
+ // --- Ejemplo de uso ---
102
+ // async function miScriptDeDeploy() {
103
+ // const dirProduccion = '/ruta/a/produccion/public';
104
+ // const dirNuevoBuild = './public';
105
+
106
+ // const hayDiferencias = await getAssetsDiffBetweenRenders(dirProduccion, dirNuevoBuild);
107
+
108
+ // if (hayDiferencias) {
109
+ // // Lógica para el CAMINO A (cambio de código)
110
+ // } else {
111
+ // // Lógica para el CAMINO B (solo contenido)
112
+ // }
113
+ // }
@@ -0,0 +1,61 @@
1
+ import fsp from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import { pathExists } from "../../../utils/folders";
5
+ import { getRenderPathsFromDB } from "../../../utils/render";
6
+
7
+ /**
8
+ * Update the Griddo's `/dist` dir with the contents from `public` dir only
9
+ * with files of type: js, json and css.
10
+ * TODO: Explicar que el static se copia a assets porque el js va en el subdominio de assets.
11
+ */
12
+ async function extractAssetsFromDist(domain: string) {
13
+ const { __root } = await getRenderPathsFromDB({ domain });
14
+
15
+ // Archivos (no carpetas) válidos de public
16
+ const filesFromDist = (await fsp.readdir(path.join(__root, "dist"))).filter(
17
+ (file) =>
18
+ path.extname(file) === ".js" ||
19
+ path.extname(file) === ".json" ||
20
+ path.extname(file) === ".css",
21
+ );
22
+
23
+ // Creamos assets si es necesario (needsAssetPrefix)
24
+ await fsp.mkdir(path.join(__root, "assets"), { recursive: true });
25
+ // page-data folder
26
+ await fsp.cp(path.join(__root, "dist", "page-data"), path.join(__root, "assets", "page-data"), {
27
+ preserveTimestamps: true,
28
+ recursive: true,
29
+ });
30
+ // static folder si existe
31
+ if (await pathExists(path.join(__root, "static"))) {
32
+ await fsp.cp(path.join(__root, "static"), path.join(__root, "assets"), {
33
+ force: false,
34
+ preserveTimestamps: true,
35
+ recursive: true,
36
+ });
37
+ }
38
+ // dist/static -> assets/static
39
+ await fsp.cp(path.join(__root, "dist", "static"), path.join(__root, "assets", "static"), {
40
+ force: false,
41
+ preserveTimestamps: true,
42
+ recursive: true,
43
+ });
44
+ // otro static...
45
+ if (await pathExists(path.join(__root, "static"))) {
46
+ await fsp.cp(path.join(__root, "static"), path.join(__root, "dist", domain), {
47
+ preserveTimestamps: true,
48
+ recursive: true,
49
+ force: false,
50
+ });
51
+ }
52
+
53
+ // Copia el resto de archivos...
54
+ for (const file of filesFromDist) {
55
+ const fileSrc = path.join(__root, "dist", file);
56
+ const fileDest = path.join(__root, "assets", file);
57
+ await fsp.cp(fileSrc, fileDest, { preserveTimestamps: true, recursive: true });
58
+ }
59
+ }
60
+
61
+ export { extractAssetsFromDist };
@@ -0,0 +1,54 @@
1
+ import { spawn } from "node:child_process";
2
+
3
+ import { GRIDDO_SSG_VERBOSE_LOGS } from "../../../constants/envs";
4
+ import { verboseLog } from "../../../utils/loggin";
5
+ import { getRenderPathsFromDB } from "../../../utils/render";
6
+
7
+ /**
8
+ * Spawn a new node process `yarn gatsby-build`
9
+ * @note This proccess (`yarn gatsby-build`) can not access to the custom Griddo
10
+ * `process.env` so it needs variables passed to it via the `env` prop.
11
+ */
12
+ async function gatsbyBuild(assetPrefixWithDomain: string): Promise<void> {
13
+ verboseLog(`read assetPrefixWithDomain, ${assetPrefixWithDomain}`);
14
+ verboseLog(`using this NODE_OPTIONS in gatsby command: ${process.env.NODE_OPTIONS}`);
15
+
16
+ const { __ssg } = await getRenderPathsFromDB();
17
+
18
+ // Remove `--openssl-legacy-provider` from NODE_OPTIONS because this value
19
+ // break Gatsby render.
20
+ const nodeOptionsWithoutLegacyOpenSSL = process.env.NODE_OPTIONS
21
+ ? process.env.NODE_OPTIONS.replace(/--openssl-legacy-provider\s*/g, "").trim()
22
+ : "";
23
+
24
+ const args = ["gatsby-build"];
25
+ if (GRIDDO_SSG_VERBOSE_LOGS) {
26
+ args.push("--verbose");
27
+ }
28
+
29
+ return new Promise((resolve, reject) => {
30
+ const child = spawn("yarn", args, {
31
+ cwd: __ssg,
32
+ stdio: ["ignore", "inherit", "inherit"], // stdout y stderr van a consola directamente
33
+ env: Object.assign(process.env, {
34
+ GRIDDO_EXPORTER: "true",
35
+ SPAWN_ASSET_PREFIX_WITH_DOMAIN: assetPrefixWithDomain,
36
+ NODE_OPTIONS: nodeOptionsWithoutLegacyOpenSSL,
37
+ }),
38
+ });
39
+
40
+ child.on("close", (code) => {
41
+ if (code === 0) {
42
+ resolve();
43
+ } else {
44
+ reject(new Error(`Gatsby build failed with exit code ${code}`));
45
+ }
46
+ });
47
+
48
+ child.on("error", (error) => {
49
+ reject(new Error(`Failed to start Gatsby build process: ${error.message}`));
50
+ });
51
+ });
52
+ }
53
+
54
+ export { gatsbyBuild };