@griddo/cx 11.9.17 → 11.10.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.
- package/README.md +13 -240
- package/build/commands/end-render.d.ts +0 -1
- package/build/commands/end-render.js +31 -0
- package/build/commands/end-render.js.map +7 -0
- package/build/commands/prepare-assets-directory.js +9 -0
- package/build/commands/prepare-assets-directory.js.map +7 -0
- package/build/commands/prepare-domains-render.js +40 -0
- package/build/commands/prepare-domains-render.js.map +7 -0
- package/build/commands/reset-render.d.ts +0 -1
- package/build/commands/reset-render.js +31 -0
- package/build/commands/reset-render.js.map +7 -0
- package/build/commands/single-domain-upload-search-content.d.ts +1 -0
- package/build/commands/start-render.d.ts +0 -1
- package/build/commands/start-render.js +66 -0
- package/build/commands/start-render.js.map +7 -0
- package/build/commands/upload-search-content.d.ts +0 -1
- package/build/commands/upload-search-content.js +32 -0
- package/build/commands/upload-search-content.js.map +7 -0
- package/build/core/GriddoLog.d.ts +16 -0
- package/build/{utils/health-checks.d.ts → core/check-env-health.d.ts} +4 -2
- package/build/core/db-class.d.ts +11 -0
- package/build/core/db.d.ts +4 -0
- package/build/core/dist-rollback.d.ts +11 -0
- package/build/core/errors.d.ts +26 -0
- package/build/core/fs.d.ts +69 -0
- package/build/core/life-cycle.d.ts +26 -0
- package/build/core/logger.d.ts +18 -0
- package/build/core/objects.d.ts +11 -0
- package/build/core/print-logos.d.ts +5 -0
- package/build/index.d.ts +10 -29
- package/build/index.js +404 -73
- package/build/react/DynamicScript/index.d.ts +4 -0
- package/build/react/GriddoFavicon/index.d.ts +4 -0
- package/build/react/GriddoIntegrations/index.d.ts +3 -4
- package/build/react/GriddoIntegrations/utils.d.ts +7 -6
- package/build/react/GriddoOpenGraph/index.d.ts +10 -0
- package/build/react/index.d.ts +3 -2
- package/build/react/index.js +1 -3
- package/build/{utils → services}/api.d.ts +1 -1
- package/build/services/auth.d.ts +2 -5
- package/build/services/domains.d.ts +3 -4
- package/build/services/manage-sites.d.ts +22 -0
- package/build/services/manage-store.d.ts +32 -0
- package/build/services/navigation.d.ts +16 -16
- package/build/{utils → services}/pages.d.ts +3 -3
- package/build/services/reference-fields.d.ts +3 -3
- package/build/services/render-artifacts.d.ts +6 -0
- package/build/services/render.d.ts +70 -0
- package/build/services/robots.d.ts +2 -19
- package/build/services/sitemaps.d.ts +5 -0
- package/build/services/sites.d.ts +8 -5
- package/build/services/store.d.ts +10 -1
- package/build/shared/context.d.ts +36 -0
- package/build/shared/envs.d.ts +17 -0
- package/build/{errors/errors-data.d.ts → shared/errors.d.ts} +5 -3
- package/build/shared/npm-modules/brush.d.ts +18 -0
- package/build/shared/npm-modules/find-up-simple.d.ts +34 -0
- package/build/shared/npm-modules/pkg-dir.d.ts +7 -0
- package/build/shared/npm-modules/xml-parser.d.ts +4 -0
- package/build/{types → shared/types}/api.d.ts +18 -18
- package/build/{types → shared/types}/global.d.ts +15 -16
- package/build/{types → shared/types}/navigation.d.ts +5 -5
- package/build/{types → shared/types}/pages.d.ts +9 -9
- package/build/shared/types/render.d.ts +56 -0
- package/build/{types → shared/types}/sites.d.ts +18 -19
- package/build/shared/types.d.ts +15 -0
- package/build/ssg-adapters/gatsby/actions/clean.d.ts +3 -0
- package/build/ssg-adapters/gatsby/actions/close.d.ts +3 -0
- package/build/ssg-adapters/gatsby/actions/data.d.ts +2 -0
- package/build/ssg-adapters/gatsby/actions/healthCheck.d.ts +2 -0
- package/build/ssg-adapters/gatsby/actions/init.d.ts +2 -0
- package/build/ssg-adapters/gatsby/actions/logs.d.ts +3 -0
- package/build/ssg-adapters/gatsby/actions/meta.d.ts +2 -0
- package/build/ssg-adapters/gatsby/actions/prepare.d.ts +2 -0
- package/build/ssg-adapters/gatsby/actions/relocation.d.ts +2 -0
- package/build/ssg-adapters/gatsby/actions/restore.d.ts +3 -0
- package/build/ssg-adapters/gatsby/actions/ssg.d.ts +3 -0
- package/build/ssg-adapters/gatsby/actions/sync.d.ts +3 -0
- package/build/ssg-adapters/gatsby/index.d.ts +9 -0
- package/build/ssg-adapters/gatsby/shared/artifacts.d.ts +4 -0
- package/build/ssg-adapters/gatsby/shared/diff-assets.d.ts +15 -0
- package/build/ssg-adapters/gatsby/shared/extract-assets.d.ts +7 -0
- package/build/ssg-adapters/gatsby/shared/gatsby-build.d.ts +7 -0
- package/build/ssg-adapters/gatsby/shared/render-rollback.d.ts +18 -0
- package/build/ssg-adapters/gatsby/shared/sync-render.d.ts +26 -0
- package/build/ssg-adapters/gatsby/shared/types.d.ts +34 -0
- package/cli.mjs +231 -0
- package/exporter/build-esbuild.noop +42 -0
- package/exporter/build.sh +16 -24
- package/exporter/commands/README.md +142 -0
- package/exporter/commands/end-render.ts +53 -87
- package/exporter/commands/prepare-assets-directory.ts +35 -0
- package/exporter/commands/prepare-domains-render.ts +150 -33
- package/exporter/commands/reset-render.ts +13 -8
- package/exporter/commands/single-domain-upload-search-content.ts +206 -0
- package/exporter/commands/start-render.ts +14 -65
- package/exporter/commands/upload-search-content.ts +204 -26
- package/exporter/core/GriddoLog.ts +45 -0
- package/exporter/core/check-env-health.ts +203 -0
- package/exporter/core/db-class.ts +54 -0
- package/exporter/core/db.ts +33 -0
- package/exporter/core/dist-rollback.ts +49 -0
- package/exporter/core/errors.ts +93 -0
- package/exporter/core/fs.ts +385 -0
- package/exporter/{utils → core}/images.ts +1 -6
- package/exporter/{utils → core}/instance.ts +9 -13
- package/exporter/core/life-cycle.ts +73 -0
- package/exporter/core/logger.ts +137 -0
- package/exporter/core/objects.ts +37 -0
- package/exporter/core/print-logos.ts +21 -0
- package/exporter/index.ts +14 -56
- package/exporter/react/DynamicScript/index.tsx +33 -0
- package/exporter/react/{Favicon → GriddoFavicon}/index.tsx +3 -9
- package/exporter/react/GriddoIntegrations/index.tsx +17 -23
- package/exporter/react/GriddoIntegrations/utils.ts +24 -12
- package/exporter/react/GriddoOpenGraph/index.tsx +39 -0
- package/exporter/react/index.tsx +3 -9
- package/exporter/services/api.ts +306 -0
- package/exporter/services/auth.ts +8 -10
- package/exporter/services/domains.ts +23 -8
- package/exporter/services/manage-sites.ts +116 -0
- package/exporter/services/manage-store.ts +173 -0
- package/exporter/services/navigation.ts +12 -18
- package/exporter/{utils → services}/pages.ts +27 -92
- package/exporter/services/reference-fields.ts +14 -32
- package/exporter/services/render-artifacts.ts +44 -0
- package/exporter/services/render.ts +229 -0
- package/exporter/services/robots.ts +33 -61
- package/exporter/services/sitemaps.ts +129 -0
- package/exporter/services/sites.ts +40 -28
- package/exporter/services/store.ts +354 -321
- package/exporter/shared/context.ts +49 -0
- package/exporter/{constants → shared}/endpoints.ts +12 -11
- package/exporter/shared/envs.ts +58 -0
- package/exporter/{errors/errors-data.ts → shared/errors.ts} +24 -14
- package/exporter/shared/npm-modules/README.md +36 -0
- package/exporter/shared/npm-modules/brush.ts +34 -0
- package/exporter/shared/npm-modules/find-up-simple.ts +100 -0
- package/exporter/shared/npm-modules/pkg-dir.ts +17 -0
- package/exporter/shared/npm-modules/xml-parser.ts +57 -0
- package/exporter/{types → shared/types}/api.ts +40 -41
- package/exporter/{types → shared/types}/global.ts +17 -21
- package/exporter/{types → shared/types}/navigation.ts +3 -3
- package/exporter/{types → shared/types}/pages.ts +10 -11
- package/exporter/shared/types/render.ts +65 -0
- package/exporter/{types → shared/types}/sites.ts +18 -19
- package/exporter/shared/types.ts +15 -0
- package/exporter/ssg-adapters/gatsby/actions/clean.ts +26 -0
- package/exporter/ssg-adapters/gatsby/actions/close.ts +17 -0
- package/exporter/ssg-adapters/gatsby/actions/data.ts +22 -0
- package/exporter/ssg-adapters/gatsby/actions/healthCheck.ts +10 -0
- package/exporter/ssg-adapters/gatsby/actions/init.ts +12 -0
- package/exporter/ssg-adapters/gatsby/actions/logs.ts +10 -0
- package/exporter/ssg-adapters/gatsby/actions/meta.ts +13 -0
- package/exporter/ssg-adapters/gatsby/actions/prepare.ts +9 -0
- package/exporter/ssg-adapters/gatsby/actions/relocation.ts +15 -0
- package/exporter/ssg-adapters/gatsby/actions/restore.ts +21 -0
- package/exporter/ssg-adapters/gatsby/actions/ssg.ts +12 -0
- package/exporter/ssg-adapters/gatsby/actions/sync.ts +65 -0
- package/exporter/ssg-adapters/gatsby/index.ts +117 -0
- package/exporter/ssg-adapters/gatsby/shared/artifacts.ts +17 -0
- package/exporter/ssg-adapters/gatsby/shared/diff-assets.ts +128 -0
- package/exporter/ssg-adapters/gatsby/shared/extract-assets.ts +75 -0
- package/exporter/ssg-adapters/gatsby/shared/gatsby-build.ts +58 -0
- package/exporter/ssg-adapters/gatsby/shared/render-rollback.ts +33 -0
- package/exporter/ssg-adapters/gatsby/shared/sync-render.ts +298 -0
- package/exporter/ssg-adapters/gatsby/shared/types.ts +35 -0
- package/gatsby-browser.tsx +41 -58
- package/gatsby-config.ts +10 -17
- package/gatsby-node.ts +20 -79
- package/gatsby-ssr.tsx +2 -1
- package/package.json +41 -78
- package/plugins/gatsby-plugin-svgr-loader/gatsby-node.js +55 -0
- package/plugins/gatsby-plugin-svgr-loader/package.json +8 -0
- package/src/components/Head.tsx +28 -73
- package/src/components/template.tsx +6 -29
- package/src/gatsby-node-utils.ts +81 -2
- package/src/html.tsx +2 -11
- package/src/types.ts +3 -3
- package/tsconfig.commands.json +36 -0
- package/tsconfig.exporter.json +21 -0
- package/tsconfig.json +5 -3
- package/build/adapters/gatsby/index.d.ts +0 -4
- package/build/adapters/gatsby/utils.d.ts +0 -26
- package/build/artifacts/index.d.ts +0 -6
- package/build/constants/envs.d.ts +0 -37
- package/build/constants/index.d.ts +0 -57
- package/build/end-render.js +0 -74
- package/build/end-render.js.map +0 -7
- package/build/errors/index.d.ts +0 -15
- package/build/index.js.map +0 -7
- package/build/prepare-domains-render.js +0 -73
- package/build/prepare-domains-render.js.map +0 -7
- package/build/react/Favicon/index.d.ts +0 -5
- package/build/registers/api.d.ts +0 -9
- package/build/registers/gatsby.d.ts +0 -9
- package/build/registers/index.d.ts +0 -3
- package/build/reset-render.js +0 -74
- package/build/reset-render.js.map +0 -7
- package/build/services/register.d.ts +0 -36
- package/build/services/settings.d.ts +0 -4
- package/build/start-render.js +0 -100
- package/build/start-render.js.map +0 -7
- package/build/upload-search-content.js +0 -74
- package/build/upload-search-content.js.map +0 -7
- package/build/utils/alerts.d.ts +0 -3
- package/build/utils/cache.d.ts +0 -35
- package/build/utils/core-utils.d.ts +0 -107
- package/build/utils/create-build-data.d.ts +0 -8
- package/build/utils/domains.d.ts +0 -13
- package/build/utils/folders.d.ts +0 -53
- package/build/utils/loggin.d.ts +0 -51
- package/build/utils/render.d.ts +0 -13
- package/build/utils/searches.d.ts +0 -15
- package/build/utils/sites.d.ts +0 -31
- package/build/utils/store.d.ts +0 -81
- package/cx.config.d.ts +0 -5
- package/cx.config.js +0 -36
- package/exporter/adapters/gatsby/index.ts +0 -182
- package/exporter/adapters/gatsby/utils.ts +0 -186
- package/exporter/artifacts/README.md +0 -34
- package/exporter/artifacts/index.ts +0 -33
- package/exporter/commands/move-assets.ts +0 -11
- package/exporter/constants/envs.ts +0 -94
- package/exporter/constants/index.ts +0 -129
- package/exporter/errors/index.ts +0 -40
- package/exporter/registers/api.ts +0 -14
- package/exporter/registers/gatsby.ts +0 -14
- package/exporter/registers/index.ts +0 -4
- package/exporter/services/register.ts +0 -113
- package/exporter/services/settings.ts +0 -17
- package/exporter/utils/alerts.ts +0 -29
- package/exporter/utils/api.ts +0 -243
- package/exporter/utils/cache.ts +0 -142
- package/exporter/utils/core-utils.ts +0 -458
- package/exporter/utils/create-build-data.ts +0 -17
- package/exporter/utils/domains.ts +0 -39
- package/exporter/utils/folders.ts +0 -320
- package/exporter/utils/health-checks.ts +0 -64
- package/exporter/utils/loggin.ts +0 -184
- package/exporter/utils/render.ts +0 -71
- package/exporter/utils/searches.ts +0 -156
- package/exporter/utils/sites.ts +0 -312
- package/exporter/utils/store.ts +0 -314
- package/src/README.md +0 -7
- package/start-render.js +0 -7
- /package/build/commands/{move-assets.d.ts → prepare-assets-directory.d.ts} +0 -0
- /package/build/{utils → core}/images.d.ts +0 -0
- /package/build/{utils → core}/instance.d.ts +0 -0
- /package/build/react/{Favicon → GriddoFavicon}/utils.d.ts +0 -0
- /package/build/{constants → shared}/endpoints.d.ts +0 -0
- /package/build/{types → shared/types}/templates.d.ts +0 -0
- /package/exporter/react/{Favicon → GriddoFavicon}/utils.ts +0 -0
- /package/exporter/{types → shared/types}/templates.ts +0 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { ErrorsType } from "../shared/errors";
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { brush } from "../shared/npm-modules/brush";
|
|
6
|
+
import { RENDER_MODE } from "../shared/types/render";
|
|
7
|
+
import { readDB, writeDB } from "./db";
|
|
8
|
+
import { distRollback } from "./dist-rollback";
|
|
9
|
+
import { GriddoLog } from "./GriddoLog";
|
|
10
|
+
|
|
11
|
+
export type ErrorData = {
|
|
12
|
+
error: ErrorsType;
|
|
13
|
+
message: string;
|
|
14
|
+
expected?: string;
|
|
15
|
+
hint?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export class RenderError extends Error {
|
|
19
|
+
constructor(originalError?: unknown) {
|
|
20
|
+
super(originalError instanceof Error ? originalError.message : String(originalError));
|
|
21
|
+
|
|
22
|
+
this.name = "InternalCXError";
|
|
23
|
+
this.stack = originalError instanceof Error ? originalError.stack : "";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Throws an error with the provided error message, expected value, and hint.
|
|
29
|
+
*/
|
|
30
|
+
function throwError(options: ErrorData, stack?: unknown): never {
|
|
31
|
+
const { error, message, expected, hint } = options;
|
|
32
|
+
|
|
33
|
+
const errorColor = GriddoLog.log(brush.red(`[ ${error} ]`));
|
|
34
|
+
const extraText = [expected, hint].filter(Boolean).join("\n");
|
|
35
|
+
|
|
36
|
+
GriddoLog.log(`
|
|
37
|
+
${errorColor}
|
|
38
|
+
${message}
|
|
39
|
+
${extraText}
|
|
40
|
+
|
|
41
|
+
${brush.red("stack")}
|
|
42
|
+
${JSON.stringify(stack, null, 2)}`);
|
|
43
|
+
|
|
44
|
+
throw new RenderError(stack);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Executes the provided asynchronous function and handles errors that occur during execution.
|
|
49
|
+
*
|
|
50
|
+
* - If an error is thrown, attempts to log the error and, if necessary, rolls back the exports directory.
|
|
51
|
+
* - Updates the render database to set the domain's rendering state to "ERROR" if an error occurs.
|
|
52
|
+
*
|
|
53
|
+
* @param fn - The asynchronous function to execute within the error handler.
|
|
54
|
+
* @returns A promise that resolves when the function is executed successfully.
|
|
55
|
+
* @throws RenderError | unknown - Rethrows the original error after handling and database state update.
|
|
56
|
+
*/
|
|
57
|
+
async function withErrorHandler(fn: () => Promise<void>) {
|
|
58
|
+
try {
|
|
59
|
+
await fn();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (error instanceof RenderError) {
|
|
62
|
+
GriddoLog.error("Internal Griddo RenderError");
|
|
63
|
+
} else if (error instanceof Error) {
|
|
64
|
+
GriddoLog.error(error.message);
|
|
65
|
+
} else {
|
|
66
|
+
GriddoLog.error(`An unexpected error occurred ${error}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Try to rollback the exports directory if needed
|
|
70
|
+
try {
|
|
71
|
+
const data = await readDB();
|
|
72
|
+
const { root } = data.paths;
|
|
73
|
+
if (data.needsRollbackOnError) {
|
|
74
|
+
GriddoLog.info("Cleaning exports dir...");
|
|
75
|
+
GriddoLog.verbose(`Deleting ${path.join(root, "exports")}...`);
|
|
76
|
+
|
|
77
|
+
await distRollback(data.currentRenderingDomain!);
|
|
78
|
+
} else {
|
|
79
|
+
GriddoLog.info("No rollback needed, skipping...");
|
|
80
|
+
}
|
|
81
|
+
} catch (_e) {
|
|
82
|
+
GriddoLog.info("Early render stage, no db.json created yet...");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const data = await readDB();
|
|
86
|
+
data.domains[data.currentRenderingDomain!].isRendering = false;
|
|
87
|
+
data.domains[data.currentRenderingDomain!].renderMode = RENDER_MODE.ERROR;
|
|
88
|
+
await writeDB(data);
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { throwError, withErrorHandler };
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import type { MakeDirectoryOptions } from "node:fs";
|
|
2
|
+
|
|
3
|
+
import fsp from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { ArtifactError } from "../shared/errors";
|
|
7
|
+
import { throwError } from "./errors";
|
|
8
|
+
import { GriddoLog } from "./GriddoLog";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Remove an empty directory from the basePath recursively.
|
|
12
|
+
* If the directory has only .xml files it will handle as empty too (empty site)
|
|
13
|
+
*
|
|
14
|
+
* @param baseDir - The base directory.
|
|
15
|
+
*/
|
|
16
|
+
async function deleteDisposableSiteDirs(baseDir: string) {
|
|
17
|
+
if (!(await pathExists(baseDir))) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const sitesDirs = (await fsp.readdir(baseDir, { withFileTypes: true })).filter((file) =>
|
|
22
|
+
file.isDirectory(),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
for (const siteDir of sitesDirs) {
|
|
26
|
+
const sitePath = path.join(baseDir, siteDir.name);
|
|
27
|
+
if (await siteIsEmpty(sitePath)) {
|
|
28
|
+
await fsp.rm(sitePath, { recursive: true });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates multiple artifact directories.
|
|
35
|
+
*
|
|
36
|
+
* @param dirs - An array of directory paths.
|
|
37
|
+
* @param options - Same option as `fs.mkdirSync()`
|
|
38
|
+
*/
|
|
39
|
+
async function mkDirs(dirs: string[], options?: MakeDirectoryOptions) {
|
|
40
|
+
for (const dir of dirs) {
|
|
41
|
+
try {
|
|
42
|
+
if (!(await pathExists(dir))) {
|
|
43
|
+
await fsp.mkdir(dir, { recursive: true, ...options });
|
|
44
|
+
GriddoLog.verbose(`create directory: ${dir}`);
|
|
45
|
+
}
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throwError(ArtifactError, error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function renamePath(src: string, dst: string) {
|
|
53
|
+
try {
|
|
54
|
+
if (await pathExists(src)) {
|
|
55
|
+
await fsp.rename(src, dst);
|
|
56
|
+
GriddoLog.verbose(`rename ${src} to ${dst}`);
|
|
57
|
+
}
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throwError(ArtifactError, error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Copy multiple directories with backup option.
|
|
65
|
+
*
|
|
66
|
+
* @param src - Source directory.
|
|
67
|
+
* @param dst - Destination directory.
|
|
68
|
+
* @param dirs - Directories to copy.
|
|
69
|
+
* @param options.withBackup - Create a previous backup before copy.
|
|
70
|
+
*/
|
|
71
|
+
async function cpDirs(
|
|
72
|
+
src: string,
|
|
73
|
+
dst: string,
|
|
74
|
+
dirs: string[],
|
|
75
|
+
options = {
|
|
76
|
+
withBackup: false,
|
|
77
|
+
},
|
|
78
|
+
) {
|
|
79
|
+
const { withBackup } = options;
|
|
80
|
+
for (const dir of dirs) {
|
|
81
|
+
const srcCompose = path.join(src, dir);
|
|
82
|
+
const dstCompose = path.join(dst, dir);
|
|
83
|
+
|
|
84
|
+
// The dir we want to copy, doesn't exist.
|
|
85
|
+
if (!(await pathExists(srcCompose))) {
|
|
86
|
+
GriddoLog.info(`(Maybe first render) Source directory does not exist: ${srcCompose}`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Create the backup
|
|
91
|
+
if (withBackup) {
|
|
92
|
+
await createBackup(dstCompose);
|
|
93
|
+
GriddoLog.verbose(`create backup: ${dstCompose}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Copy directory
|
|
97
|
+
try {
|
|
98
|
+
// First clean destination
|
|
99
|
+
if (await pathExists(dstCompose)) {
|
|
100
|
+
await fsp.rm(dstCompose, { recursive: true, force: true });
|
|
101
|
+
GriddoLog.verbose(`clean destination: ${dstCompose}`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Then copy src to dst
|
|
105
|
+
await fsp.cp(srcCompose, dstCompose, {
|
|
106
|
+
recursive: true,
|
|
107
|
+
preserveTimestamps: true,
|
|
108
|
+
});
|
|
109
|
+
GriddoLog.verbose(`copy: ${srcCompose} to ${dstCompose}`);
|
|
110
|
+
|
|
111
|
+
if (withBackup) {
|
|
112
|
+
await deleteBackup(dstCompose);
|
|
113
|
+
GriddoLog.verbose(`delete backup: ${dstCompose}`);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (withBackup) {
|
|
117
|
+
await restoreBackup(dstCompose);
|
|
118
|
+
GriddoLog.verbose("Backup has been restored.");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
throwError(ArtifactError, error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Move artifacts between cx-paths
|
|
128
|
+
*
|
|
129
|
+
* @param src - Source directory.
|
|
130
|
+
* @param dst - Destination directory.
|
|
131
|
+
* @param dirs - Directories to move.
|
|
132
|
+
* @param options - Options.
|
|
133
|
+
*/
|
|
134
|
+
async function mvDirs(
|
|
135
|
+
src: string,
|
|
136
|
+
dst: string,
|
|
137
|
+
dirs: string[],
|
|
138
|
+
options?: { withBackup?: boolean; override?: boolean },
|
|
139
|
+
) {
|
|
140
|
+
const { override, withBackup } = options || {};
|
|
141
|
+
|
|
142
|
+
for (const dir of dirs) {
|
|
143
|
+
const srcCompose = path.join(src, dir);
|
|
144
|
+
const dstCompose = path.join(dst, dir);
|
|
145
|
+
|
|
146
|
+
if (!(await pathExists(srcCompose))) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (withBackup) {
|
|
151
|
+
await createBackup(dstCompose);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
// Clean destination
|
|
156
|
+
if (override && (await pathExists(dstCompose))) {
|
|
157
|
+
await fsp.rm(dstCompose, { recursive: true, force: true });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
await fsp.rename(srcCompose, dstCompose);
|
|
161
|
+
GriddoLog.verbose(`moved: ${srcCompose} to ${dstCompose}`);
|
|
162
|
+
|
|
163
|
+
if (withBackup) {
|
|
164
|
+
await deleteBackup(dstCompose);
|
|
165
|
+
}
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (withBackup) {
|
|
168
|
+
await restoreBackup(dstCompose);
|
|
169
|
+
GriddoLog.info("Backup has been restored.");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
throwError(ArtifactError, error);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Removes multiple artifact directories.
|
|
179
|
+
*
|
|
180
|
+
* @param dirs - An array of directory paths.
|
|
181
|
+
*/
|
|
182
|
+
async function rmDirs(dirs: string[]) {
|
|
183
|
+
for (const dir of dirs) {
|
|
184
|
+
try {
|
|
185
|
+
await fsp.rm(dir, { recursive: true, force: true });
|
|
186
|
+
GriddoLog.verbose(`artifact removed: ${dir}`);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
throwError(ArtifactError, error);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function restoreBackup(src: string, suffix = "-BACKUP") {
|
|
194
|
+
const dst = src + suffix;
|
|
195
|
+
try {
|
|
196
|
+
await fsp.rename(dst, src);
|
|
197
|
+
GriddoLog.info(`Backup ${dst} has been restored`);
|
|
198
|
+
} catch (_error) {
|
|
199
|
+
throw new Error(`Error while delete ${dst} backup`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function deleteBackup(src: string, suffix = "-BACKUP") {
|
|
204
|
+
const dst = src + suffix;
|
|
205
|
+
|
|
206
|
+
if (!(await pathExists(dst))) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
await fsp.rm(dst, { recursive: true, force: true });
|
|
212
|
+
GriddoLog.verbose(`Backup ${dst} has been deleted`);
|
|
213
|
+
} catch (_error) {
|
|
214
|
+
throw new Error(`Error while delete ${dst} backup`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function createBackup(src: string, suffix = "-BACKUP") {
|
|
219
|
+
const dst = src + suffix;
|
|
220
|
+
|
|
221
|
+
if (!(await pathExists(src))) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (await pathExists(dst)) {
|
|
226
|
+
GriddoLog.warn(`Destination ${dst} already exists`);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
await fsp.rename(src, dst);
|
|
232
|
+
GriddoLog.verbose(`Backup of ${src} has been created in ${dst}`);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
GriddoLog.error(`Error while coping ${src} to ${dst} backup`);
|
|
235
|
+
throwError(ArtifactError, error);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Return true if the site folder is empty or only has xml files. (Recursively)
|
|
241
|
+
*/
|
|
242
|
+
async function siteIsEmpty(sitePath: string) {
|
|
243
|
+
const siteFiles = (
|
|
244
|
+
await fsp.readdir(sitePath, {
|
|
245
|
+
withFileTypes: true,
|
|
246
|
+
recursive: true,
|
|
247
|
+
})
|
|
248
|
+
).filter((file) => file.isFile() && !path.basename(file.name).startsWith("."));
|
|
249
|
+
|
|
250
|
+
const xmlFiles = siteFiles.filter((file) => file.name.endsWith(".xml"));
|
|
251
|
+
|
|
252
|
+
if (siteFiles.length === xmlFiles.length) {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Delete empty directories from the given directory in a recursive way.
|
|
259
|
+
*/
|
|
260
|
+
async function deleteEmptyDirectories(dirPath: string) {
|
|
261
|
+
try {
|
|
262
|
+
const stats = await fsp.stat(dirPath);
|
|
263
|
+
|
|
264
|
+
// Si no es un directorio, no hacemos nada
|
|
265
|
+
if (!stats.isDirectory()) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let filesInDirectory: string[];
|
|
270
|
+
try {
|
|
271
|
+
filesInDirectory = await fsp.readdir(dirPath);
|
|
272
|
+
} catch (err: any) {
|
|
273
|
+
// Si el directorio no existe o no se puede leer (ej. permisos), lo saltamos.
|
|
274
|
+
if (err.code === "ENOENT") {
|
|
275
|
+
GriddoLog.warn(`The directory "${dirPath}" does not exist, skipping it.`);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
GriddoLog.error(`Error al leer el directorio "${dirPath}":`, err);
|
|
279
|
+
throw err; // Re-lanza el error para que sea manejado por el llamador
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Recorrer los contenidos del directorio
|
|
283
|
+
for (const file of filesInDirectory) {
|
|
284
|
+
const fullPath = path.join(dirPath, file);
|
|
285
|
+
await deleteEmptyDirectories(fullPath); // Llamada recursiva síncrona
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Después de procesar todos los subdirectorios, verifica si el directorio actual está vacío
|
|
289
|
+
const remainingFiles = await fsp.readdir(dirPath);
|
|
290
|
+
|
|
291
|
+
if (remainingFiles.length === 0) {
|
|
292
|
+
try {
|
|
293
|
+
await fsp.rmdir(dirPath);
|
|
294
|
+
GriddoLog.verbose(`Remove empty directory: ${dirPath}`);
|
|
295
|
+
} catch (err: any) {
|
|
296
|
+
// Puede que haya habido un problema de concurrencia o permisos
|
|
297
|
+
if (err.code === "ENOENT") {
|
|
298
|
+
GriddoLog.warn(
|
|
299
|
+
`El directorio "${dirPath}" ya no existe. Posiblemente fue borrado por otra operación.`,
|
|
300
|
+
);
|
|
301
|
+
} else {
|
|
302
|
+
GriddoLog.error(`Error al borrar el directorio "${dirPath}":`, err);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} catch (err: any) {
|
|
307
|
+
if (err.code === "ENOENT") {
|
|
308
|
+
// El directorio ya no existe, no es un error para nosotros en este contexto
|
|
309
|
+
GriddoLog.warn(`The directory "${dirPath}" does not exist or has already been processed.`);
|
|
310
|
+
} else {
|
|
311
|
+
GriddoLog.error(`General error general while processing "${dirPath}":`, err);
|
|
312
|
+
throw err;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function pathExists(dir: string) {
|
|
318
|
+
try {
|
|
319
|
+
await fsp.access(dir);
|
|
320
|
+
return true;
|
|
321
|
+
} catch {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Busca recursivamente archivos que terminen con un sufijo específico dentro de un directorio.
|
|
328
|
+
* Esta función es un generador asíncrono, lo que la hace muy eficiente en uso de memoria.
|
|
329
|
+
*
|
|
330
|
+
* @param dir El directorio base para comenzar la búsqueda.
|
|
331
|
+
* @param suffix El sufijo con el que deben terminar los nombres de archivo (ej: 'page-data.json').
|
|
332
|
+
* @returns Un generador asíncrono que produce la ruta completa de cada archivo encontrado.
|
|
333
|
+
* @throws Si el directorio inicial `dir` no existe o no se puede leer.
|
|
334
|
+
*/
|
|
335
|
+
async function* findFilesBySuffix(dir: string, suffix: string): AsyncGenerator<string> {
|
|
336
|
+
const dirHandle = await fsp.opendir(dir);
|
|
337
|
+
for await (const item of dirHandle) {
|
|
338
|
+
const fullPath = path.join(dir, item.name);
|
|
339
|
+
if (item.isDirectory()) {
|
|
340
|
+
// yield* para encadenar otro generator.
|
|
341
|
+
yield* findFilesBySuffix(fullPath, suffix);
|
|
342
|
+
} else if (item.isFile() && item.name.endsWith(suffix)) {
|
|
343
|
+
yield fullPath;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Walk a directory and returns the JSON file absolute paths with one level of depth.
|
|
350
|
+
* /abs/.../sotre/331/158.json
|
|
351
|
+
* /abs/.../sotre/114/443.json
|
|
352
|
+
* /abs/.../sotre/131/217.json
|
|
353
|
+
* /abs/.../sotre/191/281.json
|
|
354
|
+
*/
|
|
355
|
+
async function* walkStore(storeDir: string): AsyncGenerator<string> {
|
|
356
|
+
const storeDirHandle = await fsp.opendir(storeDir);
|
|
357
|
+
|
|
358
|
+
for await (const siteDirent of storeDirHandle) {
|
|
359
|
+
if (siteDirent.isDirectory()) {
|
|
360
|
+
const siteDirPath = path.join(storeDir, siteDirent.name);
|
|
361
|
+
const siteDirHandle = await fsp.opendir(siteDirPath);
|
|
362
|
+
|
|
363
|
+
for await (const fileDirent of siteDirHandle) {
|
|
364
|
+
const filePath = path.join(siteDirPath, fileDirent.name);
|
|
365
|
+
|
|
366
|
+
if (fileDirent.isFile() && path.extname(filePath) === ".json") {
|
|
367
|
+
yield filePath;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export {
|
|
375
|
+
cpDirs,
|
|
376
|
+
deleteDisposableSiteDirs,
|
|
377
|
+
deleteEmptyDirectories,
|
|
378
|
+
findFilesBySuffix,
|
|
379
|
+
mkDirs,
|
|
380
|
+
mvDirs,
|
|
381
|
+
pathExists,
|
|
382
|
+
renamePath,
|
|
383
|
+
rmDirs,
|
|
384
|
+
walkStore,
|
|
385
|
+
};
|
|
@@ -13,12 +13,7 @@ import type { Fields } from "@griddo/core";
|
|
|
13
13
|
* formatImage("https://images.griddo.io/tree", 300, 200, "jpg")
|
|
14
14
|
* // https://images.griddo.io/tree/w/300/h/200/f/jpg
|
|
15
15
|
*/
|
|
16
|
-
function formatImage(
|
|
17
|
-
image: Fields.Image | string,
|
|
18
|
-
width: number,
|
|
19
|
-
height: number,
|
|
20
|
-
format = "jpg",
|
|
21
|
-
) {
|
|
16
|
+
function formatImage(image: Fields.Image | string, width: number, height: number, format = "jpg") {
|
|
22
17
|
const url = typeof image === "string" ? image : image?.url;
|
|
23
18
|
|
|
24
19
|
if (!url) {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
import { throwError } from "
|
|
7
|
-
import { NoJSConfigFileFound } from "../errors/errors-data";
|
|
3
|
+
import { NoJSConfigFileFound } from "../shared/errors";
|
|
4
|
+
import { findUpSync } from "../shared/npm-modules/find-up-simple";
|
|
5
|
+
import { pkgDirSync } from "../shared/npm-modules/pkg-dir";
|
|
6
|
+
import { throwError } from "./errors";
|
|
8
7
|
|
|
9
8
|
// Consts
|
|
10
9
|
const isComponentLibrary = __dirname.includes("node_modules");
|
|
@@ -17,22 +16,18 @@ const componentLibraryPathAlias = getComponentsLibAliases();
|
|
|
17
16
|
*/
|
|
18
17
|
function resolveComponentsPath(customPath = "") {
|
|
19
18
|
return isComponentLibrary
|
|
20
|
-
? path.resolve(
|
|
21
|
-
: path.resolve(
|
|
22
|
-
pkgDir.sync(__dirname) as string,
|
|
23
|
-
"../griddo-components",
|
|
24
|
-
customPath,
|
|
25
|
-
);
|
|
19
|
+
? path.resolve(pkgDirSync({ cwd: __dirname }) as string, "../../../", customPath)
|
|
20
|
+
: path.resolve(pkgDirSync({ cwd: __dirname }) as string, "../griddo-components", customPath);
|
|
26
21
|
}
|
|
27
22
|
|
|
28
23
|
/**
|
|
29
24
|
* Return the instance or monorepo components {t|j}sconfig.json file.
|
|
30
25
|
*/
|
|
31
26
|
function getComponentsJSConfig() {
|
|
32
|
-
const jsConfigPath =
|
|
27
|
+
const jsConfigPath = findUpSync("jsconfig.json", {
|
|
33
28
|
cwd: resolveComponentsPath(),
|
|
34
29
|
});
|
|
35
|
-
const tsConfigPath =
|
|
30
|
+
const tsConfigPath = findUpSync("tsconfig.json", {
|
|
36
31
|
cwd: resolveComponentsPath(),
|
|
37
32
|
});
|
|
38
33
|
|
|
@@ -57,6 +52,7 @@ function getComponentsLibAliases() {
|
|
|
57
52
|
const absolutePath = resolveComponentsPath(relativePathToDir);
|
|
58
53
|
|
|
59
54
|
return {
|
|
55
|
+
// biome-ignore lint/performance/noAccumulatingSpread: to be fixed
|
|
60
56
|
...currentAlias,
|
|
61
57
|
[aliasKey]: absolutePath,
|
|
62
58
|
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { LifeCycleAction } from "../shared/types/global";
|
|
2
|
+
|
|
3
|
+
import { LifecycleExecutionError } from "../shared/errors";
|
|
4
|
+
import { brush } from "../shared/npm-modules/brush";
|
|
5
|
+
import { throwError } from "./errors";
|
|
6
|
+
import { GriddoLog } from "./GriddoLog";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Measures the execution time of a series of sync or async functions.
|
|
10
|
+
*
|
|
11
|
+
* @async
|
|
12
|
+
* @param functions - Functions to be executed to measure their execution time.
|
|
13
|
+
* @returns A promise that resolves with the total execution time in seconds.
|
|
14
|
+
*/
|
|
15
|
+
async function executeAndReturnExecutionTime<T>(func: () => T | Promise<T>): Promise<number> {
|
|
16
|
+
const start = process.hrtime.bigint();
|
|
17
|
+
await Promise.resolve(func());
|
|
18
|
+
const end = process.hrtime.bigint();
|
|
19
|
+
return Number((Number(end - start) / 1e9).toFixed(3));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Executes a life cycle process, which involves executing a callback
|
|
24
|
+
* function, printing to the console, and handling errors with optional retries.
|
|
25
|
+
*
|
|
26
|
+
* @async
|
|
27
|
+
* @param name - The name of the life cycle.
|
|
28
|
+
* @param action - A callback function to execute.
|
|
29
|
+
* @param options - Optional configuration for the life cycle process.
|
|
30
|
+
* @param options.maxRetrys - The maximum number of retries for the life cycle.
|
|
31
|
+
* @param options.enabled - Whether the life cycle process should be executed.
|
|
32
|
+
* @returns - A promise that resolves when the life cycle process is completed.
|
|
33
|
+
*/
|
|
34
|
+
async function doLifeCycle(
|
|
35
|
+
name: string,
|
|
36
|
+
action: LifeCycleAction,
|
|
37
|
+
options?: {
|
|
38
|
+
maxRetrys?: number;
|
|
39
|
+
skip?: undefined | false | string;
|
|
40
|
+
},
|
|
41
|
+
) {
|
|
42
|
+
const { maxRetrys = 3, skip } = options || {};
|
|
43
|
+
|
|
44
|
+
if (skip) {
|
|
45
|
+
GriddoLog.info(`Skiping life-cycle ${name}. Reason: ${skip}\n`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let trysCount = 0;
|
|
50
|
+
|
|
51
|
+
while (trysCount < maxRetrys) {
|
|
52
|
+
try {
|
|
53
|
+
GriddoLog.info(`start ${name} life-cycle`);
|
|
54
|
+
const exeTime = await executeAndReturnExecutionTime(action);
|
|
55
|
+
GriddoLog.success(`${name} - ${exeTime}s\n`);
|
|
56
|
+
break;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
const errorString = brush.red(` Error in ${name} LifeCycle `);
|
|
59
|
+
const attemptsString = brush.yellow(`Attempt (${trysCount + 1})`);
|
|
60
|
+
GriddoLog.log(`\n${errorString} ${attemptsString}\n`);
|
|
61
|
+
GriddoLog.log(error);
|
|
62
|
+
GriddoLog.log();
|
|
63
|
+
|
|
64
|
+
trysCount++;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (trysCount === maxRetrys) {
|
|
69
|
+
throwError(LifecycleExecutionError(maxRetrys, name));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { doLifeCycle, executeAndReturnExecutionTime };
|