-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
295 lines (274 loc) · 10.9 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const fs = require('fs');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const { chromium } = require('playwright');
function launchChromeAndRunLighthouse(url, flags = {}, config = null) {
return chromeLauncher.launch(flags).then((chrome) => {
flags.port = chrome.port;
return lighthouse(url, flags, config).then((results) => chrome.kill().then(() => results));
});
}
const translations = {
es: {
title: 'Informes de Lighthouse',
subtitle: 'Más detalles en cada enlace',
description: 'Estos informes se generaron a través de un script',
lang: 'Español',
},
gl: {
title: 'Informes de Lighthouse',
subtitle: 'Máis detalles en cada enlace',
description: 'Estes informes foron xerados a través dun script',
lang: 'Galego',
},
en: {
title: 'Lighthouse reports',
subtitle: 'More details in each link',
description: 'These reports were generated via a script',
lang: 'English',
},
};
// LightHouse options
const options = {
logLevel: 'info',
output: 'html',
onlyCategories: ['performance', 'accessibility', 'seo'],
chromeFlags: ['--headless'],
settings: {
formFactor: 'desktop',
},
};
const DOMAIN = 'https://xabierlameiro.com';
/**
* Anonymouse function to run the script
* @description This script will generate a report for each url in the sitemap.xml file publish in xabierlameiro.com/sitemap.xml
* @see https://github.com/GoogleChrome/lighthouse
* @see https://playwright.dev/docs/api/class-playwright
* @see https://github.com/fperucic/treant-js
*/
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(`${DOMAIN}/sitemap.xml`);
// get site map inside a body
const sitemap = await page.$eval('body', (el) => el.innerHTML);
// get all urls inside sitemap
let urls = sitemap.match(/<loc>(.*?)<\/loc>/g).map((url) => url.replace(/<\/?loc>/g, ''));
// split urls in locales bearing in mind that 'en' is the default locale and it's not included in the url
let locales = urls.reduce((acc, url) => {
let locale = url.split('/')[3];
if (locale !== 'gl' && locale !== 'es') {
locale = 'en';
}
if (!acc[locale]) {
acc[locale] = [];
}
acc[locale].push(url);
return acc;
}, {});
const index = locales.en.indexOf(DOMAIN);
locales.en[index] = `${DOMAIN}/home`;
await browser.close();
locales.en.push(`${DOMAIN}/legal/cookies-policy`);
locales.en.push(`${DOMAIN}/legal/legal-notice`);
locales.en.push(`${DOMAIN}/legal/privacy-policy`);
if (!fs.existsSync('output')) {
fs.mkdirSync('output');
}
Object.keys(locales).forEach((locale) => {
if (!fs.existsSync(`output/${locale}`) && locale !== 'en') {
fs.mkdirSync(`output/${locale}`);
}
});
for (const lang of Object.keys(translations)) {
let levels = {};
for (const url of locales[lang]) {
// Report
const result = await launchChromeAndRunLighthouse(url === `${DOMAIN}/home` ? DOMAIN : url, options);
let fileName = url.replace(/https:\/\/xabierlameiro.com\//, '').replace(/\//g, '-');
if (fileName === 'es' || fileName === 'gl') {
fileName = 'home';
}
// write report in output folder
fs.writeFileSync(lang === 'en' ? `output/${fileName}.html` : `output/${lang}/${fileName}.html`, result.report);
const cleanUrl = url.replace(/https:\/\/xabierlameiro.com\//, '');
const urlWithoutLocale = cleanUrl.replace(/(gl|es)\//, '');
const urlSplitted = urlWithoutLocale.split('/');
let currentLevel = levels;
for (let i = 0; i < urlSplitted.length; i++) {
const part = urlSplitted[i];
if (i === urlSplitted.length - 1) {
if (!currentLevel[part]) {
currentLevel[part] = [];
}
currentLevel[part].push({
url: url,
route: lang === 'en' ? `../${fileName}.html` : `../${lang}/${fileName}.html`,
locale: lang,
});
} else {
if (!currentLevel[part]) {
currentLevel[part] = {};
}
currentLevel = currentLevel[part];
}
}
}
// Make connectors.js file
const config = {
...chart,
nodeStructure: {
...nodeStructure,
children: Object.keys(levels).map((firstLevel) => {
let url = '';
if (Array.isArray(levels[firstLevel])) {
url = levels[firstLevel].find((item) => item.locale === lang);
}
return {
text: { name: firstLevel === 'es' || firstLevel === 'gl' ? 'home' : firstLevel },
...(url && { link: { href: url?.route } }),
stackChildren: true,
connectors: {
style: {
stroke: '#8080FF',
'arrow-end': 'block-wide-long',
},
},
...(Object.keys(levels[firstLevel]).length > 0 && {
children: Object.keys(levels[firstLevel]).map((secondLevel) => {
if (isNaN(secondLevel)) {
let url = '';
if (Array.isArray(levels[firstLevel][secondLevel])) {
url = levels[firstLevel][secondLevel].find((item) => item.locale === lang);
}
return {
text: { name: secondLevel },
...(url && { link: { href: url?.route } }),
drawLineThrough: true,
collapsable: true,
stackChildren: true,
connectors: {
stackIndent: 30,
style: {
stroke: '#E3C61A',
'arrow-end': 'block-wide-long',
},
},
...(Object.keys(levels[firstLevel][secondLevel]).length > 0 && {
children: Object.keys(levels[firstLevel][secondLevel]).map((thirdLevel) => {
if (isNaN(thirdLevel)) {
let url = '';
if (Array.isArray(levels[firstLevel][secondLevel][thirdLevel])) {
url = levels[firstLevel][secondLevel][thirdLevel].find(
(item) => item.locale === lang,
);
}
return {
text: { name: thirdLevel },
...(url && { link: { href: url?.route } }),
drawLineThrough: true,
collapsable: true,
stackChildren: true,
};
}
}),
}),
};
}
}),
}),
};
}),
},
};
// clean empty childrens
config.nodeStructure.children.forEach((item) => {
if (item.children && item.children.every((item) => item === undefined)) {
delete item.children;
} else if (item.children) {
item.children.forEach((item) => {
if (item.children && item.children.every((item) => item === undefined)) {
delete item.children;
}
});
}
});
// write connectors to make a tree
fs.writeFileSync(
lang === 'en' ? `output/connectors.js` : `output/${lang}/connectors.js`,
`const config = ${JSON.stringify(config, null, 2)}`,
);
// Make links for the others languages
const links = Object.keys(translations)
.filter((item) => item !== lang)
.map(
(item) =>
`<li><a href="${item === 'en' ? '/index.html' : `../${item}/index.html`}">${
translations[item].lang
}</a></li>`,
);
// write entry point
fs.writeFileSync(
lang === 'en' ? `output/index.html` : `output/${lang}/index.html`,
`<!DOCTYPE html>
<html lang="${lang}">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width" />
<title>${translations[lang].title}</title>
<link rel="icon" href="../../asserts/favicon.svg" title="The favicon" />
<link rel="stylesheet" href="../../asserts/connectors.css" />
<meta name="robots" content="noindex" />
</head>
<body class="container">
<h1>${translations[lang].title}</h1>
<h2>${translations[lang].subtitle}</h2>
<p class="notice">${translations[lang].description}</p>
<ul class="links">${links.join('')}</ul>
<div class="chart" id="OrganiseChart-big-commpany"></div>
<script src="../../asserts/raphael.js"></script>
<script src="../../asserts/Treant.js"></script>
<script src="${lang === 'en' ? '../../' : `../../${lang}/`}connectors.js"></script>
<script>
new Treant(config);
var div = document.getElementById('OrganiseChart-big-commpany');
var element = document.querySelector('.node.big-commpany.domain');
div.scrollLeft = element.offsetLeft - div.clientWidth / 2 + element.clientWidth / 2;
</script>
</body>
</html>`,
);
}
})();
const chart = {
chart: {
container: '#OrganiseChart-big-commpany',
levelSeparation: 40,
siblingSeparation: 20,
subTeeSeparation: 30,
rootOrientation: 'NORTH',
nodeAlign: 'BOTTOM',
connectors: {
type: 'step',
style: {
'stroke-width': 2,
},
},
node: {
HTMLclass: 'big-commpany',
},
},
};
const nodeStructure = {
text: { name: 'xabierlameiro.com' },
HTMLclass: 'domain',
drawLineThrough: true,
collapsable: true,
connectors: {
style: {
stroke: 'blue',
'arrow-end': 'oval-wide-long',
},
},
};