-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerender.mjs
81 lines (71 loc) · 2.19 KB
/
prerender.mjs
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
import https from 'https';
import fs from 'fs';
import { spawn } from 'node:child_process';
function sendRequest(apiUrl, module, action, params) {
return new Promise((resolve, reject) => {
var req = https.request(apiUrl,
{
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/json'
}
},
res => {
let rawData = '';
res.on('data', chunk => { rawData += chunk; });
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
resolve(parsedData);
} catch (e) {
reject(e.message);
}
});
}
);
req.on('error', (error) => {
reject(error);
});
req.write(JSON.stringify({ module, action, params }));
req.end();
});
}
function writeToFile(path, content) {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, err => {
if (err) {
reject(err);
}
resolve();
});
});
}
async function getNav(apiUrl) {
return await sendRequest(apiUrl, 'core', 'getnav', { includeSubNav: true });
}
function getUrlsFromNav(nav, baseUrl) {
const urls = [];
nav.forEach(one => {
urls.push(baseUrl + one.url);
if (one.sub) {
urls.push(...getUrlsFromNav(one.sub, baseUrl));
}
});
return urls;
}
async function run() {
const hostname = process.argv.slice(2)[0];
const apiUrl = `https://cms.${hostname}/system/json/`;
const nav = await getNav(apiUrl);
const urls = getUrlsFromNav(nav, '/');
writeToFile('routes.txt', urls.join('\n'));
process.env['HOSTNAME'] = hostname; // used in app.server.module.ts
const child = spawn('npx', [
'ng',
'run',
`wimme.net:prerender${hostname !== 'wimme.net' ? `:production-${hostname}`: ''}`
], { shell: process.platform === 'win32' });
child.stdout.pipe(process.stdout);
}
await run();