@griddo/cx 1.75.192 → 1.75.193

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.
@@ -15,7 +15,7 @@ import pkgDir from "pkg-dir";
15
15
  // Utils
16
16
  import { initCache } from "../src/utils/cache";
17
17
  import { findDomains } from "../src/utils/domains";
18
- import { exporterLogo as splash } from "../src/utils/shared";
18
+ import { exporterLogo as splash, measureFunctions } from "../src/utils/shared";
19
19
 
20
20
  // Envs
21
21
  const artifactsArchivable = ["dist", "assets", "apiCache", ".cache"];
@@ -39,24 +39,24 @@ type Env = Record<string, unknown>;
39
39
  const getEnvRunner = (env: Env) => (command: string) => runner(command, env);
40
40
 
41
41
  const runner = (command: string, env: Env = {}) =>
42
- // TODO: Cuando esto falle, ejecutar `cleanAfterFail`
43
- execSync(command, {
44
- cwd: workingPath,
45
- stdio: "inherit",
46
- env: {
47
- ...process.env,
48
- ...env,
49
- GRIDDO_EXPORTER: "true",
50
- },
51
- });
42
+ // TODO: Cuando esto falle, ejecutar `cleanAfterFail`
43
+ execSync(command, {
44
+ cwd: workingPath,
45
+ stdio: "inherit",
46
+ env: {
47
+ ...process.env,
48
+ ...env,
49
+ GRIDDO_EXPORTER: "true",
50
+ },
51
+ });
52
52
 
53
53
  const getAssetPrefix = (domain: string) => {
54
- // TODO: Usar asset_prefix del dominio
55
- if (!process.env.GRIDDO_ASSET_PREFIX || !domain) return "";
54
+ // TODO: Usar asset_prefix del dominio
55
+ if (!process.env.GRIDDO_ASSET_PREFIX || !domain) return "";
56
56
 
57
- if (!domain.startsWith("pro-")) return "";
57
+ if (!domain.startsWith("pro-")) return "";
58
58
 
59
- return `${process.env.GRIDDO_ASSET_PREFIX}/${domain}`;
59
+ return `${process.env.GRIDDO_ASSET_PREFIX}/${domain}`;
60
60
  };
61
61
 
62
62
  // -----------------------------------------------------------------------------
@@ -71,208 +71,240 @@ const logWarning = (...message: Array<any>) => console.log(...message);
71
71
  // Main Stuff
72
72
  // -----------------------------------------------------------------------------
73
73
  const domainExport = async (domain: string) => {
74
- const runner = getDomainRunner(domain);
75
- runner.init();
76
- runner.restoreArtifacts();
77
- runner.runExporter();
78
- runner.archiveArtifacts();
74
+ const runner = getDomainRunner(domain);
75
+ // onPreExporter
76
+ const endRestore = measureFunctions(runner.init, runner.restoreArtifacts);
77
+ logInfo(`\n⌛️ Domain ${domain} restaured: ${endRestore}s`);
78
+
79
+ // Gatsby
80
+ runner.runExporter();
81
+
82
+ // onPostExporter
83
+ const endArchive = measureFunctions(runner.archiveArtifacts);
84
+ logInfo(`\n⌛️ Domain ${domain} archived: ${endArchive}s`);
79
85
  };
80
86
 
81
87
  const launchExports = async () => {
82
- splash();
83
- initCache();
88
+ splash();
89
+ initCache();
84
90
 
85
- const domains = await findDomains();
91
+ const domains = await findDomains();
86
92
 
87
- for (const domain of domains) {
88
- logInfo(`️🚢 Exporting domain ${chalk.underline(domain)}`);
89
- await domainExport(domain);
90
- }
93
+ for (const domain of domains) {
94
+ logInfo(`️🚢 Exporting domain ${chalk.underline(domain)}`);
95
+ await domainExport(domain);
96
+ }
91
97
  };
92
98
 
93
99
  const getDomainRunner = (domain: string) => {
94
- const assetPrefix = getAssetPrefix(domain);
95
-
96
- const run = getEnvRunner({
97
- DOMAIN: domain,
98
- GRIDDO_ASSET_PREFIX: assetPrefix,
99
- HAS_ASSET_DOMAIN: assetPrefix && assetPrefix !== "",
100
- RENDERID: new Date().valueOf(),
101
- });
102
-
103
- const init = () => {
104
- logInfo(`🚀 Initializing ${chalk.underline(domain)} exporter`);
105
-
106
- cleanDirtyArtifactPaths();
107
- createBaseExportPaths();
108
- };
109
-
110
- const getArtifactPaths = (artifact = "") => {
111
- const domainExportArchiveBasePath = path.resolve(exportArchiveBasePath, domain);
112
- const currentExportArchivePath = path.resolve(domainExportArchiveBasePath, artifact);
113
- const currentExportBackupPath = `${currentExportArchivePath}-BACKUP`;
114
- const currentExportWorkingPath = path.resolve(workingPath, artifact);
115
-
116
- return {
117
- domainExportArchiveBasePath,
118
- currentExportArchivePath,
119
- currentExportBackupPath,
120
- currentExportWorkingPath,
121
- };
122
- };
123
-
124
- const cleanDirtyArtifactPaths = () => {
125
- for (const artifact of [...artifactsArchivable, ...artifactsDisposable]) {
126
- const { currentExportWorkingPath } = getArtifactPaths(artifact);
127
-
128
- if (fs.existsSync(currentExportWorkingPath)) {
129
- logWarning(`🧹 Cleanup dirty ${chalk.gray(currentExportWorkingPath)}`);
130
- run(`rm -rf ${currentExportWorkingPath}`);
131
- }
132
- }
133
- };
134
-
135
- const createBaseExportPaths = () => {
136
- const { domainExportArchiveBasePath } = getArtifactPaths();
137
-
138
- if (!fs.existsSync(domainExportArchiveBasePath)) {
139
- logInfo(`✨ Creating ${domainExportArchiveBasePath}`);
140
- run(`mkdir -p ${domainExportArchiveBasePath}`);
141
- }
142
- };
143
-
144
- const archiveExportArtifact = (artifact: string) => {
145
- const { currentExportArchivePath, currentExportWorkingPath } = getArtifactPaths(artifact);
146
-
147
- logInfo(`🚚 Moving files from ${chalk.gray(currentExportWorkingPath)} to ${chalk.gray(currentExportArchivePath)}`);
148
- run(`mv ${currentExportWorkingPath} ${currentExportArchivePath}`);
149
- };
150
-
151
- const archiveArtifacts = () => {
152
- for (const artifact of artifactsArchivable) {
153
- const { currentExportWorkingPath, currentExportArchivePath } = getArtifactPaths(artifact);
154
-
155
- if (!fs.existsSync(currentExportWorkingPath)) {
156
- logError("📁 Source directory has not been created:", currentExportWorkingPath);
157
- continue;
158
- }
159
-
160
- try {
161
- createArtifactBackup(artifact);
162
- archiveExportArtifact(artifact);
163
- deleteArtifactBackup(artifact);
164
- } catch (error) {
165
- logError(`🚚 Error moving files to ${currentExportArchivePath}`, error.message);
166
- restoreArtifactBackup(artifact);
167
- continue;
168
- }
169
- }
170
- };
171
-
172
- const restoreArtifacts = () => {
173
- for (const artifact of artifactsArchivable) {
174
- const { currentExportArchivePath, currentExportWorkingPath } = getArtifactPaths(artifact);
175
-
176
- if (fs.existsSync(currentExportArchivePath)) {
177
- logWarning(`️♻️ Restoring ${chalk.gray(currentExportArchivePath)}`);
178
- run(`cp -R ${currentExportArchivePath} ${currentExportWorkingPath}`);
179
- }
180
- }
181
-
182
- moveDistToPublic();
183
- };
184
-
185
- const createArtifactBackup = (artifact: string) => {
186
- const { currentExportArchivePath, currentExportBackupPath } = getArtifactPaths(artifact);
187
-
188
- if (fs.existsSync(currentExportArchivePath)) {
189
- logInfo(`💿 Creating backup of ${chalk.gray(currentExportArchivePath)}`);
190
- run(`mv ${currentExportArchivePath} ${currentExportBackupPath}`);
191
- }
192
- };
193
-
194
- const restoreArtifactBackup = (artifact: string) => {
195
- const { currentExportArchivePath, currentExportBackupPath } = getArtifactPaths(artifact);
196
-
197
- if (fs.existsSync(currentExportBackupPath)) {
198
- logWarning(`️♻️ Restoring backup ${chalk.gray(currentExportArchivePath)}`);
199
- run(`mv ${currentExportBackupPath} ${currentExportArchivePath}`);
200
- }
201
- };
202
-
203
- const deleteArtifactBackup = (artifact: string) => {
204
- const { currentExportArchivePath, currentExportBackupPath } = getArtifactPaths(artifact);
205
-
206
- logWarning(`️❌ Removing backup ${chalk.gray(currentExportArchivePath)}`);
207
- run(`rm -rf ${currentExportBackupPath}`);
208
- };
209
-
210
- const runExporter = () => {
211
- console.log(`\n${chalk.black(chalk.bgGreen(" Gatsby (start) "))}\n`);
212
-
213
- run("yarn export");
214
- logSuccess("Your sites have been exported correctly");
215
-
216
- console.log(`\n${chalk.black(chalk.bgGreen(" Gatsby (end) "))}\n`);
217
-
218
- removeDisposableArtifacts();
219
- };
220
-
221
- // TODO: Hacer que infra tire de `public` en vez de `dist` y quitar esto
222
- const moveDistToPublic = () => {
223
- const { currentExportWorkingPath: distPath } = getArtifactPaths("dist");
224
- const { currentExportWorkingPath: publicPath } = getArtifactPaths("public");
225
-
226
- if (fs.existsSync(distPath)) {
227
- logWarning(`🚚 Moving ${chalk.gray(distPath)} to ${chalk.gray(publicPath)}`);
228
- run(`mv ${distPath} ${publicPath}`);
229
- }
230
- };
231
-
232
- // TODO: Esto no debería hacerse desde infra
233
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
234
- const movePublicToDist = () => {
235
- const { currentExportWorkingPath: publicPath } = getArtifactPaths("public");
236
- const { currentExportWorkingPath: distPath } = getArtifactPaths("dist");
237
-
238
- if (fs.existsSync(publicPath)) {
239
- logWarning(`🚚 Moving ${publicPath} to ${distPath}`);
240
- run(`mv ${publicPath} ${distPath}`);
241
- }
242
- };
243
-
244
- const removeDisposableArtifacts = () => {
245
- for (const artifact of artifactsDisposable) {
246
- const { currentExportWorkingPath: artifactPath } = getArtifactPaths(artifact);
247
-
248
- if (fs.existsSync(artifactPath)) {
249
- logWarning(`❌ Removing ${chalk.gray(artifactPath)}`);
250
- run(`rm -rf ${artifactPath}`);
251
- }
252
- }
253
- };
254
-
255
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
256
- const cleanAfterFail = () => {
257
- cleanDirtyArtifactPaths();
258
- };
259
-
260
- return {
261
- init,
262
- getArtifactPaths,
263
- archiveExportArtifact,
264
- archiveArtifacts,
265
- restoreArtifacts,
266
- createArtifactBackup,
267
- restoreArtifactBackup,
268
- deleteArtifactBackup,
269
- runExporter,
270
- };
100
+ const assetPrefix = getAssetPrefix(domain);
101
+
102
+ const run = getEnvRunner({
103
+ DOMAIN: domain,
104
+ GRIDDO_ASSET_PREFIX: assetPrefix,
105
+ HAS_ASSET_DOMAIN: assetPrefix && assetPrefix !== "",
106
+ RENDERID: new Date().valueOf(),
107
+ });
108
+
109
+ const init = () => {
110
+ logInfo(`🚀 Initializing ${chalk.underline(domain)} exporter`);
111
+
112
+ cleanDirtyArtifactPaths();
113
+ createBaseExportPaths();
114
+ };
115
+
116
+ const getArtifactPaths = (artifact = "") => {
117
+ const domainExportArchiveBasePath = path.resolve(
118
+ exportArchiveBasePath,
119
+ domain
120
+ );
121
+ const currentExportArchivePath = path.resolve(
122
+ domainExportArchiveBasePath,
123
+ artifact
124
+ );
125
+ const currentExportBackupPath = `${currentExportArchivePath}-BACKUP`;
126
+ const currentExportWorkingPath = path.resolve(workingPath, artifact);
127
+
128
+ return {
129
+ domainExportArchiveBasePath,
130
+ currentExportArchivePath,
131
+ currentExportBackupPath,
132
+ currentExportWorkingPath,
133
+ };
134
+ };
135
+
136
+ const cleanDirtyArtifactPaths = () => {
137
+ for (const artifact of [...artifactsArchivable, ...artifactsDisposable]) {
138
+ const { currentExportWorkingPath } = getArtifactPaths(artifact);
139
+
140
+ if (fs.existsSync(currentExportWorkingPath)) {
141
+ logWarning(`🧹 Cleanup dirty ${chalk.gray(currentExportWorkingPath)}`);
142
+ run(`rm -rf ${currentExportWorkingPath}`);
143
+ }
144
+ }
145
+ };
146
+
147
+ const createBaseExportPaths = () => {
148
+ const { domainExportArchiveBasePath } = getArtifactPaths();
149
+
150
+ if (!fs.existsSync(domainExportArchiveBasePath)) {
151
+ logInfo(`✨ Creating ${domainExportArchiveBasePath}`);
152
+ run(`mkdir -p ${domainExportArchiveBasePath}`);
153
+ }
154
+ };
155
+
156
+ const archiveExportArtifact = (artifact: string) => {
157
+ const { currentExportArchivePath, currentExportWorkingPath } =
158
+ getArtifactPaths(artifact);
159
+
160
+ logInfo(
161
+ `🚚 Moving files from ${chalk.gray(
162
+ currentExportWorkingPath
163
+ )} to ${chalk.gray(currentExportArchivePath)}`
164
+ );
165
+ run(`mv ${currentExportWorkingPath} ${currentExportArchivePath}`);
166
+ };
167
+
168
+ const archiveArtifacts = () => {
169
+ for (const artifact of artifactsArchivable) {
170
+ const { currentExportWorkingPath, currentExportArchivePath } =
171
+ getArtifactPaths(artifact);
172
+
173
+ if (!fs.existsSync(currentExportWorkingPath)) {
174
+ logError(
175
+ "📁 Source directory has not been created:",
176
+ currentExportWorkingPath
177
+ );
178
+ continue;
179
+ }
180
+
181
+ try {
182
+ createArtifactBackup(artifact);
183
+ archiveExportArtifact(artifact);
184
+ deleteArtifactBackup(artifact);
185
+ } catch (error) {
186
+ logError(
187
+ `🚚 Error moving files to ${currentExportArchivePath}`,
188
+ error.message
189
+ );
190
+ restoreArtifactBackup(artifact);
191
+ continue;
192
+ }
193
+ }
194
+ };
195
+
196
+ const restoreArtifacts = () => {
197
+ for (const artifact of artifactsArchivable) {
198
+ const { currentExportArchivePath, currentExportWorkingPath } =
199
+ getArtifactPaths(artifact);
200
+
201
+ if (fs.existsSync(currentExportArchivePath)) {
202
+ logWarning(`️♻️ Restoring ${chalk.gray(currentExportArchivePath)}`);
203
+ run(`cp -R ${currentExportArchivePath} ${currentExportWorkingPath}`);
204
+ }
205
+ }
206
+
207
+ moveDistToPublic();
208
+ };
209
+
210
+ const createArtifactBackup = (artifact: string) => {
211
+ const { currentExportArchivePath, currentExportBackupPath } =
212
+ getArtifactPaths(artifact);
213
+
214
+ if (fs.existsSync(currentExportArchivePath)) {
215
+ logInfo(`💿 Creating backup of ${chalk.gray(currentExportArchivePath)}`);
216
+ run(`mv ${currentExportArchivePath} ${currentExportBackupPath}`);
217
+ }
218
+ };
219
+
220
+ const restoreArtifactBackup = (artifact: string) => {
221
+ const { currentExportArchivePath, currentExportBackupPath } =
222
+ getArtifactPaths(artifact);
223
+
224
+ if (fs.existsSync(currentExportBackupPath)) {
225
+ logWarning(
226
+ `️♻️ Restoring backup ${chalk.gray(currentExportArchivePath)}`
227
+ );
228
+ run(`mv ${currentExportBackupPath} ${currentExportArchivePath}`);
229
+ }
230
+ };
231
+
232
+ const deleteArtifactBackup = (artifact: string) => {
233
+ const { currentExportArchivePath, currentExportBackupPath } =
234
+ getArtifactPaths(artifact);
235
+
236
+ logWarning(`️❌ Removing backup ${chalk.gray(currentExportArchivePath)}`);
237
+ run(`rm -rf ${currentExportBackupPath}`);
238
+ };
239
+
240
+ const runExporter = () => {
241
+ console.log(`\n${chalk.black(chalk.bgGreen(" Gatsby (start) "))}\n`);
242
+
243
+ run("yarn export");
244
+ logSuccess("Your sites have been exported correctly");
245
+
246
+ console.log(`\n${chalk.black(chalk.bgGreen(" Gatsby (end) "))}\n`);
247
+
248
+ removeDisposableArtifacts();
249
+ };
250
+
251
+ // TODO: Hacer que infra tire de `public` en vez de `dist` y quitar esto
252
+ const moveDistToPublic = () => {
253
+ const { currentExportWorkingPath: distPath } = getArtifactPaths("dist");
254
+ const { currentExportWorkingPath: publicPath } = getArtifactPaths("public");
255
+
256
+ if (fs.existsSync(distPath)) {
257
+ logWarning(
258
+ `🚚 Moving ${chalk.gray(distPath)} to ${chalk.gray(publicPath)}`
259
+ );
260
+ run(`mv ${distPath} ${publicPath}`);
261
+ }
262
+ };
263
+
264
+ // TODO: Esto no debería hacerse desde infra
265
+ const movePublicToDist = () => {
266
+ const { currentExportWorkingPath: publicPath } = getArtifactPaths("public");
267
+ const { currentExportWorkingPath: distPath } = getArtifactPaths("dist");
268
+
269
+ if (fs.existsSync(publicPath)) {
270
+ logWarning(`🚚 Moving ${publicPath} to ${distPath}`);
271
+ run(`mv ${publicPath} ${distPath}`);
272
+ }
273
+ };
274
+
275
+ const removeDisposableArtifacts = () => {
276
+ for (const artifact of artifactsDisposable) {
277
+ const { currentExportWorkingPath: artifactPath } =
278
+ getArtifactPaths(artifact);
279
+
280
+ if (fs.existsSync(artifactPath)) {
281
+ logWarning(`❌ Removing ${chalk.gray(artifactPath)}`);
282
+ run(`rm -rf ${artifactPath}`);
283
+ }
284
+ }
285
+ };
286
+
287
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
288
+ const cleanAfterFail = () => {
289
+ cleanDirtyArtifactPaths();
290
+ };
291
+
292
+ return {
293
+ init,
294
+ getArtifactPaths,
295
+ archiveExportArtifact,
296
+ archiveArtifacts,
297
+ restoreArtifacts,
298
+ createArtifactBackup,
299
+ restoreArtifactBackup,
300
+ deleteArtifactBackup,
301
+ runExporter,
302
+ };
271
303
  };
272
304
 
273
305
  console.clear();
274
306
 
275
307
  launchExports().catch((err) => {
276
- console.error("❌ ERROR", err?.stdout?.toString() || err);
277
- process.exit(1);
308
+ console.error("❌ ERROR", err?.stdout?.toString() || err);
309
+ process.exit(1);
278
310
  });
@@ -111,6 +111,18 @@ export function getSafeSiteId(response: APIResponses) {
111
111
  return "site" in response && response.site ? response?.site : undefined;
112
112
  }
113
113
 
114
+ /**
115
+ * Execute functions and return time it takes for functions to execute
116
+ * @param funcs Array of functions
117
+ * @returns seconds it takes for functions to execute
118
+ */
119
+ function measureFunctions(...funcs: Array<() => void>) {
120
+ const start = performance.now();
121
+ funcs.forEach((func) => func());
122
+
123
+ return msToSec(performance.now() - start);
124
+ }
125
+
114
126
  /**
115
127
  * Print a f**king great Griddo builder logo.
116
128
  */
@@ -152,4 +164,13 @@ function exporterLogo() {
152
164
  console.log(gradient.cristal(logo));
153
165
  }
154
166
 
155
- export { delay, exporterLogo, logBox, logInfo, logPageSize, splash, walk };
167
+ export {
168
+ delay,
169
+ exporterLogo,
170
+ logBox,
171
+ logInfo,
172
+ logPageSize,
173
+ measureFunctions,
174
+ splash,
175
+ walk,
176
+ };