-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
52 lines (48 loc) · 1.44 KB
/
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
import EventEmitter from 'events'
import serveIndex from 'serve-index-75lb'
class Index extends EventEmitter {
description () {
return 'Serves directory listings.'
}
optionDefinitions () {
return [
{
name: 'index.root',
type: String,
typeLabel: '{underline path}',
description: 'Index root directory, defaults to the same value as --directory or the current directory.'
},
{
name: 'index.hidden',
type: Boolean,
description: 'Show hidden files.'
},
{
name: 'index.view',
type: String,
typeLabel: '{underline name}',
description: 'Display mode, either `tiles` or `details`. Defaults to tiles.'
}
]
}
middleware (options) {
const path = options.indexRoot || options.directory || process.cwd()
if (path) {
const indexOptions = { icons: true }
if (options.indexHidden !== undefined) indexOptions.hidden = options.indexHidden
if (options.indexView !== undefined) indexOptions.view = options.indexView
this.emit('verbose', 'middleware.index.config', indexOptions)
const index = serveIndex(path, indexOptions)
return function (ctx, next) {
return new Promise((resolve, reject) => {
function expressNext () {
next()
resolve()
}
index(ctx.req, ctx.res, expressNext, ctx)
})
}
}
}
}
export default Index