-
Notifications
You must be signed in to change notification settings - Fork 2
/
ondir.js
46 lines (39 loc) · 1.62 KB
/
ondir.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
var fs = require('fs');
var path = require('path');
var util = require('util');
var Gate = require('./gate');
/**
* executes across all files/dirs in a directory - non recursive.
* Note that the main callback is not called until either all the file/dir items have executed
* (assuming the path is good).
*
* @param root_path :string - an absolute directory path
* @param if_file(done_callback): :function - executes on all files. Passed full path to file.
* @param if_dir(done_callback): :function - executes on all directories. Passed full path to file.
* @param if_nopath(callback) :function - executes if root path does not exist; otherwise error thrown.
*/
module.exports = function(root_path, callback, if_file, if_dir, if_nopath) {
root_path = root_path.replace(/\/$/, '');
if (!fs.existsSync(root_path)) {
// console.log('no path ', root_path);
if (if_nopath) {
// console.log('handling no path ', root_path);
if_nopath(root_path, callback);
} else {
callback(new Error(util.format('%s: cannot find path "%s"', __dirname, root_path)));
}
} else {
var items = fs.readdirSync(root_path);
var gate = new Gate(callback);
items.forEach(function(item) {
var full_path = root_path + '/' + item;
var s = fs.statSync(full_path);
if (s.isDirectory() && if_dir) {
if_dir(full_path, gate.task_done_callback(true));
} else if (s.isFile() && if_file){
if_file(full_path, gate.task_done_callback(true));
}
})
gate.start();
}
}