@griddo/cx 1.72.2 → 1.72.5

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/gatsby-node.js CHANGED
@@ -70,9 +70,31 @@ exports.onPreInit = async () => {
70
70
  initCache();
71
71
  await folders.prepareStaticFolder();
72
72
  await SettingsService.getAll();
73
- updatedSites = await sites.checkSites();
74
- helpers.log('info', 'RENDERING SITES ===>', updatedSites);
75
- await RobotsService.loadRobots();
73
+ try {
74
+ const { sitesToRender, sitesToUnpublish } = await sites.checkSites();
75
+
76
+ if (sitesToRender.length === 0 && sitesToUnpublish.length === 0) {
77
+ console.log(
78
+ '\n',
79
+ JSON.stringify({ sitesToRender, sitesToUnpublish }, null, 2),
80
+ '\n'
81
+ );
82
+
83
+ helpers.log('warning', 'There are no sites to update');
84
+ process.exit(0);
85
+ }
86
+
87
+ helpers.log('info', 'RENDERING SITES ===>', sitesToRender);
88
+ await sites.unpublishSites(sitesToUnpublish);
89
+ helpers.log('success', 'UNPUBLISHED SITES ->', sitesToUnpublish);
90
+ await folders.deleteSites([...sitesToRender]);
91
+ await RobotsService.loadRobots();
92
+
93
+ updatedSites = sitesToRender;
94
+ } catch (err) {
95
+ console.log(err.message);
96
+ process.exit(1);
97
+ }
76
98
  };
77
99
 
78
100
  // -----------------------------------------------------------------------------
@@ -83,7 +105,9 @@ exports.createPages = async ({ actions }) => {
83
105
  for (const site of updatedSites) {
84
106
  const cache = !site.shouldBeUpdated;
85
107
  const NavService = new NavigationService();
108
+
86
109
  const { id: siteID, slug: siteSlug, theme, favicon } = site;
110
+
87
111
  const {
88
112
  siteInfo,
89
113
  validPagesIds,
@@ -139,7 +163,7 @@ exports.createPages = async ({ actions }) => {
139
163
  showBasicMetaRobots,
140
164
  },
141
165
  BUILD_MODE,
142
- siteScript
166
+ siteScript,
143
167
  };
144
168
 
145
169
  helpers.log('info', 'VALID PAGES -->', validPagesIds);
@@ -173,27 +197,27 @@ exports.createPages = async ({ actions }) => {
173
197
  // Multi-page (pagination) or single-page
174
198
  isList
175
199
  ? await utils.renderListPages(
176
- {
177
- rootPage: page,
178
- pages: utils.getList(distributorTemplate),
179
- isRoot: false,
180
- defaultLang,
181
- distributorTemplate,
182
- },
183
- additionalInfo,
184
- actions.createPage
185
- )
200
+ {
201
+ rootPage: page,
202
+ pages: utils.getList(distributorTemplate),
203
+ isRoot: false,
204
+ defaultLang,
205
+ distributorTemplate,
206
+ },
207
+ additionalInfo,
208
+ actions.createPage
209
+ )
186
210
  : await utils.renderPage(
187
- {
188
- ...page,
189
- template: distributorTemplate,
190
- isRoot: false,
191
- defaultLang,
192
- multiPageElements,
193
- },
194
- additionalInfo,
195
- actions.createPage
196
- );
211
+ {
212
+ ...page,
213
+ template: distributorTemplate,
214
+ isRoot: false,
215
+ defaultLang,
216
+ multiPageElements,
217
+ },
218
+ additionalInfo,
219
+ actions.createPage
220
+ );
197
221
 
198
222
  buildProcessData[siteID].publishHashes.push(page.hash);
199
223
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@griddo/cx",
3
3
  "description": "Griddo SSG based on Gatsby",
4
- "version": "1.72.2",
4
+ "version": "1.72.5",
5
5
  "authors": [
6
6
  "Álvaro Sánchez' <alvaro.sanches@secuoyas.com>",
7
7
  "Carlos Torres <carlos.torres@secuoyas.com>",
@@ -65,7 +65,7 @@
65
65
  "react-helmet": "^6.0.0"
66
66
  },
67
67
  "devDependencies": {
68
- "@griddo/eslint-config-back": "^1.72.2",
68
+ "@griddo/eslint-config-back": "^1.72.5",
69
69
  "eslint": "^7.5.0",
70
70
  "eslint-plugin-node": "^11.1.0",
71
71
  "eslint-plugin-react": "7.14.3",
@@ -97,5 +97,5 @@
97
97
  "publishConfig": {
98
98
  "access": "public"
99
99
  },
100
- "gitHead": "13b8c276e0c121ce023d23ca45f25e91f1bc0adc"
100
+ "gitHead": "d41bd7e743330e0523b1218c58f4b97763a25338"
101
101
  }
@@ -1,84 +1,129 @@
1
1
  #!/usr/bin/env node
2
+ require('dotenv').config();
3
+
2
4
  const { execSync } = require('child_process');
3
- const chalk = require('chalk');
4
5
  const fs = require('fs');
5
6
  const path = require('path');
6
7
  const pkgDir = require('pkg-dir');
7
8
 
8
- const artifacts = ['dist', 'assets'];
9
+ const helpers = require('../src/utils/helpers');
10
+ const { initCache } = require('../src/utils/cache');
11
+ const { findDomains } = require('../src/utils/domains');
9
12
 
10
- console.clear();
13
+ const artifacts = ['dist', 'assets'];
11
14
 
12
15
  const currentPath = pkgDir.sync();
13
16
  const workingDirPath = pkgDir.sync(__dirname);
14
- const destDirPath = path.resolve(currentPath, 'exports');
17
+ const baseOutputPath = path.resolve(currentPath, 'public');
18
+ const destDirPath = path.resolve(currentPath, 'exports/sites');
15
19
 
16
- try {
17
- console.log();
18
- console.log(chalk.blue(' Griddo Exporter'));
19
- console.log();
20
+ // -----------------------------------------------------------------------------
21
+ // Process Runner
22
+ // -----------------------------------------------------------------------------
23
+ const getEnvRunner = env => command => runner(command, env);
20
24
 
21
- console.log(chalk.yellow(' Your sites are being exported'));
22
-
23
- execSync('npm run build', {
25
+ const runner = (command, env = {}) =>
26
+ execSync(command, {
24
27
  cwd: workingDirPath,
25
28
  stdio: 'inherit',
26
29
  shell: true,
30
+ env: {
31
+ ...process.env,
32
+ ...env,
33
+ GRIDDO_EXPORTER: true,
34
+ },
27
35
  });
28
36
 
29
- console.log(chalk.green(' Your sites have been exported correctly'));
37
+ const getAssetPrefix = domain => {
38
+ // TODO: Usar asset_prefix del dominio
39
+ if (!process.env.ASSET_PREFIX || !domain) return '';
40
+
41
+ if (!domain.startsWith('pro-')) return '';
42
+
43
+ return `${process.env.ASSET_PREFIX}/${domain}`;
44
+ };
45
+
46
+ // -----------------------------------------------------------------------------
47
+ // Loggers
48
+ // -----------------------------------------------------------------------------
49
+ const logError = (...message) => helpers.log('error', ...message);
50
+ const logInfo = (...message) => helpers.log('info', ...message);
51
+ const logSuccess = (...message) => helpers.log('success', ...message);
52
+ const logWarning = (...message) => helpers.log('warning', ...message);
30
53
 
31
- execSync(`mkdir -p exports`, {
32
- cwd: currentPath,
54
+ // -----------------------------------------------------------------------------
55
+ // Main Stuff
56
+ // -----------------------------------------------------------------------------
57
+ const runExportDomain = async domain => {
58
+ const run = getEnvRunner({
59
+ DOMAIN: domain,
60
+ ASSET_PREFIX: getAssetPrefix(domain),
33
61
  });
62
+ const domainDestDirPath = path.resolve(destDirPath, domain);
63
+
64
+ if (fs.existsSync(baseOutputPath)) {
65
+ logInfo(`Deleting base output path: ${baseOutputPath}`);
66
+ run(`rm -rf ${baseOutputPath}`);
67
+ }
68
+
69
+ run('npm run build');
70
+
71
+ logSuccess('Your sites have been exported correctly');
72
+
73
+ run(`mkdir -p ${domainDestDirPath}`);
34
74
 
35
75
  for (const dir of artifacts) {
36
76
  const cxDirSource = path.resolve(workingDirPath, dir);
37
- const cxDirDest = path.resolve(destDirPath, dir);
38
-
39
- if (fs.existsSync(cxDirSource)) {
40
- // TODO: Use proper tools instead of execSync to delete and move files
41
- try {
42
- if (fs.existsSync(cxDirDest)) {
43
- console.log(chalk.blue("Creando backup de " + cxDirDest + "..."));
44
- execSync(`mv ${cxDirDest} ${cxDirDest}-BACKUP`);
45
- }
46
- //execSync(`mkdir -p ${cxDirDest}`);
47
- console.log(chalk.blue("Moviendo ficheros de " + cxDirSource + " a " + cxDirDest + "..."));
48
- execSync(`mv ${cxDirSource} ${cxDirDest}`, {
49
- cwd: workingDirPath,
50
- });
51
- console.log(chalk.blue("Eliminando backup de " + cxDirDest + "..."));
52
- execSync(`rm -rf ${cxDirDest}-BACKUP`);
53
- } catch {
54
- console.log(
55
- chalk.red(` Error moving files to ${cxDirDest}`),
56
- componentDir,
57
- error.message
58
- );
59
- if (fs.existsSync(cxDirDest + "-BACKUP")) {
60
- console.log(chalk.blue("Restaurando " + cxDirDest + "..."));
61
- execSync(`mv ${cxDirDest}-BACKUP ${cxDirDest}`, {
62
- cwd: workingDirPath,
63
- });
64
- }
65
- continue;
77
+ const cxDirDest = path.resolve(domainDestDirPath, dir);
78
+ const cxDirDestBackup = `${cxDirDest}-BACKUP`;
79
+
80
+ if (!fs.existsSync(cxDirSource)) {
81
+ logError('Source directory has not been created:', cxDirSource);
82
+ continue;
83
+ }
84
+
85
+ try {
86
+ if (fs.existsSync(cxDirDest)) {
87
+ logInfo(`Creando backup de ${cxDirDest}...`);
88
+ run(`mv ${cxDirDest} ${cxDirDestBackup}`);
89
+ }
90
+
91
+ logInfo(`Moviendo ficheros de ${cxDirSource} a ${cxDirDest}...`);
92
+
93
+ run(`mv ${cxDirSource} ${cxDirDest}`);
94
+
95
+ logInfo(`Eliminando backup de ${cxDirDest}...`);
96
+ run(`rm -rf ${cxDirDestBackup}`);
97
+
98
+ logSuccess(`Your sites are ready to deploy at ${domainDestDirPath}`);
99
+ } catch (error) {
100
+ logError(`Error moving files to ${cxDirDest}`, error.message);
101
+
102
+ if (fs.existsSync(cxDirDestBackup)) {
103
+ logWarning(`Restaurando ${cxDirDest}...`);
104
+ run(`mv ${cxDirDestBackup} ${cxDirDest}`);
66
105
  }
67
- } else {
68
- console.log(
69
- chalk.red(" There's been an error with your export"),
70
- cxDirSource
71
- );
72
106
  continue;
73
107
  }
74
108
  }
109
+ };
110
+
111
+ const launchExports = async () => {
112
+ logInfo('Griddo Exporter');
113
+
114
+ initCache();
115
+
116
+ const domains = await findDomains();
117
+
118
+ for (const domain of domains) {
119
+ logInfo(`Exporting domain ${domain}`);
120
+ await runExportDomain(domain);
121
+ }
122
+ };
123
+
124
+ console.clear();
75
125
 
76
- console.log(
77
- chalk.green(` Your sites are ready to deploy at ${destDirPath}`)
78
- );
79
- console.log();
80
- } catch (err) {
81
- console.log(chalk.red('ERROR'), err?.stdout?.toString());
82
- console.log(chalk.red('ERROR'), err.message);
126
+ launchExports().catch(err => {
127
+ console.error('ERROR', err?.stdout?.toString() || err);
83
128
  process.exit(1);
84
- }
129
+ });
@@ -0,0 +1,16 @@
1
+ const api = require('./../api');
2
+
3
+ const baseURL = process.env.API_URL;
4
+ const ENDPOINTS = {
5
+ GET_ALL: `${baseURL}/domains`,
6
+ };
7
+
8
+ class DomainsService {
9
+ static async getAll() {
10
+ const { GET_ALL } = ENDPOINTS;
11
+ const response = await api.get(GET_ALL);
12
+ return response;
13
+ }
14
+ }
15
+
16
+ exports.default = DomainsService;
@@ -0,0 +1,24 @@
1
+ const AuthService = require('../services/auth').default;
2
+ const DomainsService = require('./../services/domains').default;
3
+
4
+ const getDomainHostname = url => {
5
+ const urlObject = new URL(url);
6
+ return urlObject.hostname;
7
+ };
8
+
9
+ const getDomainSlugs = domains =>
10
+ domains
11
+ .filter(domain => !!domain.slug)
12
+ .map(domain => domain.slug.replace('/',''))
13
+ .reduce((acc, item) => acc.indexOf(item) < 0 ? [...acc, item] : acc, []);
14
+
15
+ const findDomains = async () => {
16
+ await AuthService.login();
17
+ const domains = await DomainsService.getAll();
18
+
19
+ return getDomainSlugs(domains);
20
+ };
21
+
22
+ exports.getDomainHostname = getDomainHostname;
23
+ exports.getDomainSlugs = getDomainSlugs;
24
+ exports.findDomains = findDomains;
@@ -39,6 +39,9 @@ const deleteSites = async updatedSites => {
39
39
  };
40
40
 
41
41
  const updateDist = async () => {
42
+ const isGriddoExporter =
43
+ process.env.GRIDDO_EXPORTER && process.env.GRIDDO_EXPORTER === 'on';
44
+
42
45
  const src = './public';
43
46
  const dest = './dist';
44
47
 
@@ -54,6 +57,7 @@ const updateDist = async () => {
54
57
 
55
58
  const staticSrc = './dist/static';
56
59
  const staticDest = './assets/static';
60
+
57
61
  try {
58
62
  fs.copySync(src, dest);
59
63
  fs.copySync(pageDataSrc, pageDataDest);
@@ -63,6 +67,9 @@ const updateDist = async () => {
63
67
  const fileDest = `${assetsDest}/${file}`;
64
68
  fs.copySync(fileSrc, fileDest);
65
69
  });
70
+
71
+ if (isGriddoExporter) fs.rmSync(src, { recursive: true });
72
+
66
73
  helpers.log('success', 'SUCCESS!');
67
74
  } catch (err) {
68
75
  console.error(err);
@@ -3,8 +3,18 @@ const LOGS = process.env.LOGS && JSON.parse(process.env.LOGS);
3
3
 
4
4
  // Terminal Log
5
5
  const log = (state, message, data) => {
6
- const color = { error: 'red', info: 'cyan', success: 'green' };
7
- const msg = { error: 'ERROR', info: 'INFO', success: 'SUCCESS' };
6
+ const color = {
7
+ error: 'red',
8
+ info: 'cyan',
9
+ success: 'green',
10
+ warning: 'yellow',
11
+ };
12
+ const msg = {
13
+ error: 'ERROR',
14
+ info: 'INFO',
15
+ success: 'SUCCESS',
16
+ warning: '! ',
17
+ };
8
18
 
9
19
  LOGS !== false &&
10
20
  state !== 'error' &&
@@ -93,14 +93,14 @@ async function renderSinglePage(page, additionalInfo, createPage) {
93
93
 
94
94
  if (assetPrefix) mappedPage.matchPath = fullPath.compose;
95
95
 
96
+ createPage(mappedPage);
97
+
96
98
  const exportFile = path.resolve(
97
99
  __dirname,
98
100
  '../../dist/',
99
101
  `./${filepath}index.html`
100
102
  );
101
103
 
102
- createPage(mappedPage);
103
-
104
104
  const content = fs.existsSync(exportFile)
105
105
  ? fs.readFileSync(exportFile).toString()
106
106
  : '';
@@ -178,8 +178,9 @@ async function renderMultiplePages(page, additionalInfo, createPage) {
178
178
  ? paginatedPage.fullUrl.slice(0, -1)
179
179
  : paginatedPage.fullUrl;
180
180
  const rightSectionSlug = sectionSlug?.replace(/\//g, '');
181
- const newCompose = `${compose}${compose.endsWith('/') ? '' : '/'
182
- }${rightSectionSlug}`;
181
+ const newCompose = `${compose}${
182
+ compose.endsWith('/') ? '' : '/'
183
+ }${rightSectionSlug}`;
183
184
  paginatedPage.fullUrl = `${fullUrl}/${rightSectionSlug}`;
184
185
  paginatedPage.fullPath.compose = newCompose;
185
186
  paginatedPage.slug = newCompose;
@@ -9,7 +9,7 @@ const folders = require('./folders');
9
9
 
10
10
  const baseUrl = process.env.API_URL;
11
11
  const testSite = process.env.testSite && parseInt(process.env.testSite);
12
- const BYPASSRENDER = JSON.parse(process.env.BYPASSRENDER);
12
+ const BYPASSRENDER = JSON.parse(process.env.BYPASSRENDER || false);
13
13
  const updateAllSitesAlways = process.env.updateAllSites === 'on';
14
14
 
15
15
  const checkSites = async () => {
@@ -28,26 +28,20 @@ const checkSites = async () => {
28
28
  if (updatedSites.length) {
29
29
  for (const updatedSite of updatedSites) {
30
30
  const langResponse = await SitesService.getLanguages(updatedSite.id);
31
- const domains = langResponse.items.map(item => {
32
- return { [item.id]: `${item.domain.slug}${item.path}` };
33
- });
34
-
35
- updatedSite.domains = domains;
31
+ updatedSite.domains = langResponse.items
32
+ .filter(
33
+ item => !process.env.DOMAIN || item.domain.slug.indexOf(process.env.DOMAIN) > 0
34
+ )
35
+ .map(item => ({ [item.id]: `${item.domain.slug}${item.path}` }));
36
36
  }
37
37
  }
38
38
 
39
39
  const sitesToRender = updatedSites.filter(site =>
40
- testSite ? site.id === testSite : !!site.isPublished
40
+ testSite ? site.id === testSite : !!site.isPublished && site.domains.length > 0
41
41
  );
42
42
  const sitesToUnpublish = updatedSites.filter(site => !site.isPublished);
43
43
 
44
- await unpublishSites(sitesToUnpublish);
45
-
46
- helpers.log('success', 'UNPUBLISHED SITES ->', sitesToUnpublish);
47
-
48
- await folders.deleteSites([...updatedSites]);
49
-
50
- return [...sitesToRender];
44
+ return { sitesToRender, sitesToUnpublish };
51
45
  };
52
46
 
53
47
  const unpublishSites = async sites => {
@@ -190,6 +184,7 @@ const saveFile = (filePath, content) => {
190
184
  };
191
185
 
192
186
  exports.checkSites = checkSites;
187
+ exports.unpublishSites = unpublishSites;
193
188
  exports.setPageRenderEnd = setPageRenderEnd;
194
189
  exports.getSiteData = getSiteData;
195
190
  exports.generateSitemaps = generateSitemaps;