-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverser.js
executable file
·64 lines (54 loc) · 1.79 KB
/
traverser.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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var traverse = function*(filePath) {
let list = [ path.resolve(filePath) ],
fileList = [],
tmp = null,
curr = null,
stat = null;
while (list.length > 0) {
curr = list.shift();
stat = (yield $P(fs.lstat)(curr))[1]; // (yield xxx)[1] is stats. [0] is err.
if (!stat) continue; // just ignore err here
if (stat.isDirectory()) {
tmp = (yield $P(fs.readdir)(curr))[1]; // (yield xxx)[1] is fileNames, [0] is err.
if (!tmp) continue; // just ignore err here
tmp = tmp.map(item => path.resolve(curr, item));
list = list.concat(tmp);
} else {
fileList.push(curr);
}
}
return fileList;
};
// a simple promisify function like bluebird.promisify
function $P(fn) {
return function() {
let THIS = this ? this : {};
let args = [].slice.call(arguments);
return new Promise(function(resolve) {
args.push(function(){
resolve([].slice.call(arguments));
});
fn.apply(this, args);
}.bind(THIS));
};
}
// a simple Executor like co.
function simpleExecutor(gen) {
let args = [].splice.call(arguments, 1);
gen = gen.apply(null, args);
return (function looper (prom) {
return prom.then(realValue => {
let t =gen.next(realValue); // t = {value: promise, done:xxx}
return t.done ? t.value : looper(Promise.resolve(t.value));
});
})(Promise.resolve());
}
/* call traverse */
/* promise api */
simpleExecutor(traverse, process.argv[2])
.then(res => console.log(res.length + '\n' + JSON.stringify(res)))
.catch(err => console.error('Catch Error:', err));