-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-index.js
112 lines (85 loc) · 2.93 KB
/
generate-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
const fs = require('fs');
const path = require('path');
const NAME_MAX_LENGTH = 50;
const DATE_MAX_LENGTH = 20;
var MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
function pad (s) {
return s < 10 ? ('0' + s) : ('' + s)
}
function toHTML (dir, list) {
if (dir[dir.length - 1] === '/') dir = dir.slice(0, -1)
var prev = `\n<a href="../">../</a>\n`
var pre = prev + list.map(renderEntry).join('')
var dirname = dir || '/'
return `
<!DOCTYPE html>
<html>
<head><title>Index of ${dirname}</title></head>
<body bgcolor="white">
<h1>Index of ${dirname}</h1>
<hr>
<pre>
${pre}
</pre>
<hr>
</body>
</html>
`;
function renderEntry (entry) {
var name = entry.name;
var isDir = entry.isDirectory();
var size = isDir ? '-' : '' + entry.size;
var mtime = new Date(entry.mtime);
var d = pad(mtime.getUTCDate())
var m = MONTHS[mtime.getUTCMonth()]
var y = mtime.getUTCFullYear()
var h = pad(mtime.getUTCHours())
var min = pad(mtime.getUTCMinutes())
var time = `${d}-${m}-${y} ${h}:${min}`
if (isDir && name[name.length - 1] !== '/') name += '/'
var fname = elide(name, NAME_MAX_LENGTH);
var href = encodeURI(name)
return `<a href="${href}" title="${name}">${fname}</a>${''.padEnd(NAME_MAX_LENGTH - fname.length + 1) + time.padEnd(DATE_MAX_LENGTH) + size}\n`
}
}
function elide(name, maxLength) {
if (name.length > maxLength) return name.slice(0, maxLength - 3) + '..>'
return name
}
function getFiles(dir, ignore) {
return fs.readdirSync(dir, { withFileTypes: true })
.filter(d => (d.isFile() || d.isDirectory()) && !ignore.includes(d.name) && !d.name.startsWith('.'))
.map(d => {
const p = path.join(dir, d.name);
if (!metadata[p]) {
const st = fs.statSync(p);
metadata[p] = {
size: st.size,
mtime: st.mtime
};
}
d.size = metadata[p].size;
d.mtime = metadata[p].mtime;
return d;
});
}
// Metadata
var metadata = {};
try {
metadata = require('./metadata.json');
} catch(_) {}
// Main
const rootIgnoreFiles = [ 'CNAME', 'metadata.json', 'index.html', path.basename(__filename) ];
fs.writeFileSync('index.html', toHTML('/', getFiles('.', rootIgnoreFiles)));
// Dist
const distIgnoreFiles = [ 'index.html' ];
fs.writeFileSync('dist/index.html', toHTML('dist', getFiles('dist', rootIgnoreFiles)));
const distDirs = fs.readdirSync('dist', { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => d.name)
.sort();
for (const d of distDirs) {
const distDir = `dist/${d}`;
fs.writeFileSync(`${distDir}/index.html`, toHTML(distDir, getFiles(distDir, distIgnoreFiles)));
}
fs.writeFileSync('./metadata.json', JSON.stringify(metadata));