-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (48 loc) · 1.28 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
51
52
53
54
55
56
"use strict"
const path = require("path")
const nodeFs = require("fs")
const fsExtra = require("fs-extra")
const Promise = require('bluebird-lst')
function makeFs(Promise) {
const fs = Object.create(null)
for (const methodName of Object.keys(fsExtra)) {
const method = fsExtra[methodName]
if (methodName === "createFile" || methodName === "mkdirp") {
continue
}
if (typeof method !== "function" ||
methodName.endsWith("Sync") ||
methodName.endsWith("Stream") ||
methodName.match(/^[A-Z]/) ||
methodName === "exists" ||
methodName === "watch" ||
methodName === "watchFile" ||
methodName === "unwatchFile") {
fs[methodName] = method
}
else {
fs[methodName] = Promise.promisify(method)
}
}
fs.createFile = fs.ensureFile
fs.mkdirp = fs.mkdirs
return fs
}
const newFs = makeFs(Promise)
newFs.emptyDir = function (dir) {
return new Promise((resolve, reject) => {
nodeFs.readdir(dir, (error, files) => {
if (error == null) {
Promise.map(files, it => newFs.remove(path.join(dir, it)))
.then(resolve)
.catch(reject)
}
else {
newFs.mkdirs(dir)
.then(resolve)
.catch(reject)
}
})
})
}
module.exports = newFs