-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
71 lines (57 loc) · 1.63 KB
/
build.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
const fs = require('fs');
let sitemap = [];
function buildFolder(folder) {
fs.readdirSync(folder).forEach(file => {
const full_path = folder + '/' + file;
console.log("Processing: " + full_path);
if (fs.lstatSync(full_path).isDirectory()) {
fs.mkdirSync('out/' + file);
buildFolder(full_path);
} else {
buildFile(full_path);
}
});
}
function buildFile(file) {
if (file.endsWith(".html") || file.endsWith(".js") || file.endsWith(".css")) {
var contents = fs.readFileSync(file, 'utf8');
fs.readdirSync('content').forEach(file => {
if (file.endsWith(".html")) {
var cont_file = file.substring(0, file.length - 5);
var token = "{{content." + cont_file + "}}";
contents = contents.split(token).join(fs.readFileSync('content/' + file, 'utf8'));
}
});
addToSitemap(file);
fs.writeFileSync('out/' + file.substring(4), contents);
} else {
fs.copyFileSync(file, 'out/' + file.substring(4));
}
}
function addToSitemap(file) {
if (file.endsWith("googleb196aa64fd5d60bb.html")) {
return;
}
var url = "https://theultimatefoxos.dev/" + file;
if (url.endsWith("index.html")) {
url = url.substring(0, url.length - 10);
}
sitemap.push(url);
}
function sitemapToXML() {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
sitemap.forEach(url => {
xml += ' <url>\n';
xml += ' <loc>' + url + '</loc>\n';
xml += ' </url>\n';
});
xml += '</urlset>';
return xml;
}
try {
fs.rmSync('out', { recursive: true });
} catch (err) {}
fs.mkdirSync('out');
buildFolder('src');
fs.writeFileSync('out/sitemap.xml', sitemapToXML(), 'utf8');