-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhelper.js
executable file
·61 lines (50 loc) · 1.43 KB
/
helper.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
'use strict'
var readDir = require('readdir')
var _ = require('lodash')
let isDir = function (path) {
return path.match(/\/$/)
}
let getName = function (path) {
return _.split(path, '/').filter((i) => i !== '').pop()
}
let splitPath = function (path) {
return _.split(path, '/').filter((i) => i !== '')
}
let getDirList = function (path) {
var list = readDir.readSync(path, null, readDir.INCLUDE_DIRECTORIES + readDir.INCLUDE_HIDDEN)
return _.filter(list, (i) => !i.match(/[\\]\.|node_modules|\.git|\.DS_Store/))
}
let getDirJson = function (path) {
var list = readDir.readSync(path, null, readDir.INCLUDE_DIRECTORIES + readDir.INCLUDE_HIDDEN)
list = _.filter(list, (i) => !i.match(/[\\]\.|node_modules|\.git|\.DS_Store/))
var result = []
_.forEachRight(list, (item) => {
var info = {
path: '/' + item,
name: getName(item)
}
if (isDir(item)) {
info.type = 'directory'
info.children = []
} else {
info.type = 'file'
}
var deep = splitPath(item)
var temp = []
temp[0] = result
var i = 0
for (; i < deep.length - 1; i++) {
temp[i + 1] = temp[i].find(item => item.name === deep[i]).children
}
temp[i].splice(0, 0, info)
})
let rootName = process.cwd().split('/').pop()
let dirTree = {
name: rootName,
path: '/',
type: 'directory',
children: _.sortBy(result, ['type'])
}
return dirTree
}
module.exports = { getDirJson, getDirList }