-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathfs-utils.js
More file actions
48 lines (40 loc) · 930 Bytes
/
fs-utils.js
File metadata and controls
48 lines (40 loc) · 930 Bytes
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
/* eslint no-use-before-define: 0 */
import Rx from 'rx';
import path from 'path';
import fsp from 'fs-promise';
function expanddir(dir) {
let fullDirPath = path.resolve(dir);
let paths = fsp.readdir(dir);
return Rx.Observable.fromPromise(paths)
.flatMap(x => x)
.map(filename => {
let filepath = path.join(fullDirPath, filename);
return expand(filepath);
})
.flatMap(x => x);
}
function expand(filepath) {
let fullPath = path.resolve(filepath);
let p = fsp.stat(fullPath);
return Rx.Observable.fromPromise(p)
.flatMap(stat => {
if (stat.isDirectory()) {
return expanddir(fullPath);
} else {
return Rx.Observable.just(fullPath);
}
});
}
function ensureDir(dir) {
return fsp.exists(dir)
.then(exists => {
if (exists) {
return null;
}
return fsp.mkdir(dir);
});
}
export default {
expand,
ensureDir
};