-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildIndex.js
101 lines (86 loc) ยท 2.52 KB
/
buildIndex.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
const fs = require('fs');
const util = require('util');
const packageJson = require("./package.json");
const copyFile = util.promisify(fs.copyFile);
const exportFolder = 'data';
const isGithubActions = process.env.GITHUB_ACTIONS != null;
let formPackageVersion = '0.0.0';
async function generateHtml() {
if (isGithubActions) {
const formPackageJson = require("./nmsud-form/package.json");
formPackageVersion = formPackageJson.version;
}
await copyFile('./CNAME', `./${exportFolder}/CNAME`);
await generateHtmlForFolder(`./${exportFolder}`, [`<a href="/">๐ Home</a>`]);
}
function getLink(type, name) {
let emoji = 'โ';
if (type == 0) emoji = '๐';
if (type == 1) emoji = '๐';
return `<li data-sort="${type}-${name.toLowerCase()}"><a href="./${name}">${emoji} ${name}</a></li>`;
}
async function generateHtmlForFolder(folder, breadcrumbs) {
let fileLists = [];
const allFiles = fs.readdirSync(folder, { withFileTypes: true });
for (const dirent of allFiles) {
if (dirent.isDirectory()) {
fileLists.push(getLink(0, dirent.name));
await generateHtmlForFolder(
`./${folder}/${dirent.name}`,
[...breadcrumbs, `<a href="/${dirent.name}">${dirent.name}</a>`]
);
continue;
}
if (dirent.name.includes('index.html')) continue;
if (dirent.name.includes('CNAME')) continue;
if (dirent.name[0] == ".") continue;
if (dirent.name[0] == "_") continue;
fileLists.push(getLink(1, dirent.name));
}
fileLists.sort();
let htmlString = `
<!DOCTYPE html>
<html>
<head>
<title>NMSUD - Data browser</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
<style>
body {
background: #1c222f;
color: #FFFFFF;
font-family: Roboto;
margin: 0 2em;
}
h3 a,
ul li a {
text-decoration: none;
color: lightblue;
}
footer {
position: absolute;
bottom: 1em;
}
</style>
</head>
<body>
<h1>NMSUD - Data browser</h1>
<h3>${breadcrumbs.join(' / ')}</h3>
<hr />
<ul>
${fileLists.join('\n\t')}
</ul>
<footer>
<span>Data browser v${packageJson.version}</span>
<span> | </span>
<span>Form version: v${formPackageVersion}</span>
<span> | </span>
<span>Generated on ${(new Date()).toDateString()}</span>
</footer>
</body>
</html>
`;
fs.writeFile(`${folder}/index.html`, htmlString, ['utf8'], () => { });
}
generateHtml();