This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·154 lines (128 loc) · 4.82 KB
/
index.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
#! /usr/bin/env node
const fetch = require('node-fetch');
const {existsSync, readFileSync, mkdirSync, writeFileSync, copySync, readdirSync, rmdirSync} = require('fs-extra');
const {execSync, fork} = require('child_process');
const folder = process.argv[2] || 'static';
const reserved = ['public', '.production', '.development', 'src'];
if(reserved.includes(folder)) {
console.log('\x1b[31m%s\x1b[0m', `You cannot use a folder named "${folder}"`);
process.exit();
}
let isNullstackFolder = true;
if(!existsSync('package.json')) {
isNullstackFolder = false;
} else {
const json = readFileSync('package.json', 'utf-8');
if(json.indexOf('nullstack') === -1) {
isNullstackFolder = false;
}
}
if(!isNullstackFolder) {
console.log('\x1b[31m%s\x1b[0m', `You must be in a Nullstack project folder to run this command`);
process.exit();
}
const links = {};
let key;
let project;
const pages = {};
async function crawl(port, path) {
if(path.indexOf('.') > -1) {
console.log(`--> skipping: ${path}`)
return;
} else {
console.log(`--> reading: ${path}`)
}
links[path] = true;
const response = await fetch('http://localhost:' + port + path);
const html = await response.text();
if(key === undefined) {
const environmentLookup = 'window.environment = ';
const environment = html.split("\n").find((line) => line.indexOf(environmentLookup) > -1).split(environmentLookup)[1].slice(0, -1);
key = JSON.parse(environment).key;
}
if(project === undefined) {
const projectLookup = 'window.project = ';
project = JSON.parse(html.split("\n").find((line) => line.indexOf(projectLookup) > -1).split(projectLookup)[1].slice(0, -1));
}
if(path === '/404') {
writeFileSync(folder + '/404.html', html);
}
const instancesLookup = 'window.instances = ';
const instances = html.split("\n").find((line) => line.indexOf(instancesLookup) > -1).split(instancesLookup)[1].slice(0, -1);
const pageLookup = 'window.page = ';
const page = html.split("\n").find((line) => line.indexOf(pageLookup) > -1).split(pageLookup)[1].slice(0, -1);
if(path !== `/offline-${key}`) {
pages[path] = JSON.parse(page);
}
if(!existsSync(folder + path)) {
mkdirSync(folder + path, {recursive: true});
}
writeFileSync(folder + path + '/index.html', html);
if(path !== '/') {
writeFileSync(folder + path + '.html', html);
}
const json = `{"instances": ${instances}, "page": ${page}}`;
writeFileSync(folder + path + '/index.json', json);
const pattern = /<a href="(.*?)"/g;
while(match=pattern.exec(html)){
const link = match[1].split('#')[0];
if(link.startsWith('/')) {
if(links[link] === undefined) {
links[link] = false;
}
}
}
for(const link in links) {
if(!links[link]) {
await crawl(port, link);
}
}
};
async function copyPath(port, path) {
const response = await fetch(`http://localhost:${port}${path}`);
const json = await response.text();
writeFileSync(folder + path, json);
}
async function createSitemap() {
const timestamp = new Date().toJSON().substring(0,10);
const urls = Object.keys(pages).map((path) => {
const page = pages[path];
const canonical = `https://${project.domain}${path}`;
return `<url><loc>${canonical}</loc><lastmod>${timestamp}</lastmod>${page.changes ? `<changefreq>${page.changes}</changefreq>` : ''}${page.priority ? `<priority>${page.priority.toFixed(1)}</priority>` : ''}</url>`;
});
const xml = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join('')}</urlset>`;
writeFileSync(folder + '/sitemap.xml', xml);
}
async function run() {
console.log('\x1b[36m%s\x1b[0m', 'Starting the static site generation...');
if(existsSync(folder)) {
rmdirSync(folder, {recursive: true});
}
await execSync('npm run build');
const server = fork('.production/server.js', ['--static'], {silent: true});
server.stdout.on('data', async (buffer) => {
console.log('hey?')
const lookup = 'production mode at http://127.0.0.1:';
const message = buffer.toString('utf-8');
if(message.indexOf(lookup) > -1) {
const port = parseInt(message.split(lookup)[1]);
await crawl(port, '/');
await crawl(port, `/offline-${key}`);
await crawl(port, `/404`);
await copyPath(port, `/manifest-${key}.json`);
await copyPath(port, `/service-worker-${key}.js`);
await copyPath(port, `/robots.txt`);
await createSitemap();
server.kill();
console.log(`--> copying: public`)
copySync('public', folder);
for(const file of readdirSync('.production')) {
if(file.startsWith('client')) {
copySync('.production/' + file, folder + '/' + file.replace('.', `-${key}.`));
}
}
console.log('\x1b[36m%s\x1b[0m', 'Yay! Your static Nullstack application is ready.');
}
});
}
run();