-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (42 loc) · 1.33 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
const path = require('path')
const fs = require('fs')
const Handlebars = require('handlebars')
const DEFAULTS = {
target: '',
destination: '',
mode: '',
prefix: '',
suffix: '',
filter: (children) => children,
}
const updateIndex = ({target, destination, mode, prefix, suffix, filter} = DEFAULTS) => {
let children = fs.readdirSync(target)
if (typeof filter === 'function') {
children = filter(children, mode)
}
let acceptList = {}
for (const child of children) {
const stat = fs.statSync(path.join(target, child))
const info = path.parse(child)
const isNotIndexJs = info.ext === '.js' && info.name !== 'index'
if (
(!mode && (isNotIndexJs || info.ext === '')) ||
(mode === 'FILE' && stat.isFile() && isNotIndexJs) ||
(mode === 'FOLDER' && stat.isDirectory())
) {
const exportKey = (prefix || '') + info.name + (suffix || '')
const exportPath = ('./' + path.relative(destination, target) + '/')
.replace(/\\/g, '/')
.replace(/\/\//g, '/')
acceptList[exportKey] = exportPath + info.name
}
}
let content = ''
content += 'module.exports = {\n'
for (let key in acceptList) {
content += ` ${key}: require('${acceptList[key]}'),\n`
}
content += '}\n'
fs.writeFileSync(path.join(destination, 'index.js'), content)
}
module.exports = updateIndex