-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir-bit.js
74 lines (71 loc) · 2.46 KB
/
dir-bit.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
70
71
72
73
/**
* Asynchronously removes an entire directory.
* @param {string} path The path of the directory to remove.
* @param {(boolean)} callback A callback that is executed after directory is removed.
* This callback is passed a boolean as an argument to check if the directory is removed.
*/
function removeDir(path,callback){
fs.readdir(path,"utf8",(err,f)=>{
if(err){
if(typeof (callback)==="function"){callback(false);}
return;
}
delFiless(path,f,0,(arr)=>{
delDirs(path,arr,0,(del)=>{
fs.rmdir(path,(e)=>{
if(e){};
if(typeof (callback)==="function"){callback(true);}
});
});
},[]);
});
};
function _delD(path,fileArr,start,callback){delDirs(path,fileArr,start,callback);};
function delDirs(path,fileArr,start,callback){
if(start<fileArr.length){
removeDir(path+"/"+fileArr[start],(del)=>{
_delD(path,fileArr,start+1,callback);
});
}else{
callback(true);
}
};
function _delFss(path,fileArr,start,callback,arr){delFiless(path,fileArr,start,callback,arr);};
function delFiless(path,fileArr,start,callback,arr){
if(start<fileArr.length){
fs.unlink(path+"/"+fileArr[start],(err)=>{
if(err){
arr.push(fileArr[start]);
}
_delFss(path,fileArr,start+1,callback,arr);
});
}else{
callback(arr);
}
};
function _delFs(path,fileArr,start,callback){delFiles(path,fileArr,start,callback);};
/**
* Asynchronously deletes files in a directory.
* @param {string} path The path to the directory where files are to be deleted.
* @param {string[]} fileArr An array of files to delete. Deletes only files that exist in the directory.
* @param {()=>void} callback An `optional` callback that is executed after files are deleted.
*/
function _delf(path,fileArr,callback){delFiles(path,fileArr,0,callback)};
function delFiles(path,fileArr,start,callback){
if(start<fileArr.length){
fs.unlink(path+"/"+fileArr[start],(err)=>{
if(err){}
_delFs(path,fileArr,start+1,callback);
});
}else{
if(typeof (callback)==="function"){
callback();
}
}
};
/**
*
* @param {object} new_fs `fs` module overriding object.
*/
function o(new_fs){fs=new_fs}
module.exports = {deleteFiles:_delf,removeDir:removeDir,overide_fs:o};