-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.js
executable file
·362 lines (317 loc) · 11 KB
/
indexer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env node
import fsPromises from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
import pug from 'pug'
const source = process.env.STATICDIRS_SOURCE || path.resolve()
const destination = process.env.STATICDIRS_DESTINATION || path.resolve('build')
const webSiteName = process.env.STATICDIRS_SITENAME || 'Home'
const webDeptName = process.env.STATICDIRS_DEPTNAME || ''
const webDeptURL = process.env.STATICDIRS_DEPTURL || ''
const indexName = process.env.STATICDIRS_INDEXNAME || 'index.html'
const altIndexName = process.env.STATICDIRS_ALTINDEXNAME || 'staticdirs_index.html'
const webAssetsDir = process.env.STATICDIRS_WEBASSETSDIR || url.fileURLToPath(new URL('webassets/', import.meta.url))
const webRootURL = process.env.STATICDIRS_WEBROOT || url.pathToFileURL(destination)
const webExtraRoot = ['staticdirs']
const webScripts = webExtraRoot.concat(['scripts'])
const webImages = webExtraRoot.concat(['images'])
const webStyles = webExtraRoot.concat(['styles'])
const baseOptions = {
root: webRootURL,
index: indexName,
siteName: webSiteName,
deptName: webDeptName,
deptURL: webDeptURL,
scripts: [webRootURL].concat(webScripts).join('/'),
images: [webRootURL].concat(webImages).join('/'),
styles: [webRootURL].concat(webStyles).join('/')
}
const verboseLogger = {
indentLevel: 0,
message(mess) {
// eslint-disable-next-line no-console
console.log(mess)
},
debugMessage() {},
paddedName(name) {
return ' '.repeat(this.indentLevel) + name
},
formattedEntry(name) {
return this.paddedName(name)
},
formattedDir(dirName, parents, siblings, entries) {
const dirHeader = `${this.paddedName(dirName)}/ ${parents.join(' --> ')} | [${siblings.join(', ')}]`
return [dirHeader].concat(entries).join('\n')
},
deeper() {
return { ...this, indentLevel: this.indentLevel + 2 }
}
}
const normalLogger = {
message() {},
debugMessage() {},
paddedName() {},
formattedEntry() {},
formattedDir() {},
deeper() {
return this
}
}
const debugLogger = {
...verboseLogger,
debugMessage(mess) {
// eslint-disable-next-line no-console
console.log(mess)
}
}
const topLog = (level => {
switch (level) {
case '1': {
return verboseLogger
}
case '2': {
return debugLogger
}
default: {
return normalLogger
}
}
})(process.env.STATICDIRS_VERBOSE)
const errorExit = message => {
throw new Error(message)
}
const subTreeIndex = pug.compileFile(path.join(webAssetsDir, 'subtreeindex.pug'), { basedir: webAssetsDir })
const topLevelIndex = pug.compileFile(path.join(webAssetsDir, 'toplevelindex.pug'), { basedir: webAssetsDir })
const patternCopiedFiles = async (srcDir, dstDir, pattern, log) => {
try {
const dirents = await fsPromises.readdir(srcDir, { withFileTypes: true })
const copyList = dirents.filter(entry => entry.isFile() && pattern.test(entry.name))
.map(fileEntry => {
const src = path.join(srcDir, fileEntry.name)
const dst = path.join(dstDir, fileEntry.name)
return fsPromises.copyFile(src, dst, fsPromises.constants.COPYFILE_EXCL)
})
return Promise.all(copyList)
} catch (error) {
log.message(error)
}
}
const joinedPath = (root, pathList) => {
return path.join(root, ...pathList)
}
const tupleReducer = n => {
return tuplelist => {
const extract = tuplelist.map(tuple => tuple[n])
return extract.filter(nth => (nth !== null) && (nth !== undefined))
}
}
const extractWebRefs = tupleReducer(0)
const extractLogFrags = tupleReducer(1)
const breakageHandler = (entry, name, log) => {
return errmes => {
const badName = `BROKEN ${name}`
log.message(`Problem processing ${entry} ${name}: ${errmes}`)
return [
{ entryType: entry, href: '#', title: badName },
log.formattedEntry(badName)
]
}
}
const doIndexEntry = async (entry, srcTree, dstTree, log) => {
const { name } = entry
try {
if (entry.isFile()) {
await fsPromises.symlink(path.join(srcTree, name), path.join(dstTree, name))
}
return [
{ entryType: 'index' },
log.formattedEntry(name)
]
} catch (error) {
(breakageHandler('file', name, log))(error)
}
}
const doFileEntry = async (name, entryPath, dstTree, webTree, log) => {
const encoded = encodeURIComponent(name)
try {
await fsPromises.symlink(entryPath, path.join(dstTree, name))
return [
{ entryType: 'file', href: `${webTree}/${encoded}`, title: name },
log.formattedEntry(name)
]
} catch (error) {
(breakageHandler('file', name, log))(error)
}
}
const doSymlinkEntry = async (name, entryPath, webTree, log) => {
try {
const actualEntry = await fsPromises.realpath(entryPath)
const [rel, stats] = await Promise.all([
fsPromises.readlink(entryPath),
fsPromises.stat(actualEntry)
])
const relURL = rel.split(path.sep).map(part => encodeURIComponent(part)).join('/')
const relRef = (stats.isDirectory()) ? `${relURL}/${indexName}` : relURL
return [
{ entryType: 'link', href: `${webTree}/${relRef}`, title: name },
log.formattedEntry(name)
]
} catch (error) {
(breakageHandler('link', name, log))(error)
}
}
// eslint-disable-next-line max-params
const tupleEntryProcessor = (srcTree, dstTree, webTree, parents, siblings, log, doDirEntry) => {
return entry => {
if (entry.name === indexName) {
return doIndexEntry(entry, srcTree, dstTree, log)
}
const entryPath = path.join(srcTree, entry.name)
if (entry.isFile()) {
return doFileEntry(entry.name, entryPath, dstTree, webTree, log)
}
if (entry.isSymbolicLink()) {
return doSymlinkEntry(entry.name, entryPath, webTree, log)
}
if (entry.isDirectory()) {
return doDirEntry(entry.name, parents, siblings, log)
}
return errorExit(`Cannot process ${entry.name} in ${srcTree}`)
}
}
const crumb = (crumbBase, dirName) => {
const encoded = encodeURIComponent(dirName)
const currentBase = `${crumbBase}/${encoded}`
return [{ base: currentBase, href: `${currentBase}/${indexName}`, title: dirName }]
}
const webBreadcrumbs = (webRoot, parents) => {
if ((parents === undefined) || (parents.length === 0)) {
return []
}
const [dirName, ...rest] = parents
const trailStart = crumb(webRoot, dirName)
if ((rest === undefined) || (rest.length === 0)) {
return trailStart
}
const descent = (previous, trail) => {
if ((previous === undefined) || (previous.length === 0)) {
return trail
}
const [dirName, ...rest] = previous
const currentCrumb = crumb(trail[trail.length - 1].base, dirName)
if (rest === undefined) {
return trail.concat(currentCrumb)
}
return descent(rest, trail.concat(currentCrumb))
}
return descent(rest, trailStart)
}
const webNavProcessor = (webContext, current) => {
return navName => {
const encoded = encodeURIComponent(navName)
if (navName === current) {
return { href: '#', title: navName, active: true }
}
return { href: `${webContext}/${encoded}/${indexName}`, title: navName, active: false }
}
}
const writeIndex = async (dstTree, filledTemplate) => {
let fhandle
try {
fhandle = await fsPromises.open(path.join(dstTree, indexName), 'wx', 0o644)
await fhandle.writeFile(filledTemplate)
} catch (error) {
if (error.code === 'EEXIST') {
await fsPromises.writeFile(path.join(dstTree, altIndexName), filledTemplate, { mode: 0o644 })
} else {
throw error
}
} finally {
await fhandle?.close()
}
}
const dirProcessor = (srcRoot, dstRoot, webRoot) => {
const doDirEntry = async (dirName, parents, siblings, log) => {
const nextParents = parents.concat([dirName])
const nextLog = log.deeper()
const srcTree = joinedPath(srcRoot, nextParents)
const dstTree = joinedPath(dstRoot, nextParents)
const webAbove = [webRoot].concat(parents.map(encodeURIComponent)).join('/')
const webTree = `${webAbove}/${encodeURIComponent(dirName)}`
const navRefs = siblings.map(webNavProcessor(webAbove, dirName))
const breadcrumbs = webBreadcrumbs(webRoot, parents)
const process = entries => {
const nextSiblings = entries.filter(entry => entry.isDirectory())
.map(dirEntry => dirEntry.name)
return entries
.map(tupleEntryProcessor(srcTree, dstTree, webTree, nextParents, nextSiblings, nextLog, doDirEntry))
}
try {
await fsPromises.mkdir(dstTree, { mode: 0o755 })
const entries = await fsPromises.readdir(srcTree, { withFileTypes: true })
const tuples = await Promise.all(process(entries))
const dirRefs = extractWebRefs(tuples)
const subTreeLog = extractLogFrags(tuples)
const locals = { dirName, breadcrumbs, navRefs, dirRefs, ...baseOptions }
await writeIndex(dstTree, subTreeIndex(locals))
return [
{ entryType: 'dir', href: `${webTree}/${indexName}`, title: dirName },
log.formattedDir(dirName, parents, siblings, subTreeLog)
]
} catch (error) {
(breakageHandler('dir', dirName, log))(error)
}
}
return doDirEntry
}
const topLevelDir = async (srcRoot, dstRoot, webRoot, log, processedDir) => {
const process = entries => {
const nextSiblings = entries.filter(entry => entry.isDirectory())
.map(dirEntry => dirEntry.name)
return entries
.map(tupleEntryProcessor(srcRoot, dstRoot, webRoot, [], nextSiblings, log, processedDir))
}
try {
const entries = await fsPromises.readdir(srcRoot, { withFileTypes: true })
const tuples = await Promise.all(process(entries))
const dirRefs = extractWebRefs(tuples)
const dirLogs = extractLogFrags(tuples)
const locals = { dirRefs, ...baseOptions }
await writeIndex(dstRoot, topLevelIndex(locals))
return log.formattedDir(srcRoot, [], [], dirLogs)
} catch (error) {
(breakageHandler('dir', srcRoot, log))(error)
}
}
const placedWebAssets = async (assetRoot, dst, log) => {
const scriptDir = joinedPath(dst, webScripts)
const imageDir = joinedPath(dst, webImages)
const styleDir = joinedPath(dst, webStyles)
try {
await patternCopiedFiles(assetRoot, dst, /\.ico$/, log)
await Promise.all(
[scriptDir, imageDir, styleDir]
.map(dir => fsPromises.mkdir(dir, { mode: 493, recursive: true }))
)
return Promise.all([
patternCopiedFiles(assetRoot, scriptDir, /\.js$/, log),
patternCopiedFiles(assetRoot, imageDir, /\.((png)|(jpg)|(svg))$/, log),
patternCopiedFiles(assetRoot, styleDir, /\.css$/, log)
])
} catch (error) {
log.message(error)
}
}
const processedDirTree = dirProcessor(source, destination, webRootURL)
const topRunner = async (destination, topLog) => {
try {
await fsPromises.rm(destination, { recursive: true, force: true })
await fsPromises.mkdir(destination, { mode: 0o755 })
await placedWebAssets(webAssetsDir, destination, topLog)
const dirLog = await topLevelDir(source, destination, webRootURL, topLog.deeper(), processedDirTree)
topLog.message(dirLog)
} catch (error) {
topLog.message(error)
}
}
export default await topRunner(destination, topLog)