forked from simwrapper/simwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-index.cjs
More file actions
60 lines (47 loc) · 1.48 KB
/
build-index.cjs
File metadata and controls
60 lines (47 loc) · 1.48 KB
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
'use strict'
const fs = require('fs')
const indexTemplate =
() => `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> \n\
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Directory listing for /</title></head> \n\
<body>\n\
<h1>Directory listing for %%FOLDER%%</h1><hr/>\n\
<ul>\n\
%%LISTING%%\n\
</ul>\n\
<hr/>\n\
</body>\n\
</html>`
const getFolderContent = source =>
fs.readdirSync(source, {
withFileTypes: true,
})
const buildIndexForFolder = source => {
const content = getFolderContent(source)
const folders = []
let listing = []
for (const f of content) {
if (f.name === 'index.html') continue
const clean = encodeURIComponent(f.name)
if (f.isDirectory()) {
listing.push(`<li><a href="${clean}/">${f.name}/</a></li>`)
folders.push(`${source}/${f.name}`)
} else {
listing.push(`<li><a href="${clean}">${f.name}</a></li>`)
}
}
let template = indexTemplate()
template = template.replace('%%FOLDER%%', source)
template = template.replace('%%LISTING%%', listing.join('\n'))
const indexHTML = `${source}/index.html`
console.log('Writing', indexHTML)
fs.writeFileSync(indexHTML, template)
for (const folder of folders) {
buildIndexForFolder(folder)
}
}
if (process.argv.length < 3) {
console.error('Error: Must supply folder, try:\n\nnode build-index.js public/data')
} else {
const dir = process.argv[2] || '.'
buildIndexForFolder(dir)
}