@griddo/cx 10.4.2 → 10.4.3
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.
- package/build/build-complete.js +18 -18
- package/build/create-build-data.js +19 -19
- package/build/index.js +30 -35
- package/build/reset-render.js +16 -16
- package/exporter/adapters/astro/index.ts +2 -2
- package/exporter/adapters/gatsby/index.ts +47 -125
- package/exporter/adapters/gatsby/utils.ts +6 -9
- package/exporter/build-complete.ts +1 -1
- package/exporter/index.ts +6 -9
- package/exporter/services/store.ts +5 -5
- package/exporter/types/global.ts +25 -9
- package/exporter/utils/cache.ts +4 -4
- package/exporter/utils/download-build-data.ts +10 -10
- package/exporter/utils/folders.ts +236 -43
- package/exporter/utils/searches.ts +14 -18
- package/exporter/utils/shared.ts +58 -33
- package/exporter/utils/store.ts +24 -24
- package/gatsby-node.ts +8 -10
- package/index.js +1 -1
- package/package.json +3 -3
- package/src/README.md +3 -3
- package/src/gatsby-node-utils.ts +5 -5
- package/src/types.ts +13 -5
- package/static/robots.txt +1 -1
|
@@ -1,13 +1,26 @@
|
|
|
1
|
+
import type { CXDir } from "../types/global";
|
|
1
2
|
import type { Site } from "../types/sites";
|
|
2
3
|
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
3
5
|
import path from "node:path";
|
|
4
6
|
|
|
5
7
|
import fs from "fs-extra";
|
|
6
8
|
|
|
7
|
-
import {
|
|
9
|
+
import { CXRootDir, instanceRootDir, logInfo } from "./shared";
|
|
10
|
+
// eslint-disable-next-line node/no-unpublished-import
|
|
11
|
+
import config from "../../cx.config";
|
|
12
|
+
|
|
13
|
+
export const STORE_DIR = path.resolve(__dirname, "../store/");
|
|
14
|
+
export const DIST_DIR = path.resolve(__dirname, "../dist/");
|
|
15
|
+
|
|
16
|
+
export const gatsbyRootDir = path.resolve(__dirname, "../");
|
|
17
|
+
export const exportArchiveBasePath = path.resolve(
|
|
18
|
+
instanceRootDir,
|
|
19
|
+
"exports/sites"
|
|
20
|
+
);
|
|
8
21
|
|
|
9
22
|
/**
|
|
10
|
-
* Delete sites
|
|
23
|
+
* Delete sites dirs that will be updated from de file system.
|
|
11
24
|
*
|
|
12
25
|
* @param updateSites An array of sites to be updated.
|
|
13
26
|
*/
|
|
@@ -21,30 +34,30 @@ async function deleteSites(updatedSites: Array<Site>, DOMAIN: string) {
|
|
|
21
34
|
? domainFromSite.split(DOMAIN).join("")
|
|
22
35
|
: domainFromSite;
|
|
23
36
|
|
|
24
|
-
const
|
|
25
|
-
const
|
|
37
|
+
const siteDir = path.resolve(__dirname, `../public${domainPath}`);
|
|
38
|
+
const pageDataDir = path.resolve(
|
|
26
39
|
__dirname,
|
|
27
40
|
`../public/page-data${domainPath}`
|
|
28
41
|
);
|
|
29
42
|
|
|
30
|
-
logInfo(`Site dir ${
|
|
31
|
-
logInfo(`Page data dir ${
|
|
43
|
+
logInfo(`Site dir ${siteDir}`);
|
|
44
|
+
logInfo(`Page data dir ${pageDataDir}`);
|
|
32
45
|
|
|
33
46
|
// delete directory recursively
|
|
34
|
-
if (!fs.existsSync(
|
|
47
|
+
if (!fs.existsSync(siteDir)) continue;
|
|
35
48
|
|
|
36
49
|
try {
|
|
37
|
-
await fs.rm(
|
|
38
|
-
logInfo(`${
|
|
50
|
+
await fs.rm(siteDir, { recursive: true });
|
|
51
|
+
logInfo(`${siteDir} was deleted!`);
|
|
39
52
|
} catch (err) {
|
|
40
53
|
console.log(err);
|
|
41
54
|
}
|
|
42
55
|
|
|
43
|
-
if (!fs.existsSync(
|
|
56
|
+
if (!fs.existsSync(pageDataDir)) continue;
|
|
44
57
|
|
|
45
58
|
try {
|
|
46
|
-
await fs.rm(
|
|
47
|
-
console.info(`${
|
|
59
|
+
await fs.rm(pageDataDir, { recursive: true });
|
|
60
|
+
console.info(`${pageDataDir} was deleted!`);
|
|
48
61
|
} catch (err) {
|
|
49
62
|
console.log(err);
|
|
50
63
|
}
|
|
@@ -55,80 +68,260 @@ async function deleteSites(updatedSites: Array<Site>, DOMAIN: string) {
|
|
|
55
68
|
/**
|
|
56
69
|
* Remove empty site dirst (with or without sitemaps xmls)
|
|
57
70
|
*/
|
|
58
|
-
const clearEmptyDirs = (
|
|
59
|
-
const
|
|
71
|
+
const clearEmptyDirs = (baseDir?: string) => {
|
|
72
|
+
const dir = baseDir || path.resolve(CXRootDir, "dist");
|
|
60
73
|
|
|
61
|
-
const isDir = fs.statSync(
|
|
74
|
+
const isDir = fs.statSync(dir).isDirectory();
|
|
62
75
|
if (!isDir) {
|
|
63
76
|
return;
|
|
64
77
|
}
|
|
65
78
|
|
|
66
|
-
// archivos o direvtorios dentro de `
|
|
67
|
-
let children = fs.readdirSync(
|
|
79
|
+
// archivos o direvtorios dentro de `dir`
|
|
80
|
+
let children = fs.readdirSync(dir);
|
|
68
81
|
// let children = childrenRaw.filter((file) => {
|
|
69
82
|
// return path.extname(file).toLowerCase() !== ".xml";
|
|
70
83
|
// });
|
|
71
|
-
const
|
|
84
|
+
const dirHasChildren = children.length > 0;
|
|
72
85
|
|
|
73
86
|
// Por cada uno de ellos miramos si tiene hijos, enviamos cada uno de ellos
|
|
74
87
|
// a `clearEmptyDirsWithXML`
|
|
75
|
-
if (
|
|
88
|
+
if (dirHasChildren) {
|
|
76
89
|
const childrenCount = children.length;
|
|
77
90
|
const xmlCount = children.filter((file) => {
|
|
78
91
|
return path.extname(file).toLowerCase() === ".xml";
|
|
79
92
|
}).length;
|
|
80
93
|
|
|
81
94
|
// Todos los hijos son archivos .xml
|
|
82
|
-
// Podemos borrarlos y dejar el
|
|
95
|
+
// Podemos borrarlos y dejar el dir vacío.
|
|
83
96
|
if (childrenCount === xmlCount) {
|
|
84
97
|
children.forEach(function (xmlFile) {
|
|
85
|
-
const fullPath = path.join(
|
|
98
|
+
const fullPath = path.join(dir, xmlFile);
|
|
86
99
|
fs.rmSync(fullPath);
|
|
87
100
|
});
|
|
88
|
-
children = fs.readdirSync(
|
|
101
|
+
children = fs.readdirSync(dir);
|
|
89
102
|
}
|
|
90
103
|
|
|
91
104
|
children.forEach(function (file) {
|
|
92
|
-
const fullPath = path.join(
|
|
105
|
+
const fullPath = path.join(dir, file);
|
|
93
106
|
clearEmptyDirs(fullPath);
|
|
94
107
|
});
|
|
95
108
|
|
|
96
|
-
// re-evaluate files; after deleting
|
|
109
|
+
// re-evaluate files; after deleting subdir we may have parent dir
|
|
97
110
|
// empty now...
|
|
98
|
-
children = fs.readdirSync(
|
|
111
|
+
children = fs.readdirSync(dir);
|
|
99
112
|
}
|
|
100
113
|
|
|
101
114
|
// Si no tiene hijos, lo borramos
|
|
102
115
|
if (children.length === 0) {
|
|
103
|
-
fs.rmdirSync(
|
|
116
|
+
fs.rmdirSync(dir);
|
|
104
117
|
return;
|
|
105
118
|
}
|
|
106
119
|
};
|
|
107
120
|
|
|
108
|
-
|
|
109
|
-
|
|
121
|
+
/**
|
|
122
|
+
* Copy artifacts between CX valid directories.
|
|
123
|
+
*
|
|
124
|
+
* @param args.domain - The render domain
|
|
125
|
+
* @param args.from - Source dir
|
|
126
|
+
* @param args.to - Destination dir
|
|
127
|
+
* @param args.artifacts - Artifact names
|
|
128
|
+
* @param args.withBackup - Create a previous backup
|
|
129
|
+
*/
|
|
130
|
+
async function copyArtifacts(args: {
|
|
131
|
+
domain: string;
|
|
132
|
+
from: CXDir;
|
|
133
|
+
to: CXDir;
|
|
134
|
+
artifacts: Array<string>;
|
|
135
|
+
withBackup?: boolean;
|
|
136
|
+
}) {
|
|
137
|
+
const { artifacts, domain, from, to, withBackup } = args;
|
|
138
|
+
|
|
139
|
+
const dirs = config.dirs(domain);
|
|
110
140
|
|
|
111
|
-
const
|
|
112
|
-
|
|
141
|
+
for (const artifact of artifacts) {
|
|
142
|
+
const src = path.join(dirs[from], artifact);
|
|
143
|
+
const dest = path.join(dirs[to], artifact);
|
|
144
|
+
|
|
145
|
+
// The dir we want to copy, doesn't exist.
|
|
146
|
+
if (!fs.existsSync(src)) {
|
|
147
|
+
console.log(`Source directory does not exist: ${src}`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Create the backup
|
|
152
|
+
if (withBackup) {
|
|
153
|
+
createBackup(dest);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Copy artifact
|
|
157
|
+
try {
|
|
158
|
+
// First clean destination
|
|
159
|
+
if (fs.existsSync(dest)) {
|
|
160
|
+
spawnSync("rm", ["-rf", dest]);
|
|
161
|
+
}
|
|
162
|
+
spawnSync("cp", ["-Rp", src, dest]);
|
|
163
|
+
if (withBackup) {
|
|
164
|
+
deleteBackup(dest);
|
|
165
|
+
}
|
|
166
|
+
} catch (e) {
|
|
167
|
+
console.log("Copy failed!");
|
|
168
|
+
if (withBackup) {
|
|
169
|
+
restoreBackup(dest);
|
|
170
|
+
console.log("Backup has been restored.");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Move artifacts between CX valid directories.
|
|
178
|
+
*
|
|
179
|
+
* @param domain - The render domain
|
|
180
|
+
* @param from - Source dir
|
|
181
|
+
* @param to - Destination dir
|
|
182
|
+
* @param artifacts - Artifact names
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* // normal
|
|
186
|
+
* moveArtifacts({domain: domain, from: "__cx", to: "__ssg", ["dist"]})
|
|
187
|
+
* // renaming
|
|
188
|
+
* moveArtifacts({
|
|
189
|
+
*
|
|
190
|
+
* domain: domain,
|
|
191
|
+
* from: "__cx",
|
|
192
|
+
* to: "__ssg",
|
|
193
|
+
* ["foo", "taz", "bar"],
|
|
194
|
+
* ["foo", "taz", "bar-renamed"]
|
|
195
|
+
*
|
|
196
|
+
* })
|
|
197
|
+
* // with backup
|
|
198
|
+
* moveArtifacts({domain: domain, from: "__cx", to: "__ssg", ["dist"]})
|
|
199
|
+
*/
|
|
200
|
+
function moveArtifacts(args: {
|
|
201
|
+
domain: string;
|
|
202
|
+
from: CXDir;
|
|
203
|
+
to: CXDir;
|
|
204
|
+
artifacts: Array<string>;
|
|
205
|
+
withBackup?: boolean;
|
|
206
|
+
}) {
|
|
207
|
+
const { artifacts, domain, from, to, withBackup } = args;
|
|
208
|
+
|
|
209
|
+
const dirs = config.dirs(domain);
|
|
210
|
+
|
|
211
|
+
for (const artifact of artifacts) {
|
|
212
|
+
const src = path.join(dirs[from], artifact);
|
|
213
|
+
const dest = path.join(dirs[to], artifact);
|
|
214
|
+
|
|
215
|
+
// The dir we want to move, doesn't exist.
|
|
216
|
+
if (!fs.existsSync(src)) {
|
|
217
|
+
console.log(`Source directory does not exist: ${src}`);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (withBackup) {
|
|
222
|
+
createBackup(dest);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
// First clean destination
|
|
227
|
+
if (fs.existsSync(dest)) {
|
|
228
|
+
spawnSync("rm", ["-rf", dest]);
|
|
229
|
+
}
|
|
230
|
+
spawnSync("mv", [src, dest]);
|
|
231
|
+
if (withBackup) {
|
|
232
|
+
deleteBackup(dest);
|
|
233
|
+
}
|
|
234
|
+
} catch (e) {
|
|
235
|
+
console.log("Move failed!");
|
|
236
|
+
if (withBackup) {
|
|
237
|
+
restoreBackup(dest);
|
|
238
|
+
console.log("Backup has been restored.");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Remove artifacts from CX valid directories.
|
|
246
|
+
*
|
|
247
|
+
* @param domain - The render domain
|
|
248
|
+
* @param from - Source dir
|
|
249
|
+
* @param to - Destination dir
|
|
250
|
+
* @param artifacts - Artifact names
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* removeArtifacts(domain, "__cx", "__ssg", ["dist"], ["public"])
|
|
254
|
+
*/
|
|
255
|
+
function removeArtifacts(args: {
|
|
256
|
+
domain: string;
|
|
257
|
+
from: CXDir;
|
|
258
|
+
artifacts: Array<string>;
|
|
259
|
+
}) {
|
|
260
|
+
const { artifacts, domain, from } = args;
|
|
261
|
+
|
|
262
|
+
const dirs = config.dirs(domain);
|
|
263
|
+
|
|
264
|
+
for (const artifact of artifacts) {
|
|
265
|
+
if (artifact) {
|
|
266
|
+
const src = path.join(dirs[from], artifact);
|
|
267
|
+
if (fs.existsSync(src)) {
|
|
268
|
+
spawnSync("rm", ["-rf", src]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function restoreBackup(src: string, suffix = "-BACKUP") {
|
|
275
|
+
const dest = src + suffix;
|
|
276
|
+
try {
|
|
277
|
+
spawnSync("mv", [dest, src]);
|
|
278
|
+
console.log(`Backup ${dest} has been restored`);
|
|
279
|
+
} catch (e) {
|
|
280
|
+
console.log(`Error while delete ${dest} backup`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function deleteBackup(src: string, suffix = "-BACKUP") {
|
|
285
|
+
const dest = src + suffix;
|
|
286
|
+
|
|
287
|
+
if (!fs.existsSync(dest)) {
|
|
288
|
+
console.log(`Source ${dest} does not exist`);
|
|
113
289
|
return;
|
|
114
290
|
}
|
|
115
291
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
292
|
+
try {
|
|
293
|
+
spawnSync("rm", ["-rf", dest]);
|
|
294
|
+
console.log(`Backup ${dest} has been deleted`);
|
|
295
|
+
} catch (e) {
|
|
296
|
+
console.log(`Error while delete ${dest} backup`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function createBackup(src: string, suffix = "-BACKUP") {
|
|
301
|
+
const dest = src + suffix;
|
|
122
302
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
303
|
+
if (!fs.existsSync(src)) {
|
|
304
|
+
console.log(`Source ${src} does not exist`);
|
|
305
|
+
return;
|
|
126
306
|
}
|
|
127
307
|
|
|
128
|
-
if (
|
|
129
|
-
|
|
308
|
+
if (fs.existsSync(dest)) {
|
|
309
|
+
console.log(`Destination ${dest} already exists`);
|
|
130
310
|
return;
|
|
131
311
|
}
|
|
132
|
-
};
|
|
133
312
|
|
|
134
|
-
|
|
313
|
+
try {
|
|
314
|
+
spawnSync("mv", [src, dest]);
|
|
315
|
+
console.log(`Backup of ${src} has been created in ${dest}`);
|
|
316
|
+
} catch (e) {
|
|
317
|
+
console.log(`Error while coping ${src} to ${dest} backup`);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export {
|
|
322
|
+
clearEmptyDirs,
|
|
323
|
+
copyArtifacts,
|
|
324
|
+
deleteSites,
|
|
325
|
+
moveArtifacts,
|
|
326
|
+
removeArtifacts,
|
|
327
|
+
};
|
|
@@ -103,35 +103,31 @@ function stripSpaces(str: string) {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
-
* Function that search in the
|
|
106
|
+
* Function that search in the `/public` dir the content info of the pages and
|
|
107
|
+
* send it to the search table in the ddbb using the API.
|
|
107
108
|
*/
|
|
108
109
|
async function uploadSearchContentToAPI() {
|
|
109
110
|
// Extraemos el path de la carpeta store
|
|
110
|
-
const
|
|
111
|
+
const storeDir = path.resolve(__dirname, `../store`);
|
|
111
112
|
// Convertimos el string que nos devuelve el env en un booleano
|
|
112
|
-
const isDomainStrippedFromPath: boolean = JSON.parse(
|
|
113
|
-
|
|
113
|
+
const isDomainStrippedFromPath: boolean = JSON.parse(
|
|
114
|
+
process.env.GRIDDO_EXPORT_STRIP_DOMAIN_FROM_PATH || "false"
|
|
115
|
+
);
|
|
114
116
|
|
|
115
|
-
for await (const store of getBuildPages(
|
|
116
|
-
const {
|
|
117
|
-
context: {
|
|
118
|
-
page,
|
|
119
|
-
openGraph
|
|
120
|
-
}
|
|
117
|
+
for await (const store of getBuildPages(storeDir)) {
|
|
118
|
+
const {
|
|
119
|
+
context: { page, openGraph },
|
|
121
120
|
} = store;
|
|
122
121
|
|
|
123
|
-
const {
|
|
124
|
-
compose,
|
|
125
|
-
domain
|
|
126
|
-
} = page.fullPath;
|
|
122
|
+
const { compose, domain } = page.fullPath;
|
|
127
123
|
|
|
128
|
-
const contextPath = isDomainStrippedFromPath
|
|
124
|
+
const contextPath = isDomainStrippedFromPath
|
|
129
125
|
? path.resolve(__dirname, `../dist/${compose}/index.html`)
|
|
130
126
|
: path.resolve(__dirname, `../dist${domain}${compose}/index.html`);
|
|
131
|
-
|
|
127
|
+
|
|
132
128
|
const content = fs.readFileSync(contextPath).toString();
|
|
133
129
|
|
|
134
|
-
const pageObject: PostSearchInfoProps = {
|
|
130
|
+
const pageObject: PostSearchInfoProps = {
|
|
135
131
|
siteId: page.site,
|
|
136
132
|
pageId: page.id,
|
|
137
133
|
title: page.title,
|
|
@@ -140,7 +136,7 @@ async function uploadSearchContentToAPI() {
|
|
|
140
136
|
template: page.template.templateType || page.templateId,
|
|
141
137
|
description: openGraph.description,
|
|
142
138
|
image: openGraph.image,
|
|
143
|
-
content
|
|
139
|
+
content,
|
|
144
140
|
};
|
|
145
141
|
|
|
146
142
|
await postSearchInfo(pageObject);
|
package/exporter/utils/shared.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { Adapters } from "../adapters";
|
|
1
2
|
import type { APIResponses } from "../types/api";
|
|
3
|
+
import type { LifeCyclesNames } from "../types/global";
|
|
2
4
|
import type { APIPageObject } from "../types/pages";
|
|
3
5
|
import type { Site } from "../types/sites";
|
|
4
6
|
|
|
@@ -9,7 +11,6 @@ import gradient from "gradient-string";
|
|
|
9
11
|
import pkgDir from "pkg-dir";
|
|
10
12
|
|
|
11
13
|
import { version } from "../../package.json";
|
|
12
|
-
import { Adapters } from "../adapters";
|
|
13
14
|
|
|
14
15
|
dotenv.config();
|
|
15
16
|
|
|
@@ -20,8 +21,8 @@ const GRIDDO_BUILD_LOGS =
|
|
|
20
21
|
(!!process.env.GRIDDO_BUILD_LOGS &&
|
|
21
22
|
!!JSON.parse(process.env.GRIDDO_BUILD_LOGS)) ||
|
|
22
23
|
(!!process.env.LOGS && !!JSON.parse(process.env.LOGS));
|
|
23
|
-
const
|
|
24
|
-
const
|
|
24
|
+
const CXRootDir = pkgDir.sync(__dirname)!; // usually monorepo/packages/griddo-cx/
|
|
25
|
+
const instanceRootDir = pkgDir.sync()!; // instance root dir
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Walk a directory and returns the file pathts.
|
|
@@ -183,28 +184,12 @@ function siteList(sites: Array<Site>) {
|
|
|
183
184
|
return sites.map(({ name, id }) => `${name} (${id})`).join(", ");
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
/**
|
|
187
|
-
* Print a f**king great Griddo build domain logo.
|
|
188
|
-
*/
|
|
189
|
-
function splash() {
|
|
190
|
-
const logo = `
|
|
191
|
-
___ _ _ _ _ ___ ___ ____ _ _ ____ _ __ _
|
|
192
|
-
|==] |__| | |___ |__> |__> [__] |\\/| |--| | | \\|
|
|
193
|
-
`;
|
|
194
|
-
|
|
195
|
-
console.log(gradient.cristal(logo));
|
|
196
|
-
}
|
|
197
|
-
|
|
198
187
|
/**
|
|
199
188
|
* Print the great Griddo Exporter logo in ASCII.
|
|
200
189
|
*/
|
|
201
190
|
function printExporterLogo(adapter: Adapters) {
|
|
202
191
|
const logo = `
|
|
203
192
|
··
|
|
204
|
-
·· _______ _ _ _____ _____ ______ _______ _______ ______
|
|
205
|
-
·· |______ \\___/ |_____] | | |_____/ | |______ |_____/
|
|
206
|
-
·· |______ _/ \\_ | |_____| | \\_ | |______ | \\_
|
|
207
|
-
··
|
|
208
193
|
·· Griddo Exporter ${version}
|
|
209
194
|
·· Adapter: ${adapter}
|
|
210
195
|
··
|
|
@@ -214,14 +199,14 @@ function printExporterLogo(adapter: Adapters) {
|
|
|
214
199
|
}
|
|
215
200
|
|
|
216
201
|
/**
|
|
217
|
-
* Remove unused files (old) inside the `apiCache`
|
|
202
|
+
* Remove unused files (old) inside the `apiCache` dir
|
|
218
203
|
*
|
|
219
|
-
* @param
|
|
204
|
+
* @param dirPath The path for the `apiCache` dir
|
|
220
205
|
* @todo remove other file types: sites, socials, etc..
|
|
221
206
|
*/
|
|
222
|
-
function
|
|
207
|
+
function sanitizeAPICacheDir(dirPath: string) {
|
|
223
208
|
// Read all `apiCache` file paths
|
|
224
|
-
const allCachedFiles = fs.readdirSync(
|
|
209
|
+
const allCachedFiles = fs.readdirSync(dirPath);
|
|
225
210
|
|
|
226
211
|
// Object to store the the more rencent file names
|
|
227
212
|
// Record<string, string> = { "234856872634", "3268746238747238.json"};
|
|
@@ -231,7 +216,7 @@ function sanitizeAPICacheFolder(folderPath: string) {
|
|
|
231
216
|
// Page files.
|
|
232
217
|
// We only need files that describes a page object.
|
|
233
218
|
const pageFilePaths = allCachedFiles.filter((fileName) => {
|
|
234
|
-
const filePath = `${
|
|
219
|
+
const filePath = `${dirPath}/${fileName}`;
|
|
235
220
|
const fileObject = fs.readJSONSync(filePath, "utf-8") as APIPageObject;
|
|
236
221
|
const { id, entity, fullUrl } = fileObject;
|
|
237
222
|
|
|
@@ -241,7 +226,7 @@ function sanitizeAPICacheFolder(folderPath: string) {
|
|
|
241
226
|
|
|
242
227
|
// Fill the filesById object
|
|
243
228
|
for (const fileName of pageFilePaths) {
|
|
244
|
-
const filePath = `${
|
|
229
|
+
const filePath = `${dirPath}/${fileName}`;
|
|
245
230
|
const fileObject = fs.readJSONSync(filePath, "utf-8") as APIPageObject;
|
|
246
231
|
const fileCreationDate = fs.statSync(filePath).mtimeMs;
|
|
247
232
|
|
|
@@ -251,8 +236,7 @@ function sanitizeAPICacheFolder(folderPath: string) {
|
|
|
251
236
|
// that the stored one.
|
|
252
237
|
const validPageFile =
|
|
253
238
|
!filesByIdMap[id] ||
|
|
254
|
-
fileCreationDate >
|
|
255
|
-
fs.statSync(`${folderPath}/${filesByIdMap[id]}`).mtimeMs;
|
|
239
|
+
fileCreationDate > fs.statSync(`${dirPath}/${filesByIdMap[id]}`).mtimeMs;
|
|
256
240
|
|
|
257
241
|
if (validPageFile) filesByIdMap[id] = fileName;
|
|
258
242
|
}
|
|
@@ -262,7 +246,7 @@ function sanitizeAPICacheFolder(folderPath: string) {
|
|
|
262
246
|
|
|
263
247
|
// Delete files using the store object filesById as reference map.
|
|
264
248
|
for (const fileName of pageFilePaths) {
|
|
265
|
-
const filePath = `${
|
|
249
|
+
const filePath = `${dirPath}/${fileName}`;
|
|
266
250
|
const fileObject = fs.readJSONSync(filePath, "utf-8") as APIPageObject;
|
|
267
251
|
|
|
268
252
|
const { id } = fileObject;
|
|
@@ -274,7 +258,7 @@ function sanitizeAPICacheFolder(folderPath: string) {
|
|
|
274
258
|
}
|
|
275
259
|
}
|
|
276
260
|
|
|
277
|
-
console.log(`Sanitize apiCache
|
|
261
|
+
console.log(`Sanitize apiCache dir for ${counter} files`);
|
|
278
262
|
}
|
|
279
263
|
|
|
280
264
|
/**
|
|
@@ -324,11 +308,53 @@ function successLifeCyle(value: string) {
|
|
|
324
308
|
console.info(`${chalk.green("success")} ${value}`);
|
|
325
309
|
}
|
|
326
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Executes a life cycle process, which involves executing an array of
|
|
313
|
+
* functions, printing to the console, and handling errors with optional
|
|
314
|
+
* retries.
|
|
315
|
+
*
|
|
316
|
+
* @async
|
|
317
|
+
* @param args - The arguments object.
|
|
318
|
+
* @param args.steps - An array of functions to execute.
|
|
319
|
+
* @param args.name - The name of the life cycle.
|
|
320
|
+
* @param [args.attempts=1] - The number of retry attempts allowed in case of errors.
|
|
321
|
+
* @returns - A promise that resolves when the life cycle process is completed.
|
|
322
|
+
*/
|
|
323
|
+
async function doLifeCycle(args: {
|
|
324
|
+
steps: Array<(...args: Array<unknown>) => unknown | Promise<any>>;
|
|
325
|
+
name: LifeCyclesNames;
|
|
326
|
+
attempts?: number;
|
|
327
|
+
}) {
|
|
328
|
+
const { steps, name, attempts } = args;
|
|
329
|
+
let trysCount = 0;
|
|
330
|
+
const maxTrysAccepted =
|
|
331
|
+
Number(process.env.GRIDDO_LIFECYCLE_MAX_RETRY_ATTEMPTS) || attempts || 1;
|
|
332
|
+
|
|
333
|
+
while (trysCount < maxTrysAccepted) {
|
|
334
|
+
try {
|
|
335
|
+
console.info(`\n${chalk.blue(`info`)} start ${name} life-cycle`);
|
|
336
|
+
const exeTime = await measureExecutionTime(...steps);
|
|
337
|
+
console.info(`${chalk.green("success")} ${name} - ${exeTime}s`);
|
|
338
|
+
// if no errors, go out!! :)
|
|
339
|
+
break;
|
|
340
|
+
} catch (error) {
|
|
341
|
+
console.log(`Error in ${name}. Attempt (${trysCount + 1})`);
|
|
342
|
+
trysCount++;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (trysCount === maxTrysAccepted) {
|
|
346
|
+
throw new Error(
|
|
347
|
+
`Exceeded maximum retry attempts (${maxTrysAccepted}) for ${name} LifeCycle`
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
327
352
|
export {
|
|
328
|
-
|
|
353
|
+
CXRootDir,
|
|
329
354
|
debug,
|
|
330
355
|
delay,
|
|
331
|
-
|
|
356
|
+
doLifeCycle,
|
|
357
|
+
instanceRootDir,
|
|
332
358
|
logBox,
|
|
333
359
|
logInfo,
|
|
334
360
|
logPageSize,
|
|
@@ -336,9 +362,8 @@ export {
|
|
|
336
362
|
pause,
|
|
337
363
|
printExporterLogo,
|
|
338
364
|
removeProperties,
|
|
339
|
-
|
|
365
|
+
sanitizeAPICacheDir,
|
|
340
366
|
siteList,
|
|
341
|
-
splash,
|
|
342
367
|
startLifeCycle,
|
|
343
368
|
successLifeCyle,
|
|
344
369
|
walk,
|