-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.js
56 lines (51 loc) · 1.19 KB
/
fs.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
const fs = require('fs');
const { Observable } = require('rxjs/Observable');
const readFile = (path, options = { encoding: null, flag: 'r' }) =>
Observable.create(observer =>
fs.readFile(path, options, (e, data) => {
if (e) {
observer.error(e);
}
observer.next(data);
observer.complete();
})
);
const writeFile = (file, data = '', options = {}) =>
Observable.create(observer =>
fs.writeFile(file, data, options, e => {
if (e) {
observer.error(e);
}
observer.complete();
})
);
const mkdir = (path, mask) =>
Observable.create(observer =>
fs.mkdir(path, mask, e => {
if (e) {
// ignore the error if the folder already exists
if (e.code === 'EEXIST') {
observer.complete();
} else {
observer.error(e);
} // something else went wrong
} else {
observer.complete();
}
})
);
const copyFile = (src, dest, flags = 0) =>
Observable.create(observer =>
fs.copyFile(src, dest, flags, e => {
if (e) {
observer.error(e);
}
observer.complete();
})
);
module.exports = {
readFile,
writeFile,
mkdir,
copyFile,
};