-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
get-files.js
69 lines (60 loc) · 1.63 KB
/
get-files.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
import corePath from 'path'
import fs from 'fs'
import { isNotJunk } from 'junk'
import once from 'once'
import parallel from 'run-parallel'
function notHidden (file) {
return file[0] !== '.'
}
function traversePath (path, fn, cb) {
fs.stat(path, (err, stats) => {
if (err) return cb(err)
if (stats.isDirectory()) {
fs.readdir(path, (err, entries) => {
if (err) return cb(err)
parallel(entries.filter(notHidden).filter(isNotJunk).map(entry => cb => {
traversePath(corePath.join(path, entry), fn, cb)
}), cb)
})
} else if (stats.isFile()) {
fn(path, cb)
}
// Ignore other types (not a file or directory)
})
}
/**
* Convert a file path to a lazy readable stream.
* @param {string} path
* @return {function}
*/
function getFilePathStream (path) {
return () => fs.createReadStream(path)
}
export default function getFiles (path, keepRoot, cb) {
traversePath(path, getFileInfo, (err, files) => {
if (err) return cb(err)
if (Array.isArray(files)) files = files.flat(Infinity)
else files = [files]
path = corePath.normalize(path)
if (keepRoot) {
path = path.slice(0, path.lastIndexOf(corePath.sep) + 1)
}
if (path[path.length - 1] !== corePath.sep) path += corePath.sep
files.forEach(file => {
file.getStream = getFilePathStream(file.path)
file.path = file.path.replace(path, '').split(corePath.sep)
})
cb(null, files)
})
}
function getFileInfo (path, cb) {
cb = once(cb)
fs.stat(path, (err, stat) => {
if (err) return cb(err)
const info = {
length: stat.size,
path
}
cb(null, info)
})
}