-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (49 loc) · 1.31 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
57
58
59
60
61
62
63
64
65
66
67
68
69
//import _ from 'lodash';
import path from 'path';
//import fs from 'fs-';
import promisify from 'es6-promisify';
import fs from 'fs';
const mkdir = promisify(fs.mkdir);
const unlink = promisify(fs.unlink);
let afs={};
afs.mkdir = async (dir_path,mode,dir_root) => {
const _recursive_loop = async (base_dir,dir_array) => {
try {
let fs_STAT = await fs.stat(base_dir);
}
catch(e) {
await mkdir(base_dir);
//console.log("ho creato ",base_dir);
}
if (dir_array.length === 0 )
return true;
else
await _recursive_loop(path.join(base_dir,dir_array.shift()),dir_array);
}
try {
let fs_STAT = await fs.stat(dir_path);
return null;
}
catch(e) {
const dir_array = dir_path.split(path.sep);
dir_array.shift(); //remove the first element ""
return await _recursive_loop(path.join('/',dir_array.shift()),dir_array);
}
}
afs.copy = async (src,dst) => {
let rd = fs.createReadStream(src);
let wr = fs.createWriteStream(dst);
//TODO this function is actually not tested
function rejectCleanup(err) {
rd.unpipe(wr);
//rd.destroy(); does it exists?
wr.end();
throw err;
}
rd.on('error', rejectCleanup);
wr.on('error', rejectCleanup);
wr.on('finish', () => true);
rd.pipe(wr);
}
afs.delete = async (path) => unlink(path);
export default afs;