/
render.js
107 lines (83 loc) · 2.07 KB
/
render.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
// Native
const path = require('path')
// Packages
const pathType = require('path-type')
const filesize = require('filesize')
const fs = require('fs-promise')
const ip = require('ip')
// Ours
const prepareView = require('./view')
module.exports = async (port, current, dir, ignoredFiles) => {
let files = []
const subPath = path.relative(current, dir)
if (!fs.existsSync(dir)) {
return false
}
try {
files = await fs.readdir(dir)
} catch (err) {
throw err
}
for (const file of files) {
const filePath = path.resolve(dir, file)
const index = files.indexOf(file)
const details = path.parse(filePath)
details.relative = path.join(subPath, details.base)
if (await pathType.dir(filePath)) {
details.base += '/'
} else {
details.ext = details.ext.split('.')[1] || 'txt'
let fileStats
try {
fileStats = await fs.stat(filePath)
} catch (err) {
throw err
}
details.size = filesize(fileStats.size, {round: 0})
}
if (ignoredFiles.indexOf(details.base) > -1) {
delete files[index]
} else {
files[files.indexOf(file)] = details
}
}
const directory = path.join(path.basename(current), subPath, '/')
const pathParts = directory.split(path.sep)
if (dir.indexOf(current + '/') > -1) {
const directoryPath = [...pathParts]
directoryPath.shift()
files.unshift({
base: '..',
relative: path.join(...directoryPath, '..')
})
}
const render = prepareView()
const paths = []
pathParts.pop()
for (const part in pathParts) {
if (!{}.hasOwnProperty.call(pathParts, part)) {
continue
}
let before = 0
const parents = []
while (before <= part) {
parents.push(pathParts[before])
before++
}
parents.shift()
paths.push({
name: pathParts[part],
url: parents.join('/')
})
}
const details = {
address: ip.address(),
port,
files,
assetDir: process.env.ASSET_DIR,
directory,
nodeVersion: process.version.split('v')[1],
paths
}
return render(details)
}