@griddo/cx 10.4.19 → 10.4.21

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 (73) hide show
  1. package/README.md +11 -76
  2. package/build/adapters/gatsby/index.d.ts +2 -0
  3. package/build/adapters/gatsby/utils.d.ts +27 -0
  4. package/build/adapters/index.d.ts +3 -0
  5. package/build/browser/index.d.ts +5 -0
  6. package/build/build-complete.d.ts +18 -0
  7. package/build/build-complete.js +22 -22
  8. package/build/errors/index.d.ts +1 -0
  9. package/build/index.d.ts +29 -0
  10. package/build/index.js +37 -35
  11. package/build/move-assets.d.ts +1 -0
  12. package/build/react/index.d.ts +2 -0
  13. package/build/reset-render.d.ts +1 -0
  14. package/build/reset-render.js +22 -22
  15. package/build/run-start-render.d.ts +1 -0
  16. package/build/run-start-render.js +37 -35
  17. package/build/services/auth.d.ts +21 -0
  18. package/build/services/distributors.d.ts +45 -0
  19. package/build/services/domains.d.ts +8 -0
  20. package/build/services/navigation.d.ts +77 -0
  21. package/build/services/robots.d.ts +21 -0
  22. package/build/services/settings.d.ts +23 -0
  23. package/build/services/sites.d.ts +42 -0
  24. package/build/services/store.d.ts +6 -0
  25. package/build/start-render.d.ts +3 -0
  26. package/build/start-render.js +37 -35
  27. package/build/types/api.d.ts +130 -0
  28. package/build/types/global.d.ts +78 -0
  29. package/build/types/navigation.d.ts +28 -0
  30. package/build/types/pages.d.ts +128 -0
  31. package/build/types/sites.d.ts +43 -0
  32. package/build/types/templates.d.ts +8 -0
  33. package/build/upload-search-content.d.ts +1 -0
  34. package/build/upload-search-content.js +21 -21
  35. package/build/utils/api.d.ts +23 -0
  36. package/build/utils/cache.d.ts +35 -0
  37. package/build/utils/create-build-data.d.ts +8 -0
  38. package/build/utils/domains.d.ts +5 -0
  39. package/build/utils/folders.d.ts +48 -0
  40. package/build/utils/health-checks.d.ts +7 -0
  41. package/build/utils/instance.d.ts +21 -0
  42. package/build/utils/messages.d.ts +2 -0
  43. package/build/utils/pages.d.ts +34 -0
  44. package/build/utils/searches.d.ts +14 -0
  45. package/build/utils/shared.d.ts +110 -0
  46. package/build/utils/sites.d.ts +36 -0
  47. package/build/utils/store.d.ts +79 -0
  48. package/build/utils/temp-utils.d.ts +10 -0
  49. package/cx.config.js +1 -1
  50. package/exporter/adapters/gatsby/index.ts +6 -20
  51. package/exporter/adapters/gatsby/utils.ts +25 -1
  52. package/exporter/errors/index.ts +2 -0
  53. package/exporter/index.ts +6 -4
  54. package/exporter/services/robots.ts +4 -3
  55. package/exporter/services/store.ts +13 -4
  56. package/exporter/start-render.ts +1 -2
  57. package/exporter/types/global.ts +1 -1
  58. package/exporter/utils/cache.ts +25 -11
  59. package/exporter/utils/create-build-data.ts +3 -10
  60. package/exporter/utils/folders.ts +14 -15
  61. package/exporter/utils/searches.ts +4 -4
  62. package/exporter/utils/shared.ts +18 -8
  63. package/exporter/utils/sites.ts +10 -10
  64. package/exporter/utils/store.ts +77 -27
  65. package/exporter/utils/temp-utils.ts +12 -11
  66. package/gatsby-config.ts +2 -2
  67. package/gatsby-node.ts +9 -10
  68. package/package.json +12 -6
  69. package/src/components/Head.tsx +1 -1
  70. package/src/components/template.tsx +1 -1
  71. package/src/gatsby-node-utils.ts +13 -2
  72. package/src/types.ts +1 -1
  73. package/tsconfig.json +13 -3
@@ -28,11 +28,15 @@ import {
28
28
  import { logBox, logInfo, siteList } from "../utils/shared";
29
29
  import { checkSites, getSiteData, unpublishSites } from "../utils/sites";
30
30
  import {
31
+ createStoreDir,
31
32
  getPagesToCreateOrDelete,
32
33
  removePagesFromStore,
33
34
  savePagesInStore,
34
35
  saveRenderInfoInStore,
35
36
  } from "../utils/store";
37
+ import { getConfig } from "../utils/temp-utils";
38
+
39
+ const config = getConfig();
36
40
 
37
41
  dotenv.config();
38
42
 
@@ -47,10 +51,15 @@ dotenv.config();
47
51
  * Fetch, process and save object pages and sites data into the file system to
48
52
  * be consumed by other services (Griddo itself, Adapters, etc.)
49
53
  */
50
- async function createStore(storeDir: string, domain: string) {
54
+ async function createStore(domain: string) {
55
+ createStoreDir();
56
+
51
57
  console.info(`API calls with ${API_CONCURRENCY_COUNT} threads`);
58
+
59
+ const { __cx } = config.paths();
60
+ const storeDir = path.join(__cx, "store");
52
61
  const { version: griddoVersion } = JSON.parse(
53
- fs.readFileSync(path.resolve(__dirname, "../package.json"), "utf-8")
62
+ fs.readFileSync(path.join(__cx, "package.json"), "utf-8")
54
63
  );
55
64
 
56
65
  try {
@@ -264,7 +273,7 @@ async function createStore(storeDir: string, domain: string) {
264
273
  }
265
274
 
266
275
  // Save build data to store
267
- savePagesInStore(storeDir, griddoPageObjects);
276
+ savePagesInStore(griddoPageObjects);
268
277
  };
269
278
 
270
279
  // Pages to be created or deleted in the store
@@ -281,7 +290,7 @@ async function createStore(storeDir: string, domain: string) {
281
290
  });
282
291
 
283
292
  // Delete pages from published, unpublished and deleted sites.
284
- removePagesFromStore(storeDir, pagesToDeleteFromStore);
293
+ removePagesFromStore(pagesToDeleteFromStore);
285
294
 
286
295
  // Debug time
287
296
  const changedPageLogs = `Store: ${pagesInStore.length} -> ${pagesInStore}
@@ -5,8 +5,7 @@ import { runGatsbyAdapter } from "./adapters";
5
5
  import { logBox, measureExecutionTime } from "./utils/shared";
6
6
 
7
7
  async function startRender() {
8
- const exeTime = await measureExecutionTime(runGatsbyAdapter).catch((e) => {
9
- console.log(e);
8
+ const exeTime = await measureExecutionTime(runGatsbyAdapter).catch(() => {
10
9
  console.log("<[ Render will be reset ]>");
11
10
  process.exit(1);
12
11
  });
@@ -105,7 +105,7 @@ interface CXConfig {
105
105
  COMPONENTS: "__components";
106
106
  ROOT: "__root";
107
107
  };
108
- dirs: (domain?: string) => Record<CXDir, string>;
108
+ paths: (domain?: string) => Record<CXDir, string>;
109
109
  }
110
110
 
111
111
  export {
@@ -7,16 +7,19 @@ import path from "node:path";
7
7
 
8
8
  import fsx from "fs-extra";
9
9
 
10
- // Consts
11
- const API_CACHE_DIR_PATH = path.resolve(__dirname, "./../apiCache");
12
- const SITE_HASH_FILENAME = `${API_CACHE_DIR_PATH}/siteHash.json`;
10
+ import { getConfig } from "./temp-utils";
11
+
12
+ const config = getConfig();
13
13
 
14
14
  /**
15
15
  * Creates an `apiCache` dir to store pages fetched from them API.
16
16
  */
17
17
  function createAPICacheDir() {
18
- if (!fs.existsSync(API_CACHE_DIR_PATH)) {
19
- fs.mkdirSync(API_CACHE_DIR_PATH);
18
+ const { __cx } = config.paths();
19
+ const apiCacheDir = path.join(__cx, "apiCache");
20
+
21
+ if (!fs.existsSync(apiCacheDir)) {
22
+ fs.mkdirSync(apiCacheDir);
20
23
  }
21
24
 
22
25
  console.info("Cache initialized");
@@ -29,10 +32,13 @@ function createAPICacheDir() {
29
32
  * @param petition An object
30
33
  */
31
34
  function generateFilenameWithHash(petition: Petition) {
35
+ const { __cx } = config.paths();
36
+ const apiCacheDir = path.join(__cx, "apiCache");
37
+
32
38
  const hashSum = crypto.createHash("sha256");
33
39
  hashSum.update(JSON.stringify(petition));
34
40
 
35
- return `${API_CACHE_DIR_PATH}/${hashSum.digest("hex")}`;
41
+ return `${apiCacheDir}/${hashSum.digest("hex")}`;
36
42
  }
37
43
 
38
44
  /**
@@ -88,8 +94,12 @@ function searchCacheData<T>(petition: Petition) {
88
94
  * Get site hashes from the file as an object
89
95
  */
90
96
  function getHashSites(): HashSites {
97
+ const { __cx } = config.paths();
98
+ const apiCacheDir = path.join(__cx, "apiCache");
99
+ const siteHasFilename = `${apiCacheDir}/siteHash.json`;
100
+
91
101
  try {
92
- const jsonData = fsx.readJSONSync(SITE_HASH_FILENAME, {
102
+ const jsonData = fsx.readJSONSync(siteHasFilename, {
93
103
  encoding: "utf-8",
94
104
  });
95
105
  return jsonData || {};
@@ -109,9 +119,13 @@ function updatedSiteHash(siteId: number, siteHash: SiteHash) {
109
119
  const lastHash = allHash[siteId];
110
120
  const currentHash = siteHash || lastHash || new Date().valueOf();
111
121
 
122
+ const { __cx } = config.paths();
123
+ const apiCacheDir = path.join(__cx, "apiCache");
124
+ const siteHasFilename = `${apiCacheDir}/siteHash.json`;
125
+
112
126
  if (currentHash !== lastHash) {
113
127
  allHash[siteId] = currentHash;
114
- fs.writeFileSync(SITE_HASH_FILENAME, JSON.stringify(allHash), {
128
+ fs.writeFileSync(siteHasFilename, JSON.stringify(allHash), {
115
129
  encoding: "utf-8",
116
130
  });
117
131
  }
@@ -120,9 +134,9 @@ function updatedSiteHash(siteId: number, siteHash: SiteHash) {
120
134
  }
121
135
 
122
136
  export {
137
+ createAPICacheDir,
123
138
  createSha256,
124
- updatedSiteHash,
125
- searchCacheData,
126
139
  saveCache,
127
- createAPICacheDir,
140
+ searchCacheData,
141
+ updatedSiteHash,
128
142
  };
@@ -1,24 +1,17 @@
1
- import path from "node:path";
2
-
3
1
  import { createAPICacheDir } from "./cache";
4
2
  import { sanitizeAPICacheDir } from "./shared";
5
- import { createStoreDir } from "./store";
6
3
  import { createStore } from "../services/store";
7
4
 
8
- const storeDir = path.resolve(__dirname, "../store/");
9
- const apiCacheDir = path.resolve(__dirname, "../apiCache");
10
-
11
5
  /**
12
6
  * Download all data: sites, pages etc.. from the instance private Griddo API.
13
- * Then you can use the generator funcion `getBuildPages()` to get the pages and
7
+ * Then you can use the generator funcion `getBuildPagesFromStore()` to get the pages and
14
8
  * `getBuildMetadata()` to get build and sites metadata as objects. Both from
15
9
  * exporter utils sites dir.
16
10
  */
17
11
  async function createBuildData(domain: string) {
18
12
  createAPICacheDir();
19
- createStoreDir(storeDir);
20
- await createStore(storeDir, domain);
21
- sanitizeAPICacheDir(apiCacheDir);
13
+ await createStore(domain);
14
+ sanitizeAPICacheDir();
22
15
  }
23
16
 
24
17
  export { createBuildData };
@@ -3,26 +3,25 @@ import path from "node:path";
3
3
 
4
4
  import fsx, { MakeDirectoryOptions } from "fs-extra";
5
5
 
6
- import { CXRootDir } from "./shared";
7
6
  import { getPageInStoreDir, removePagesFromStore } from "./store";
8
7
  import { getConfig } from "./temp-utils";
9
8
 
9
+ const config = getConfig();
10
+
10
11
  /**
11
12
  * Remove an empty directory from the basePath recursively.
12
13
  * If the directory has only .xml files it will handle as empty too (empty site)
13
14
  *
14
15
  * @param baseDir - The base directory.
15
16
  */
16
- const clearEmptyDirs = (baseDir?: string) => {
17
- const dir = baseDir || path.resolve(CXRootDir, "dist");
18
-
19
- const isDir = fsx.statSync(dir).isDirectory();
17
+ const clearEmptyDirs = (baseDir: string) => {
18
+ const isDir = fsx.statSync(baseDir).isDirectory();
20
19
  if (!isDir) {
21
20
  return;
22
21
  }
23
22
 
24
23
  // archivos o direvtorios dentro de `dir`
25
- let children = fsx.readdirSync(dir);
24
+ let children = fsx.readdirSync(baseDir);
26
25
  // let children = childrenRaw.filter((file) => {
27
26
  // return path.extname(file).toLowerCase() !== ".xml";
28
27
  // });
@@ -40,25 +39,25 @@ const clearEmptyDirs = (baseDir?: string) => {
40
39
  // Podemos borrarlos y dejar el dir vacío.
41
40
  if (childrenCount === xmlCount) {
42
41
  children.forEach(function (xmlFile) {
43
- const fullPath = path.join(dir, xmlFile);
42
+ const fullPath = path.join(baseDir, xmlFile);
44
43
  fsx.rmSync(fullPath);
45
44
  });
46
- children = fsx.readdirSync(dir);
45
+ children = fsx.readdirSync(baseDir);
47
46
  }
48
47
 
49
48
  children.forEach(function (file) {
50
- const fullPath = path.join(dir, file);
49
+ const fullPath = path.join(baseDir, file);
51
50
  clearEmptyDirs(fullPath);
52
51
  });
53
52
 
54
53
  // re-evaluate files; after deleting subdir we may have parent dir
55
54
  // empty now...
56
- children = fsx.readdirSync(dir);
55
+ children = fsx.readdirSync(baseDir);
57
56
  }
58
57
 
59
58
  // Si no tiene hijos, lo borramos
60
59
  if (children.length === 0) {
61
- fsx.rmdirSync(dir);
60
+ fsx.rmdirSync(baseDir);
62
61
  return;
63
62
  }
64
63
  };
@@ -245,12 +244,12 @@ function isVirtualPage(id: number) {
245
244
  }
246
245
 
247
246
  async function removeVirtualPagesFromStore() {
248
- const config = getConfig();
249
- const dirs = config.dirs();
250
- const storePath = path.join(dirs.__cx, "store");
247
+ const { __cx } = config.paths();
248
+ const storePath = path.join(__cx, "store");
249
+
251
250
  try {
252
251
  const multiPageFiles = getPageInStoreDir(storePath).filter(isVirtualPage);
253
- removePagesFromStore(storePath, multiPageFiles);
252
+ removePagesFromStore(multiPageFiles);
254
253
  } catch (e) {
255
254
  console.info(
256
255
  "`store` folder does not exist. Skipping multipage or static list template cleaning."
@@ -5,7 +5,7 @@ import fs from "node:fs";
5
5
  import path from "node:path";
6
6
 
7
7
  import { post } from "./api";
8
- import { getBuildPages } from "./store";
8
+ import { getBuildPagesFromCachedStore } from "./store";
9
9
  import { getConfig } from "./temp-utils";
10
10
 
11
11
  // Envs
@@ -112,7 +112,7 @@ async function uploadRenderedSearchContentToAPI(
112
112
  domain: string
113
113
  ) {
114
114
  const config = getConfig();
115
- const { __caches } = config.dirs(domain);
115
+ const { __caches } = config.paths(domain);
116
116
 
117
117
  // Extraemos el path de la carpeta store dentro de un dominio
118
118
  const storeFolder = path.join(__caches, "store");
@@ -126,8 +126,8 @@ async function uploadRenderedSearchContentToAPI(
126
126
  return;
127
127
  }
128
128
 
129
- // Obtenemos las páginas del store
130
- const storePages = getBuildPages(storeFolder);
129
+ // Obtenemos las páginas del store cacheado de un dominio
130
+ const storePages = getBuildPagesFromCachedStore(domain);
131
131
 
132
132
  for await (const store of storePages) {
133
133
  const {
@@ -12,6 +12,10 @@ import fs from "fs-extra";
12
12
  import gradient from "gradient-string";
13
13
  import pkgDir from "pkg-dir";
14
14
 
15
+ import { getConfig } from "./temp-utils";
16
+
17
+ const config = getConfig();
18
+
15
19
  dotenv.config();
16
20
 
17
21
  const GRIDDO_DEBUG_LOGS =
@@ -21,7 +25,6 @@ const GRIDDO_BUILD_LOGS =
21
25
  (!!process.env.GRIDDO_BUILD_LOGS &&
22
26
  !!JSON.parse(process.env.GRIDDO_BUILD_LOGS)) ||
23
27
  (!!process.env.LOGS && !!JSON.parse(process.env.LOGS));
24
- const CXRootDir = pkgDir.sync(__dirname)!; // usually monorepo/packages/griddo-cx/
25
28
  const instanceRootDir = pkgDir.sync()!; // instance root dir
26
29
 
27
30
  /**
@@ -188,8 +191,9 @@ function siteList(sites: Array<Site>) {
188
191
  * Print the great Griddo Exporter logo in ASCII.
189
192
  */
190
193
  function printExporterLogo(adapter: Adapters) {
194
+ const { __cx } = config.paths();
191
195
  const { version } = JSON.parse(
192
- fs.readFileSync(path.resolve(__dirname, "../package.json"), "utf-8")
196
+ fs.readFileSync(path.join(__cx, "package.json"), "utf-8")
193
197
  );
194
198
  const logo = `
195
199
  ··
@@ -204,11 +208,11 @@ function printExporterLogo(adapter: Adapters) {
204
208
  /**
205
209
  * Remove unused files (old) inside the `apiCache` dir
206
210
  *
207
- * @param dirPath The path for the `apiCache` dir
208
211
  * @todo remove other file types: sites, socials, etc..
209
212
  */
210
- function sanitizeAPICacheDir(dirPath: string) {
211
- // Read all `apiCache` file paths
213
+ function sanitizeAPICacheDir() {
214
+ const { __cx } = config.paths();
215
+ const dirPath = path.join(__cx, "apiCache");
212
216
  const allCachedFiles = fs.readdirSync(dirPath);
213
217
 
214
218
  // Object to store the the more rencent file names
@@ -320,7 +324,8 @@ function successLifeCyle(value: string) {
320
324
  * @param args - The arguments object.
321
325
  * @param args.steps - An array of functions to execute.
322
326
  * @param args.name - The name of the life cycle.
323
- * @param [args.attempts=1] - The number of retry attempts allowed in case of errors.
327
+ * @param args.attempts=1 - The number of retry attempts allowed in case of errors.
328
+ * @param args.bypass - Skip the step functions.
324
329
  * @returns - A promise that resolves when the life cycle process is completed.
325
330
  */
326
331
  async function doLifeCycle(args: {
@@ -329,6 +334,7 @@ async function doLifeCycle(args: {
329
334
  attempts?: number;
330
335
  }) {
331
336
  const { steps, name, attempts } = args;
337
+
332
338
  let trysCount = 0;
333
339
  const maxTrysAccepted = attempts || 1;
334
340
 
@@ -340,7 +346,12 @@ async function doLifeCycle(args: {
340
346
  // if no errors, go out!! :)
341
347
  break;
342
348
  } catch (error) {
343
- console.log(`Error in ${name}. Attempt (${trysCount + 1})`);
349
+ const errorString = chalk.bgRed.black(` Error in ${name} LifeCycle `);
350
+ const attemptsString = chalk.yellow(`Attempt (${trysCount + 1})`);
351
+ console.log(`\n${errorString} ${attemptsString}\n`);
352
+ console.log(error);
353
+ console.log();
354
+
344
355
  trysCount++;
345
356
  }
346
357
  }
@@ -371,7 +382,6 @@ function isVersionLowerThan(versionA: string, versionB: string) {
371
382
  }
372
383
 
373
384
  export {
374
- CXRootDir,
375
385
  debug,
376
386
  delay,
377
387
  doLifeCycle,
@@ -29,6 +29,8 @@ const GRIDDO_RENDER_PAGES = (
29
29
  .map((item) => parseInt(item))
30
30
  .filter(Boolean);
31
31
 
32
+ const config = getConfig();
33
+
32
34
  /**
33
35
  * Check the instance sites and returns site prepared to be published and unpublished.
34
36
  */
@@ -147,13 +149,9 @@ async function getSiteData(siteID: number) {
147
149
  * Save a file with the end of build process
148
150
  */
149
151
  async function generateBuildReport() {
150
- const config = getConfig();
151
- const dirs = config.dirs();
152
-
153
- const DIST_FOLDER = path.join(dirs.__cx, "dist");
152
+ const { __cx } = config.paths();
154
153
 
155
154
  const { buildProcessData } = await getBuildMetadata();
156
- const filePathName = path.join(DIST_FOLDER, "__build-report__.json");
157
155
 
158
156
  // Get the token
159
157
  const authControl = await AuthService.login();
@@ -168,7 +166,10 @@ async function generateBuildReport() {
168
166
  sites: buildSitesInfo,
169
167
  };
170
168
 
171
- fs.writeFileSync(filePathName, JSON.stringify(report));
169
+ fs.writeFileSync(
170
+ path.join(__cx, "dist", "__build-report__.json"),
171
+ JSON.stringify(report)
172
+ );
172
173
 
173
174
  logInfo(`Build report saved for ${buildSitesInfo.length} site(s)`);
174
175
  }
@@ -180,9 +181,8 @@ async function generateBuildReport() {
180
181
  */
181
182
  async function generateSitemaps() {
182
183
  const { sitesToPublish } = await getBuildMetadata();
183
- const config = getConfig();
184
- const dirs = config.dirs();
185
- const basePath = path.resolve(dirs.__cx, "dist");
184
+ const { __cx } = config.paths();
185
+ const distDir = path.join(__cx, "dist");
186
186
 
187
187
  const promisesOfSites = sitesToPublish.map(async (site) => {
188
188
  const { id: siteID, languages } = site;
@@ -211,7 +211,7 @@ async function generateSitemaps() {
211
211
  const sitemaps: Array<string> = [];
212
212
  const sitemapPageGroupKeys = Object.keys(sitemapPagesGroup);
213
213
 
214
- const sitemapBasePath = path.join(basePath, slug.replace(domain, ""));
214
+ const sitemapBasePath = path.join(distDir, slug.replace(domain, ""));
215
215
 
216
216
  for (const templateId of sitemapPageGroupKeys) {
217
217
  const sitemapPages = sitemapPagesGroup[templateId];
@@ -1,36 +1,64 @@
1
1
  import type { BuildMetaData } from "../types/api";
2
2
  import type { RenderInfo } from "../types/global";
3
3
  import type { GriddoPageObject } from "../types/pages";
4
+ import type { Site } from "../types/sites";
4
5
 
5
6
  import fs from "node:fs";
6
- import fsp from "node:fs/promises";
7
7
  import path from "node:path";
8
8
 
9
9
  import fsx from "fs-extra";
10
10
 
11
11
  import { removeProperties, walk } from "./shared";
12
12
  import { getConfig } from "./temp-utils";
13
- import { Site } from "../types/sites";
13
+
14
+ const config = getConfig();
14
15
 
15
16
  /**
16
- * Read all pages stored in `store` Griddo directory and return one by one with
17
- * a generator.
17
+ * Read all path pages stored in the `store` Griddo directory and returns the
18
+ * absolute file path.
19
+ *
20
+ * @param domain - The domain to get the pages from.
18
21
  */
19
- async function* getBuildPages<PageType extends GriddoPageObject>(
20
- basePath: string
22
+ function getBuildPagesFromCachedStore<PageType extends GriddoPageObject>(
23
+ domain: string
21
24
  ) {
22
- const pagesDirPath = path.resolve(basePath);
25
+ const { __caches } = config.paths(domain);
26
+ return getBuildPagesFromStore<PageType>({
27
+ basePath: path.join(__caches, "store"),
28
+ withSizeProp: false,
29
+ });
30
+ }
31
+
32
+ /**
33
+ * Read all pages stored in the `store` Griddo directory and returns one by one
34
+ * with a generator.
35
+ *
36
+ * @param basePath - Base directory to get pages from.
37
+ * @param options.withSizeProp - Add size prop to the page object.
38
+ * @todo throw error if the basePath is not an store folder
39
+ */
40
+ function* getBuildPagesFromStore<PageType extends GriddoPageObject>(args?: {
41
+ basePath?: string;
42
+ withSizeProp?: boolean;
43
+ }) {
44
+ const { basePath, withSizeProp } = args || {};
45
+ const { __cx } = config.paths();
46
+ const pagesDirPath = basePath || path.join(__cx, "store");
23
47
  const jsonFilePaths = walk(pagesDirPath).filter(
24
48
  (file) => path.extname(file) === ".json"
25
49
  );
26
50
 
27
51
  for (const filePath of jsonFilePaths) {
28
52
  try {
29
- const fileStats = await fsp.stat(filePath);
30
- const page = (await fsx.readJSON(filePath, {
53
+ const page = fsx.readJsonSync(filePath, {
31
54
  encoding: "utf-8",
32
- })) as PageType;
33
- page.size = fileStats.size / 1024;
55
+ }) as PageType;
56
+
57
+ if (withSizeProp) {
58
+ const fileStats = fs.statSync(filePath);
59
+ page.size = fileStats.size / 1024;
60
+ }
61
+
34
62
  // SECURITY: Only returns valid page objects
35
63
  if (page.path) {
36
64
  yield page;
@@ -41,18 +69,31 @@ async function* getBuildPages<PageType extends GriddoPageObject>(
41
69
  }
42
70
  }
43
71
 
72
+ /**
73
+ * Read all pages stored in `store` Griddo directory and return the absolute path.
74
+ */
75
+ function getBuildPagesPath() {
76
+ const config = getConfig();
77
+ const { __cx } = config.paths();
78
+
79
+ const PAGES_DIR = path.join(__cx, "store");
80
+ const PAGE_FILES = walk(PAGES_DIR).filter(
81
+ (file) => path.extname(file) === ".json"
82
+ );
83
+
84
+ return PAGE_FILES;
85
+ }
86
+
44
87
  /**
45
88
  * Get the build metadata from the Store.
46
89
  * TODO: Refactorizar para leer un solo archivo: __metadata__.json
47
90
  */
48
91
  async function getBuildMetadata(): Promise<BuildMetaData> {
49
- const config = getConfig();
50
- const dirs = config.dirs();
51
- const storePath = path.join(dirs.__cx, "store");
52
-
92
+ const { __cx } = config.paths();
53
93
  const { sitesToPublish, createdPages, buildProcessData } = fsx.readJSONSync(
54
- path.resolve(storePath, "metadata", "render-info.json")
94
+ path.join(__cx, "store", "metadata", "render-info.json")
55
95
  );
96
+
56
97
  return {
57
98
  buildProcessData,
58
99
  createdPages,
@@ -63,10 +104,13 @@ async function getBuildMetadata(): Promise<BuildMetaData> {
63
104
  /**
64
105
  * Creates an `store` dir to store pages transformed by createStore
65
106
  */
66
- function createStoreDir(storeDir: string) {
107
+ function createStoreDir() {
108
+ const { __cx } = config.paths();
109
+ const storeDir = path.join(__cx, "store");
110
+
67
111
  if (!fs.existsSync(storeDir)) {
68
112
  fs.mkdirSync(storeDir);
69
- fs.mkdirSync(path.resolve(storeDir, "metadata"));
113
+ fs.mkdirSync(path.join(storeDir, "metadata"));
70
114
  }
71
115
 
72
116
  console.info("Store initialized");
@@ -78,8 +122,10 @@ function createStoreDir(storeDir: string) {
78
122
  * @param renderInfo - Data that will be saved related to the render process.
79
123
  */
80
124
  function saveRenderInfoInStore(basePath: string, renderInfo: RenderInfo) {
125
+ const { __cx } = config.paths();
126
+
81
127
  fs.writeFileSync(
82
- path.resolve(basePath, "metadata", "render-info.json"),
128
+ path.join(__cx, "store", "metadata", "render-info.json"),
83
129
  JSON.stringify(renderInfo)
84
130
  );
85
131
  }
@@ -139,30 +185,32 @@ function getPageInStoreDir(basePath: string): Array<number> {
139
185
 
140
186
  /**
141
187
  * Save the pages into the file system.
142
- * @param basePath - Absolute path of the dir from which files will be saved.
143
188
  * @param pages - An array of Griddo page objects to be saved.
144
189
  */
145
- function savePagesInStore(basePath: string, pages: Array<GriddoPageObject>) {
190
+ function savePagesInStore(pages: Array<GriddoPageObject>) {
191
+ const { __cx } = config.paths();
192
+
146
193
  pages.forEach((page) => {
147
194
  removeProperties(page, ["editorID", "parentEditorID"]);
148
195
  const filename = `${page.context.page.id}.json`;
149
- const filePath = path.resolve(basePath, filename);
196
+ const filePath = path.join(__cx, "store", filename);
150
197
  fsx.writeJSONSync(filePath, page);
151
198
  });
152
199
  }
153
200
 
154
201
  /**
155
202
  * Remove files from dir.
156
- * @param basePath - Absolute path of the dir from which files will be removed.
157
203
  * @param filenames - An array of ids representing file page names.
158
204
  */
159
- function removePagesFromStore(basePath: string, filenames: Array<number>) {
205
+ function removePagesFromStore(filenames: Array<number>) {
206
+ const { __cx } = config.paths();
207
+
160
208
  if (filenames.length === 0) {
161
209
  return;
162
210
  }
163
211
 
164
212
  filenames.forEach((filename) => {
165
- const filePath = path.resolve(basePath, `${filename}.json`);
213
+ const filePath = path.join(__cx, "store", `${filename}.json`);
166
214
  try {
167
215
  fs.unlinkSync(filePath);
168
216
  } catch (error) {
@@ -219,7 +267,7 @@ async function getPagesToCreateOrDelete(
219
267
 
220
268
  // 1 - Leer el store
221
269
  // 2 - eliminar las páginas que tengan como site uno despublicado o borrado
222
- const allPagesInStore = getBuildPages(path.resolve(__dirname, "../store"));
270
+ const allPagesInStore = getBuildPagesFromStore();
223
271
 
224
272
  const pagesFromInvalidSites: Array<number> = [];
225
273
  for await (const page of allPagesInStore) {
@@ -247,7 +295,9 @@ async function getPagesToCreateOrDelete(
247
295
  export {
248
296
  createStoreDir,
249
297
  getBuildMetadata,
250
- getBuildPages,
298
+ getBuildPagesPath,
299
+ getBuildPagesFromCachedStore,
300
+ getBuildPagesFromStore,
251
301
  getPageInStoreDir,
252
302
  getPagesToCreateOrDelete,
253
303
  removePagesFromStore,
@@ -5,6 +5,8 @@ import fsx from "fs-extra";
5
5
  import { createDirsSync, removeDirsSync } from "./folders";
6
6
  import { CXConfig } from "../types/global";
7
7
 
8
+ const config = getConfig();
9
+
8
10
  function getConfig(): CXConfig {
9
11
  try {
10
12
  // eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-missing-require, node/no-unpublished-require
@@ -26,13 +28,12 @@ async function legacy__createDistFromGatsbyPublic(
26
28
  domain: string,
27
29
  needsAssetPrefix: boolean
28
30
  ) {
29
- const config = getConfig();
30
- const dirs = config.dirs(domain);
31
+ const { __cx, __ssg } = config.paths(domain);
31
32
 
32
- const cxDistDir = path.resolve(dirs.__cx, "dist");
33
- const cxAssetsDir = path.resolve(dirs.__cx, "assets");
34
- const cxDistDirWithDomain = path.join(dirs.__cx, "dist", domain);
35
- const publicDir = path.resolve(dirs.__ssg, "public");
33
+ const cxDistDir = path.join(__cx, "dist");
34
+ const cxAssetsDir = path.join(__cx, "assets");
35
+ const cxDistDirWithDomain = path.join(__cx, "dist", domain);
36
+ const publicDir = path.join(__ssg, "public");
36
37
 
37
38
  console.log(publicDir);
38
39
  const validFilesFromPublic = fsx
@@ -47,7 +48,7 @@ async function legacy__createDistFromGatsbyPublic(
47
48
  const pageDataSrc = `${publicDir}/page-data`;
48
49
  const pageDataDest = `${cxAssetsDir}/page-data`;
49
50
 
50
- const projectStaticSrc = path.resolve(dirs.__ssg, "static");
51
+ const projectStaticSrc = path.join(__ssg, "static");
51
52
  const projectStaticDest = cxAssetsDir;
52
53
 
53
54
  const gatsbyStaticSrc = `${cxDistDir}/static`;
@@ -82,15 +83,15 @@ async function legacy__createDistFromGatsbyPublic(
82
83
  }
83
84
 
84
85
  async function griddoCreateInitialDirectories(domain: string) {
85
- const config = getConfig();
86
- const { __exports, __caches } = config.dirs(domain);
86
+ const { __exports, __caches } = config.paths(domain);
87
+
87
88
  createDirsSync([__exports]);
88
89
  createDirsSync([__caches]);
89
90
  }
90
91
 
91
92
  async function griddoCleanDisposableDirectories(domain: string) {
92
- const config = getConfig();
93
- const { __cx } = config.dirs(domain);
93
+ const { __cx } = config.paths(domain);
94
+
94
95
  removeDirsSync(__cx, ["store", "apiCache", "dist"]);
95
96
  }
96
97