@griddo/cx 11.9.9-rc.0 → 11.9.9-rc.2
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 +240 -13
- package/build/adapters/gatsby/index.d.ts +4 -0
- package/build/adapters/gatsby/utils.d.ts +22 -0
- package/build/artifacts/index.d.ts +6 -0
- package/build/commands/end-render.d.ts +1 -0
- package/build/commands/reset-render.d.ts +1 -0
- package/build/commands/start-render.d.ts +1 -0
- package/build/commands/upload-search-content.d.ts +1 -0
- package/build/constants/envs.d.ts +37 -0
- package/build/constants/index.d.ts +57 -0
- package/build/end-render.js +74 -0
- package/build/end-render.js.map +7 -0
- package/build/{shared/errors.d.ts → errors/errors-data.d.ts} +3 -5
- package/build/errors/index.d.ts +15 -0
- package/build/index.d.ts +29 -10
- package/build/index.js +73 -406
- package/build/index.js.map +7 -0
- package/build/prepare-domains-render.js +73 -0
- package/build/prepare-domains-render.js.map +7 -0
- package/build/react/Favicon/index.d.ts +5 -0
- package/build/react/GriddoIntegrations/index.d.ts +4 -3
- package/build/react/GriddoIntegrations/utils.d.ts +6 -7
- package/build/react/index.d.ts +2 -3
- package/build/react/index.js +3 -1
- package/build/registers/api.d.ts +9 -0
- package/build/registers/gatsby.d.ts +9 -0
- package/build/registers/index.d.ts +3 -0
- package/build/reset-render.js +74 -0
- package/build/reset-render.js.map +7 -0
- package/build/services/auth.d.ts +5 -2
- package/build/services/domains.d.ts +4 -3
- package/build/services/navigation.d.ts +16 -16
- package/build/services/reference-fields.d.ts +3 -3
- package/build/services/register.d.ts +36 -0
- package/build/services/robots.d.ts +19 -2
- package/build/services/settings.d.ts +4 -0
- package/build/services/sites.d.ts +5 -8
- package/build/services/store.d.ts +1 -10
- package/build/start-render.js +100 -0
- package/build/start-render.js.map +7 -0
- package/build/{shared/types → types}/api.d.ts +18 -18
- package/build/{shared/types → types}/global.d.ts +16 -15
- package/build/{shared/types → types}/navigation.d.ts +5 -5
- package/build/{shared/types → types}/pages.d.ts +9 -9
- package/build/{shared/types → types}/sites.d.ts +19 -18
- package/build/upload-search-content.js +74 -0
- package/build/upload-search-content.js.map +7 -0
- package/build/utils/alerts.d.ts +3 -0
- package/build/{services → utils}/api.d.ts +1 -1
- package/build/utils/cache.d.ts +35 -0
- package/build/utils/core-utils.d.ts +107 -0
- package/build/utils/create-build-data.d.ts +8 -0
- package/build/utils/domains.d.ts +13 -0
- package/build/utils/folders.d.ts +53 -0
- package/build/{core/check-env-health.d.ts → utils/health-checks.d.ts} +2 -4
- package/build/utils/loggin.d.ts +51 -0
- package/build/{services → utils}/pages.d.ts +3 -3
- package/build/utils/render.d.ts +13 -0
- package/build/utils/searches.d.ts +15 -0
- package/build/utils/sites.d.ts +31 -0
- package/build/utils/store.d.ts +81 -0
- package/cx.config.d.ts +5 -0
- package/cx.config.js +36 -0
- package/exporter/adapters/gatsby/index.ts +162 -0
- package/exporter/adapters/gatsby/utils.ts +161 -0
- package/exporter/artifacts/README.md +34 -0
- package/exporter/artifacts/index.ts +33 -0
- package/exporter/build.sh +24 -16
- package/exporter/commands/end-render.ts +86 -65
- package/exporter/commands/move-assets.ts +11 -0
- package/exporter/commands/prepare-domains-render.ts +35 -147
- package/exporter/commands/reset-render.ts +8 -13
- package/exporter/commands/start-render.ts +64 -26
- package/exporter/commands/upload-search-content.ts +26 -201
- package/exporter/{shared → constants}/endpoints.ts +11 -12
- package/exporter/constants/envs.ts +94 -0
- package/exporter/constants/index.ts +129 -0
- package/exporter/{shared/errors.ts → errors/errors-data.ts} +14 -24
- package/exporter/errors/index.ts +40 -0
- package/exporter/index.ts +56 -14
- package/exporter/react/{GriddoFavicon → Favicon}/index.tsx +9 -3
- package/exporter/react/GriddoIntegrations/index.tsx +23 -17
- package/exporter/react/GriddoIntegrations/utils.ts +12 -24
- package/exporter/react/index.tsx +9 -3
- package/exporter/registers/api.ts +14 -0
- package/exporter/registers/gatsby.ts +14 -0
- package/exporter/registers/index.ts +4 -0
- package/exporter/services/auth.ts +10 -8
- package/exporter/services/domains.ts +8 -23
- package/exporter/services/navigation.ts +18 -12
- package/exporter/services/reference-fields.ts +32 -14
- package/exporter/services/register.ts +113 -0
- package/exporter/services/robots.ts +61 -33
- package/exporter/services/settings.ts +17 -0
- package/exporter/services/sites.ts +28 -40
- package/exporter/services/store.ts +321 -354
- package/exporter/{shared/types → types}/api.ts +41 -40
- package/exporter/{shared/types → types}/global.ts +21 -17
- package/exporter/{shared/types → types}/navigation.ts +3 -3
- package/exporter/{shared/types → types}/pages.ts +11 -10
- package/exporter/{shared/types → types}/sites.ts +19 -18
- package/exporter/utils/alerts.ts +29 -0
- package/exporter/utils/api.ts +243 -0
- package/exporter/utils/cache.ts +142 -0
- package/exporter/utils/core-utils.ts +458 -0
- package/exporter/utils/create-build-data.ts +17 -0
- package/exporter/utils/domains.ts +39 -0
- package/exporter/utils/folders.ts +320 -0
- package/exporter/utils/health-checks.ts +64 -0
- package/exporter/{core → utils}/images.ts +6 -1
- package/exporter/{core → utils}/instance.ts +13 -9
- package/exporter/utils/loggin.ts +184 -0
- package/exporter/{services → utils}/pages.ts +92 -27
- package/exporter/utils/render.ts +71 -0
- package/exporter/utils/searches.ts +156 -0
- package/exporter/utils/sites.ts +312 -0
- package/exporter/utils/store.ts +314 -0
- package/gatsby-browser.tsx +58 -41
- package/gatsby-config.ts +17 -10
- package/gatsby-node.ts +79 -20
- package/gatsby-ssr.tsx +1 -2
- package/package.json +80 -41
- package/src/README.md +7 -0
- package/src/components/Head.tsx +73 -28
- package/src/components/template.tsx +29 -6
- package/src/gatsby-node-utils.ts +2 -76
- package/src/html.tsx +11 -2
- package/src/types.ts +3 -3
- package/start-render.js +7 -0
- package/tsconfig.json +3 -5
- package/build/commands/end-render.js +0 -31
- package/build/commands/end-render.js.map +0 -7
- package/build/commands/prepare-assets-directory.js +0 -9
- package/build/commands/prepare-assets-directory.js.map +0 -7
- package/build/commands/prepare-domains-render.js +0 -38
- package/build/commands/prepare-domains-render.js.map +0 -7
- package/build/commands/reset-render.js +0 -31
- package/build/commands/reset-render.js.map +0 -7
- package/build/commands/single-domain-upload-search-content.d.ts +0 -1
- package/build/commands/start-render.js +0 -66
- package/build/commands/start-render.js.map +0 -7
- package/build/commands/upload-search-content.js +0 -31
- package/build/commands/upload-search-content.js.map +0 -7
- package/build/core/GriddoLog.d.ts +0 -16
- package/build/core/db-class.d.ts +0 -11
- package/build/core/db.d.ts +0 -4
- package/build/core/dist-rollback.d.ts +0 -11
- package/build/core/errors.d.ts +0 -26
- package/build/core/fs.d.ts +0 -69
- package/build/core/life-cycle.d.ts +0 -26
- package/build/core/logger.d.ts +0 -18
- package/build/core/objects.d.ts +0 -11
- package/build/core/print-logos.d.ts +0 -5
- package/build/react/DynamicScript/index.d.ts +0 -4
- package/build/react/GriddoFavicon/index.d.ts +0 -4
- package/build/react/GriddoOpenGraph/index.d.ts +0 -10
- package/build/services/manage-sites.d.ts +0 -22
- package/build/services/manage-store.d.ts +0 -32
- package/build/services/render-artifacts.d.ts +0 -6
- package/build/services/render.d.ts +0 -70
- package/build/services/sitemaps.d.ts +0 -5
- package/build/shared/context.d.ts +0 -36
- package/build/shared/envs.d.ts +0 -19
- package/build/shared/npm-modules/brush.d.ts +0 -18
- package/build/shared/npm-modules/find-up-simple.d.ts +0 -34
- package/build/shared/npm-modules/pkg-dir.d.ts +0 -7
- package/build/shared/npm-modules/xml-parser.d.ts +0 -4
- package/build/shared/types/render.d.ts +0 -54
- package/build/shared/types.d.ts +0 -15
- package/build/ssg-adapters/gatsby/actions/clean.d.ts +0 -3
- package/build/ssg-adapters/gatsby/actions/close.d.ts +0 -3
- package/build/ssg-adapters/gatsby/actions/data.d.ts +0 -2
- package/build/ssg-adapters/gatsby/actions/healthCheck.d.ts +0 -2
- package/build/ssg-adapters/gatsby/actions/init.d.ts +0 -2
- package/build/ssg-adapters/gatsby/actions/logs.d.ts +0 -3
- package/build/ssg-adapters/gatsby/actions/meta.d.ts +0 -2
- package/build/ssg-adapters/gatsby/actions/prepare.d.ts +0 -2
- package/build/ssg-adapters/gatsby/actions/relocation.d.ts +0 -2
- package/build/ssg-adapters/gatsby/actions/restore.d.ts +0 -3
- package/build/ssg-adapters/gatsby/actions/ssg.d.ts +0 -3
- package/build/ssg-adapters/gatsby/actions/sync.d.ts +0 -3
- package/build/ssg-adapters/gatsby/index.d.ts +0 -9
- package/build/ssg-adapters/gatsby/shared/artifacts.d.ts +0 -4
- package/build/ssg-adapters/gatsby/shared/diff-assets.d.ts +0 -15
- package/build/ssg-adapters/gatsby/shared/extract-assets.d.ts +0 -7
- package/build/ssg-adapters/gatsby/shared/gatsby-build.d.ts +0 -7
- package/build/ssg-adapters/gatsby/shared/render-rollback.d.ts +0 -18
- package/build/ssg-adapters/gatsby/shared/sync-render.d.ts +0 -26
- package/build/ssg-adapters/gatsby/shared/types.d.ts +0 -34
- package/cli.mjs +0 -231
- package/exporter/build-esbuild.noop +0 -42
- package/exporter/commands/README.md +0 -151
- package/exporter/commands/prepare-assets-directory.ts +0 -34
- package/exporter/commands/single-domain-upload-search-content.ts +0 -206
- package/exporter/core/GriddoLog.ts +0 -45
- package/exporter/core/check-env-health.ts +0 -204
- package/exporter/core/db-class.ts +0 -54
- package/exporter/core/db.ts +0 -33
- package/exporter/core/dist-rollback.ts +0 -49
- package/exporter/core/errors.ts +0 -92
- package/exporter/core/fs.ts +0 -385
- package/exporter/core/life-cycle.ts +0 -73
- package/exporter/core/logger.ts +0 -141
- package/exporter/core/objects.ts +0 -37
- package/exporter/core/print-logos.ts +0 -21
- package/exporter/react/DynamicScript/index.tsx +0 -33
- package/exporter/react/GriddoOpenGraph/index.tsx +0 -39
- package/exporter/services/api.ts +0 -306
- package/exporter/services/manage-sites.ts +0 -116
- package/exporter/services/manage-store.ts +0 -173
- package/exporter/services/render-artifacts.ts +0 -44
- package/exporter/services/render.ts +0 -229
- package/exporter/services/sitemaps.ts +0 -129
- package/exporter/shared/context.ts +0 -49
- package/exporter/shared/envs.ts +0 -62
- package/exporter/shared/npm-modules/README.md +0 -36
- package/exporter/shared/npm-modules/brush.ts +0 -34
- package/exporter/shared/npm-modules/find-up-simple.ts +0 -100
- package/exporter/shared/npm-modules/pkg-dir.ts +0 -17
- package/exporter/shared/npm-modules/xml-parser.ts +0 -57
- package/exporter/shared/types/render.ts +0 -63
- package/exporter/shared/types.ts +0 -15
- package/exporter/ssg-adapters/gatsby/actions/clean.ts +0 -26
- package/exporter/ssg-adapters/gatsby/actions/close.ts +0 -17
- package/exporter/ssg-adapters/gatsby/actions/data.ts +0 -22
- package/exporter/ssg-adapters/gatsby/actions/healthCheck.ts +0 -10
- package/exporter/ssg-adapters/gatsby/actions/init.ts +0 -12
- package/exporter/ssg-adapters/gatsby/actions/logs.ts +0 -10
- package/exporter/ssg-adapters/gatsby/actions/meta.ts +0 -13
- package/exporter/ssg-adapters/gatsby/actions/prepare.ts +0 -9
- package/exporter/ssg-adapters/gatsby/actions/relocation.ts +0 -15
- package/exporter/ssg-adapters/gatsby/actions/restore.ts +0 -21
- package/exporter/ssg-adapters/gatsby/actions/ssg.ts +0 -12
- package/exporter/ssg-adapters/gatsby/actions/sync.ts +0 -65
- package/exporter/ssg-adapters/gatsby/index.ts +0 -114
- package/exporter/ssg-adapters/gatsby/shared/artifacts.ts +0 -17
- package/exporter/ssg-adapters/gatsby/shared/diff-assets.ts +0 -128
- package/exporter/ssg-adapters/gatsby/shared/extract-assets.ts +0 -75
- package/exporter/ssg-adapters/gatsby/shared/gatsby-build.ts +0 -58
- package/exporter/ssg-adapters/gatsby/shared/render-rollback.ts +0 -33
- package/exporter/ssg-adapters/gatsby/shared/sync-render.ts +0 -298
- package/exporter/ssg-adapters/gatsby/shared/types.ts +0 -35
- package/plugins/gatsby-plugin-svgr-loader/gatsby-node.js +0 -55
- package/plugins/gatsby-plugin-svgr-loader/package.json +0 -8
- package/tsconfig.commands.json +0 -36
- package/tsconfig.exporter.json +0 -21
- /package/build/commands/{prepare-assets-directory.d.ts → move-assets.d.ts} +0 -0
- /package/build/{shared → constants}/endpoints.d.ts +0 -0
- /package/build/react/{GriddoFavicon → Favicon}/utils.d.ts +0 -0
- /package/build/{shared/types → types}/templates.d.ts +0 -0
- /package/build/{core → utils}/images.d.ts +0 -0
- /package/build/{core → utils}/instance.d.ts +0 -0
- /package/exporter/react/{GriddoFavicon → Favicon}/utils.ts +0 -0
- /package/exporter/{shared/types → types}/templates.ts +0 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import type { Site, SiteData } from "../types/sites";
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import { parse } from "js2xmlparser";
|
|
7
|
+
|
|
8
|
+
import { envs } from "../constants";
|
|
9
|
+
import { getConfig } from "./core-utils";
|
|
10
|
+
import { buildLog, verboseLog } from "./loggin";
|
|
11
|
+
import { getBuildMetadata } from "./store";
|
|
12
|
+
import { AuthService } from "../services/auth";
|
|
13
|
+
import {
|
|
14
|
+
endSiteRender,
|
|
15
|
+
getAllSites,
|
|
16
|
+
getSiteInfo,
|
|
17
|
+
getSiteLanguages,
|
|
18
|
+
getSitemap,
|
|
19
|
+
getSiteSocials,
|
|
20
|
+
startSiteRender,
|
|
21
|
+
} from "../services/sites";
|
|
22
|
+
|
|
23
|
+
const config = getConfig();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check the instance sites and returns site prepared to be published and unpublished.
|
|
27
|
+
*/
|
|
28
|
+
async function getSitesToRender(domain: string) {
|
|
29
|
+
console.info(`API URL ${envs.GRIDDO_API_URL as string}`);
|
|
30
|
+
|
|
31
|
+
// Login to API
|
|
32
|
+
await AuthService.login();
|
|
33
|
+
|
|
34
|
+
// Get all sites. An array of Site
|
|
35
|
+
const allSites = await getAllSites(domain);
|
|
36
|
+
|
|
37
|
+
// Filter the array of sites to get only the ones to build/render
|
|
38
|
+
const validSites = envs.GRIDDO_RENDER_ALL_SITES
|
|
39
|
+
? allSites.filter(
|
|
40
|
+
(site) =>
|
|
41
|
+
!envs.GRIDDO_RENDER_SITE || site.id === envs.GRIDDO_RENDER_SITE,
|
|
42
|
+
)
|
|
43
|
+
: allSites.filter((site) =>
|
|
44
|
+
envs.GRIDDO_RENDER_SITE
|
|
45
|
+
? site.id === envs.GRIDDO_RENDER_SITE
|
|
46
|
+
: !!site.shouldBeUpdated,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// If there are valid sites...
|
|
50
|
+
// En este paso se añade al objeto `Site` la información de los dominios
|
|
51
|
+
// utilizando los idiomas.
|
|
52
|
+
if (validSites.length) {
|
|
53
|
+
for (const site of validSites) {
|
|
54
|
+
const { items } = await getSiteLanguages(site.id);
|
|
55
|
+
|
|
56
|
+
// Añadimos la prop domains con el dominio "cocinado" con los
|
|
57
|
+
// idiomas y teniendo en cuenta solo el dominio actual.
|
|
58
|
+
site.domains = items
|
|
59
|
+
.filter(
|
|
60
|
+
(item) =>
|
|
61
|
+
item.domain &&
|
|
62
|
+
(item.domain.slug === domain || item.domain.slug === `/${domain}`),
|
|
63
|
+
)
|
|
64
|
+
.map((item) => ({ [item.id]: `${item.domain.slug}${item.path}` }));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Save sites object to publish
|
|
69
|
+
const sitesToPublish = validSites.filter((site) =>
|
|
70
|
+
envs.GRIDDO_RENDER_SITE
|
|
71
|
+
? site.id === envs.GRIDDO_RENDER_SITE
|
|
72
|
+
: !!site.isPublished,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Save sites object to unpublish
|
|
76
|
+
const sitesToUnpublish = validSites.filter(
|
|
77
|
+
(site) => !site.isPublished && site.shouldBeUpdated,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
sitesToPublish,
|
|
82
|
+
sitesToUnpublish,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Unpublish an array of sites sending the information to the API.
|
|
88
|
+
*
|
|
89
|
+
* @param sites An array of sites
|
|
90
|
+
* @see https://griddoio.notion.site/Sites-d7bb0b7cb8d24894a5337e1139fc3d09#2019d3255bda4d219c7e19cf28d0c4fe
|
|
91
|
+
*/
|
|
92
|
+
async function unpublishSites(sites: Array<Site>) {
|
|
93
|
+
const { __cx } = config.paths();
|
|
94
|
+
|
|
95
|
+
for (const site of sites) {
|
|
96
|
+
// API
|
|
97
|
+
const buildInfo = await startSiteRender(site.id);
|
|
98
|
+
const { siteHash } = buildInfo;
|
|
99
|
+
const body = {
|
|
100
|
+
siteHash,
|
|
101
|
+
publishHashes: [],
|
|
102
|
+
unpublishHashes: [],
|
|
103
|
+
publishPagesIds: [],
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
await endSiteRender(site.id, body);
|
|
107
|
+
|
|
108
|
+
// STORE
|
|
109
|
+
// Remove site directory from the Store
|
|
110
|
+
fs.rmSync(path.join(__cx, "store", site.id.toString()), {
|
|
111
|
+
force: true,
|
|
112
|
+
recursive: true,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Return a single site generic data.
|
|
119
|
+
*
|
|
120
|
+
* @param siteID The site id.
|
|
121
|
+
* @see SiteData
|
|
122
|
+
*/
|
|
123
|
+
async function getSiteData(siteID: number) {
|
|
124
|
+
const buildData = await startSiteRender(siteID);
|
|
125
|
+
const siteInfo = await getSiteInfo(siteID);
|
|
126
|
+
const siteLangs = await getSiteLanguages(siteID);
|
|
127
|
+
const socials = await getSiteSocials(siteID);
|
|
128
|
+
const siteLangsInfo = siteLangs.items;
|
|
129
|
+
const defaultLang = siteLangsInfo.find((lang) => lang.isDefault);
|
|
130
|
+
|
|
131
|
+
const { siteHash, unpublishHashes, publishIds } = buildData;
|
|
132
|
+
const { headers, footers } = siteInfo;
|
|
133
|
+
const validPagesIds = envs.GRIDDO_RENDER_PAGES.length
|
|
134
|
+
? envs.GRIDDO_RENDER_PAGES.filter((item) => publishIds.includes(item))
|
|
135
|
+
: publishIds;
|
|
136
|
+
|
|
137
|
+
const siteData: SiteData = {
|
|
138
|
+
siteInfo,
|
|
139
|
+
validPagesIds,
|
|
140
|
+
siteHash,
|
|
141
|
+
unpublishHashes,
|
|
142
|
+
siteLangs: siteLangsInfo,
|
|
143
|
+
defaultLang,
|
|
144
|
+
headers,
|
|
145
|
+
footers,
|
|
146
|
+
socials,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return siteData;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Save a file with the end of build process to use as `build-end` signal.
|
|
154
|
+
*/
|
|
155
|
+
async function generateBuildReport() {
|
|
156
|
+
const { __cx } = config.paths();
|
|
157
|
+
const { buildReportFileName } = config;
|
|
158
|
+
|
|
159
|
+
const { buildProcessData } = await getBuildMetadata();
|
|
160
|
+
|
|
161
|
+
// Get the token
|
|
162
|
+
const authControl = await AuthService.login();
|
|
163
|
+
|
|
164
|
+
const buildSitesInfo = Object.keys(buildProcessData).map((siteID) => ({
|
|
165
|
+
...buildProcessData[siteID],
|
|
166
|
+
siteId: Number.parseInt(siteID),
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
const report = {
|
|
170
|
+
authControl,
|
|
171
|
+
sites: buildSitesInfo,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const reportFilePath = path.join(__cx, "dist", buildReportFileName);
|
|
175
|
+
fs.writeFileSync(reportFilePath, JSON.stringify(report));
|
|
176
|
+
|
|
177
|
+
verboseLog(`build report saved in ${reportFilePath}`);
|
|
178
|
+
|
|
179
|
+
buildLog(`Build report saved for ${buildSitesInfo.length} site(s)`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Generate sitemaps and save them into file system.
|
|
184
|
+
*/
|
|
185
|
+
async function generateSitemaps() {
|
|
186
|
+
const { sitesToPublish } = await getBuildMetadata();
|
|
187
|
+
const { __cx } = config.paths();
|
|
188
|
+
const distDir = path.join(__cx, "dist");
|
|
189
|
+
|
|
190
|
+
const templateSitemapPrefix = "sitemap-";
|
|
191
|
+
|
|
192
|
+
const isGriddoSitemapFile = (file: string) =>
|
|
193
|
+
file === "sitemap.xml" ||
|
|
194
|
+
(file.startsWith(templateSitemapPrefix) && file.endsWith(".xml"));
|
|
195
|
+
|
|
196
|
+
for (const site of sitesToPublish) {
|
|
197
|
+
const { id, languages } = site;
|
|
198
|
+
|
|
199
|
+
for (const lang of languages) {
|
|
200
|
+
if (AuthService.headers) {
|
|
201
|
+
AuthService.headers.lang = lang.id.toString();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const response = await getSitemap(id);
|
|
205
|
+
|
|
206
|
+
if (!response) continue;
|
|
207
|
+
|
|
208
|
+
const {
|
|
209
|
+
items: sitemapPagesGroup,
|
|
210
|
+
url: { home, domain },
|
|
211
|
+
} = response;
|
|
212
|
+
|
|
213
|
+
if (!home) continue;
|
|
214
|
+
|
|
215
|
+
const domainLanguage = site.domains.find(
|
|
216
|
+
(domain) => Object.keys(domain)[0] === lang.id.toString(),
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
if (!domainLanguage) continue;
|
|
220
|
+
|
|
221
|
+
const slug = Object.values(domainLanguage)[0];
|
|
222
|
+
const sitemaps: Array<string> = [];
|
|
223
|
+
const sitemapPageGroupKeys = Object.keys(sitemapPagesGroup);
|
|
224
|
+
const sitemapBasePath = path.join(distDir, slug.replace(domain, ""));
|
|
225
|
+
|
|
226
|
+
/** delete every griddo sitemap file before create the new ones */
|
|
227
|
+
if (fs.existsSync(sitemapBasePath)) {
|
|
228
|
+
try {
|
|
229
|
+
const files = fs.readdirSync(sitemapBasePath);
|
|
230
|
+
for (const file of files) {
|
|
231
|
+
if (isGriddoSitemapFile(file)) {
|
|
232
|
+
const filePath = path.join(sitemapBasePath, file);
|
|
233
|
+
try {
|
|
234
|
+
fs.unlinkSync(filePath);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
console.error(`Error removing ${filePath}:`, err);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} catch (err) {
|
|
241
|
+
console.error(`Error reading ${sitemapBasePath}:`, err);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
for (const templateId of sitemapPageGroupKeys) {
|
|
246
|
+
const sitemapPages = sitemapPagesGroup[templateId];
|
|
247
|
+
|
|
248
|
+
if (!sitemapPages.length) continue;
|
|
249
|
+
|
|
250
|
+
const siteMap = parse("urlset", {
|
|
251
|
+
"@": {
|
|
252
|
+
xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
|
|
253
|
+
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
|
|
254
|
+
"xsi:schemaLocation":
|
|
255
|
+
"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd",
|
|
256
|
+
},
|
|
257
|
+
url: sitemapPages,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const sitemapName = `/${templateSitemapPrefix}${templateId.toLowerCase()}.xml`;
|
|
261
|
+
const exactPath = path.join(sitemapBasePath, sitemapName);
|
|
262
|
+
|
|
263
|
+
saveFile(exactPath, siteMap);
|
|
264
|
+
|
|
265
|
+
sitemaps.push(
|
|
266
|
+
`${home.endsWith("/") ? home.slice(0, -1) : home}${sitemapName}`,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (!sitemaps.length) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const siteMap = parse("sitemapindex", {
|
|
275
|
+
"@": { xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" },
|
|
276
|
+
sitemap: sitemaps.map((loc) => ({ loc })),
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const exactPath = path.join(sitemapBasePath, "sitemap.xml");
|
|
280
|
+
|
|
281
|
+
saveFile(exactPath, siteMap);
|
|
282
|
+
verboseLog(`sitemap generated in ${exactPath}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Saves the content to a file specified by its path. If the file exists, it will be overwritten.
|
|
289
|
+
*
|
|
290
|
+
* @param filePath The path of the file to save the content to.
|
|
291
|
+
* @param content The content to save to the file.
|
|
292
|
+
*/
|
|
293
|
+
function saveFile(filePath: string, content: string) {
|
|
294
|
+
try {
|
|
295
|
+
const pathName = path.dirname(filePath);
|
|
296
|
+
if (!fs.existsSync(pathName)) {
|
|
297
|
+
fs.mkdirSync(pathName, { recursive: true });
|
|
298
|
+
}
|
|
299
|
+
fs.writeFileSync(filePath, content);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
console.log(error);
|
|
302
|
+
throw new Error(`Error saving a file`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export {
|
|
307
|
+
generateBuildReport,
|
|
308
|
+
generateSitemaps,
|
|
309
|
+
getSiteData,
|
|
310
|
+
getSitesToRender,
|
|
311
|
+
unpublishSites,
|
|
312
|
+
};
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import type { BuildMetaData } from "../types/api";
|
|
2
|
+
import type { RenderInfo } from "../types/global";
|
|
3
|
+
import type { GriddoPageObject } from "../types/pages";
|
|
4
|
+
import type { Site } from "../types/sites";
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
|
|
9
|
+
import fsx from "fs-extra";
|
|
10
|
+
|
|
11
|
+
import { throwError } from "../errors";
|
|
12
|
+
import { getConfig, removeProperties, walk, walkStore } from "./core-utils";
|
|
13
|
+
import { verboseLog } from "./loggin";
|
|
14
|
+
import { ReadFromStoreError, WriteToStoreError } from "../errors/errors-data";
|
|
15
|
+
|
|
16
|
+
const config = getConfig();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Read all path pages stored in the `store` Griddo directory and returns the
|
|
20
|
+
* absolute file path.
|
|
21
|
+
*
|
|
22
|
+
* @param domain - The domain to get the pages from.
|
|
23
|
+
*/
|
|
24
|
+
function getBuildPagesFromCachedStore<PageType extends GriddoPageObject>(
|
|
25
|
+
domain: string,
|
|
26
|
+
) {
|
|
27
|
+
const { __cache } = config.paths(domain);
|
|
28
|
+
return getBuildPagesFromStore<PageType>({
|
|
29
|
+
basePath: path.join(__cache, "store"),
|
|
30
|
+
withSizeProp: false,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Read all pages stored in the `store` Griddo directory and returns one by one
|
|
36
|
+
* with a generator.
|
|
37
|
+
*
|
|
38
|
+
* @param basePath - Base directory to get pages from.
|
|
39
|
+
* @param options.withSizeProp - Add size prop to the page object.
|
|
40
|
+
* @todo throw error if the basePath is not an store folder
|
|
41
|
+
*/
|
|
42
|
+
function* getBuildPagesFromStore<PageType extends GriddoPageObject>(args?: {
|
|
43
|
+
basePath?: string;
|
|
44
|
+
withSizeProp?: boolean;
|
|
45
|
+
}) {
|
|
46
|
+
const { basePath, withSizeProp } = args || {};
|
|
47
|
+
const { __cx } = config.paths();
|
|
48
|
+
const pagesDirPath = basePath || path.join(__cx, "store");
|
|
49
|
+
|
|
50
|
+
const jsonFilePaths = walkStore(pagesDirPath).filter(
|
|
51
|
+
(file) => path.extname(file) === ".json",
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
for (const filePath of jsonFilePaths) {
|
|
55
|
+
try {
|
|
56
|
+
const page = fsx.readJsonSync(filePath, {
|
|
57
|
+
encoding: "utf-8",
|
|
58
|
+
}) as PageType;
|
|
59
|
+
|
|
60
|
+
if (withSizeProp) {
|
|
61
|
+
const fileStats = fs.statSync(filePath);
|
|
62
|
+
page.size = fileStats.size / 1024;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// SECURITY: Only returns valid page objects
|
|
66
|
+
if (page.path) {
|
|
67
|
+
yield page;
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
throwError(ReadFromStoreError, error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Read all pages stored in `store` Griddo directory and return the absolute
|
|
77
|
+
* path.
|
|
78
|
+
*/
|
|
79
|
+
function getBuildPagesPath() {
|
|
80
|
+
const config = getConfig();
|
|
81
|
+
const { __cx } = config.paths();
|
|
82
|
+
|
|
83
|
+
const PAGES_DIR = path.join(__cx, "store");
|
|
84
|
+
const PAGE_FILES = walk(PAGES_DIR).filter(
|
|
85
|
+
(file) => path.extname(file) === ".json",
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
return PAGE_FILES;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get the build metadata from the Store.
|
|
93
|
+
*/
|
|
94
|
+
async function getBuildMetadata(): Promise<BuildMetaData> {
|
|
95
|
+
const { __cx } = config.paths();
|
|
96
|
+
const { sitesToPublish, createdPages, buildProcessData } = fsx.readJSONSync(
|
|
97
|
+
path.join(__cx, "render-metadata.json"),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
buildProcessData,
|
|
102
|
+
createdPages,
|
|
103
|
+
sitesToPublish,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Write render info into a file.
|
|
109
|
+
* @param renderInfo - Data that will be saved related to the render process.
|
|
110
|
+
*/
|
|
111
|
+
function saveRenderInfoInStore(renderInfo: RenderInfo) {
|
|
112
|
+
const { __cx } = config.paths();
|
|
113
|
+
|
|
114
|
+
fs.writeFileSync(
|
|
115
|
+
path.join(__cx, "render-metadata.json"),
|
|
116
|
+
JSON.stringify(renderInfo),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Return an array of ids only from `.json` files (no dirs) in the `basePath` dir based in the file name and filtered by the pages ids in a concrete site.
|
|
122
|
+
* @param basePath - Absolute path of the dir from which files will be read.
|
|
123
|
+
* @returns A Array<number> of pages ids in `basePath` dir filtered by pages ids in a concrete site.
|
|
124
|
+
*/
|
|
125
|
+
// function getPagesInStoreDirBySitePages(
|
|
126
|
+
// basePath: string,
|
|
127
|
+
// pages: Array<number>,
|
|
128
|
+
// ): Array<number> {
|
|
129
|
+
// const allPagesInStore = getPageInStoreDir(basePath);
|
|
130
|
+
// const pagesBySite = allPagesInStore.filter((page) => pages.includes(page));
|
|
131
|
+
|
|
132
|
+
// return pagesBySite;
|
|
133
|
+
// }
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Return an array of ids only from `.json` files (no dirs) in the `basePath` dir based in the file name.
|
|
137
|
+
* @param basePath - Absolute path of the dir from which files will be read.
|
|
138
|
+
* @returns A Array<number> of pages ids in `basePath` dir.
|
|
139
|
+
*/
|
|
140
|
+
function getPageInStoreDir(basePath: string): Array<number> {
|
|
141
|
+
const filesInStore = fs.readdirSync(basePath);
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
filesInStore
|
|
145
|
+
.filter((file) => {
|
|
146
|
+
const fullPathFile = `${basePath}/${file}`;
|
|
147
|
+
const stat = fs.statSync(fullPathFile);
|
|
148
|
+
// Si es un directorio, no lo incluimos.
|
|
149
|
+
if (stat?.isDirectory()) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
// Si es un archivo pero no tiene la extensión `.json`, no lo incluimos
|
|
153
|
+
if (path.extname(file) !== ".json") {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
// Si es un archivo con el nombre NO pasable a número (NaN), no lo incluimos.
|
|
157
|
+
const baseFilename = path.basename(file, path.extname(file));
|
|
158
|
+
if (Number.isNaN(Number.parseInt(baseFilename))) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// no es dir, es json y con nombre pasable a número, ok :)
|
|
163
|
+
return true;
|
|
164
|
+
})
|
|
165
|
+
// Tenemos solo jsons donde el nombre es un "número": "21345.json"
|
|
166
|
+
.map((page) => {
|
|
167
|
+
const filename = path.basename(page, path.extname(page));
|
|
168
|
+
return Number.parseInt(filename);
|
|
169
|
+
})
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Save the pages into the file system.
|
|
175
|
+
* @param pages - An array of Griddo page objects to be saved.
|
|
176
|
+
*/
|
|
177
|
+
function saveSitePagesInStore(
|
|
178
|
+
siteDirName: string,
|
|
179
|
+
pages: Array<GriddoPageObject>,
|
|
180
|
+
) {
|
|
181
|
+
const { __cx } = config.paths();
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
for (const page of pages) {
|
|
185
|
+
removeProperties(page, ["editorID", "parentEditorID"]);
|
|
186
|
+
const filename = `${page.context.page.id}.json`;
|
|
187
|
+
const filePath = path.join(__cx, "store", siteDirName, filename);
|
|
188
|
+
fsx.writeJSONSync(filePath, page);
|
|
189
|
+
}
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throwError(WriteToStoreError, error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Removes JSON files from the specified site directory based on the provided arrays of page numbers.
|
|
197
|
+
* For each number in the arrays, a corresponding file with the format `<number>.json` is removed if it exists.
|
|
198
|
+
*
|
|
199
|
+
* WARNING: This function may end up handling large arrays, so don't be tempted
|
|
200
|
+
* to refactor it to use something cool like the spread operator and make a
|
|
201
|
+
* single array, etc..
|
|
202
|
+
*
|
|
203
|
+
* @param siteDirName - The name of the site directory containing the pages.
|
|
204
|
+
* @param pageIdsArray - A two-dimensional array where each inner array contains numbers representing page files to be removed.
|
|
205
|
+
*/
|
|
206
|
+
function removeSitePagesFromStore(
|
|
207
|
+
siteDirName: string,
|
|
208
|
+
pageIdsArray: Array<Array<number>>,
|
|
209
|
+
) {
|
|
210
|
+
const { __cx } = config.paths();
|
|
211
|
+
|
|
212
|
+
const processPageIdsArray = (pageIds: Array<number>) => {
|
|
213
|
+
for (const filename of pageIds) {
|
|
214
|
+
const filePath = path.join(
|
|
215
|
+
__cx,
|
|
216
|
+
"store",
|
|
217
|
+
siteDirName,
|
|
218
|
+
`${filename}.json`,
|
|
219
|
+
);
|
|
220
|
+
try {
|
|
221
|
+
if (fs.existsSync(filePath)) {
|
|
222
|
+
fs.unlinkSync(filePath);
|
|
223
|
+
verboseLog(`Removed file: ${filePath}`);
|
|
224
|
+
}
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.log(error);
|
|
227
|
+
throw new Error(`Error removing file ${filename}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
for (const pageIds of pageIdsArray) {
|
|
233
|
+
processPageIdsArray(pageIds);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Removes sites that exist in the store that should no longer be there because
|
|
239
|
+
* the domain has been changed. This is necessary because those sites will not
|
|
240
|
+
* be marked as `sitesToUnpublish` so they must be removed manually.
|
|
241
|
+
*/
|
|
242
|
+
function removeOrphanSites(sitesToPublish: Array<Site>, domain: string) {
|
|
243
|
+
const { __cx } = getConfig().paths(domain);
|
|
244
|
+
const currentSitesInStore = fs.readdirSync(path.join(__cx, "store"));
|
|
245
|
+
const sitesFromAPI = sitesToPublish.map(({ id }) => `${id}`);
|
|
246
|
+
const sitesToDelete = currentSitesInStore.filter(
|
|
247
|
+
(site) => !sitesFromAPI.includes(site),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
for (const site of sitesToDelete) {
|
|
251
|
+
fs.rmSync(path.join(__cx, "store", site), { recursive: true, force: true });
|
|
252
|
+
verboseLog(`Removed orphan site id: ${site}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Returns the list of `activePages` pages that do not currently exist in a
|
|
258
|
+
* site's store (probably due to a failed render).
|
|
259
|
+
*/
|
|
260
|
+
function getMissingPublishedPagesInStore(
|
|
261
|
+
siteName: string,
|
|
262
|
+
activePages: Array<number>,
|
|
263
|
+
) {
|
|
264
|
+
const { __cx } = config.paths();
|
|
265
|
+
const pagesInStore = getPageInStoreDir(path.join(__cx, "store", siteName));
|
|
266
|
+
const missingActivePages: Array<number> = [];
|
|
267
|
+
for (const activePage of activePages) {
|
|
268
|
+
if (!pagesInStore.includes(activePage)) {
|
|
269
|
+
missingActivePages.push(activePage);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return missingActivePages;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Get pages that are not active anymore but are in the store.
|
|
277
|
+
*/
|
|
278
|
+
function getZombiePagesInStore(siteDir: string, activePages: Array<number>) {
|
|
279
|
+
const { __cx } = getConfig().paths();
|
|
280
|
+
const storeDir = path.join(__cx, "store", siteDir);
|
|
281
|
+
const sitePages = fs.readdirSync(storeDir);
|
|
282
|
+
|
|
283
|
+
let zombiePages: Array<number> = [];
|
|
284
|
+
for (const pageFileName of sitePages) {
|
|
285
|
+
const pageFileNameWithoutExtension = pageFileName.split(".")[0];
|
|
286
|
+
const pageInStore = Number.parseInt(pageFileNameWithoutExtension);
|
|
287
|
+
|
|
288
|
+
// What the hell is `pageIdInStore > 0` ??
|
|
289
|
+
// Las páginas de listados estáticos y multipage tiene ids "basura", que
|
|
290
|
+
// en este caso son ids negativos y siempre so borran al final del render.
|
|
291
|
+
// En este caso hay que ignorarlas porque NUNCA van a estar en el
|
|
292
|
+
// `activePages`, estaríamos borrándolas siempre antes de la fase de
|
|
293
|
+
// SSG.
|
|
294
|
+
if (!activePages.includes(pageInStore) && pageInStore > 0) {
|
|
295
|
+
zombiePages.push(pageInStore);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return zombiePages;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export {
|
|
303
|
+
getBuildMetadata,
|
|
304
|
+
getBuildPagesFromCachedStore,
|
|
305
|
+
getBuildPagesFromStore,
|
|
306
|
+
getBuildPagesPath,
|
|
307
|
+
getMissingPublishedPagesInStore,
|
|
308
|
+
getPageInStoreDir,
|
|
309
|
+
getZombiePagesInStore,
|
|
310
|
+
removeOrphanSites,
|
|
311
|
+
removeSitePagesFromStore,
|
|
312
|
+
saveSitePagesInStore,
|
|
313
|
+
saveRenderInfoInStore,
|
|
314
|
+
};
|