-
Notifications
You must be signed in to change notification settings - Fork 0
/
adr.js
47 lines (38 loc) · 1.57 KB
/
adr.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
'use strict';
const { sep : path_separator, resolve : path_resolve } = require('path');
const { readdir, lstat } = require('fs');
const adr = module.exports = function adr(dir, callback){
const files = [], errors = []
readdir( dir, function(err, paths){
if(err) return callback([err]);
const paths_count = paths.length
let paths_processed = 0
paths.forEach(function(file){
let processing_path = path_resolve(dir + path_separator + file)
lstat( processing_path, function(err, stat){
if( err ){
errors.push(err)
paths_processed++
done(paths_count, paths_processed, errors, files, callback)
}else if( stat && stat.isDirectory() ){
adr( processing_path, function(err, new_files){
paths_processed++
if(err && err.length > 0)
errors = errors.concat(err)
files = files.concat(new_files)
done(paths_count, paths_processed, errors, files, callback)
})
}else{
paths_processed++
files.push(processing_path)
done(paths_count, paths_processed, errors, files, callback)
}
})
})
})
}
function done(total, processed, errors, files, callback){
if( total != processed ) return;
if( errors && errors.length > 0 ) return callback(errors, files)
callback(null, files)
}