1 |
|
2 |
|
3 | function walk (file, cb) {
|
4 | var fs = require('fs');
|
5 | var q= [];
|
6 | var queue= [q];
|
7 | walk2();
|
8 |
|
9 | function walk2 () {
|
10 | cb(file);
|
11 | fs.lstat(file, function (err, stat) {
|
12 | if (err || !stat.isDirectory()) return next();
|
13 | getDirectory(function (files) {
|
14 | queue.push(q= files);
|
15 | next();
|
16 | });
|
17 | });
|
18 | }
|
19 |
|
20 | function next () {
|
21 | if (q.length) {
|
22 | file= q.pop();
|
23 | walk2();
|
24 | }
|
25 | else if (queue.length-= 1) {
|
26 | q= queue[queue.length-1];
|
27 | next();
|
28 | }
|
29 | }
|
30 |
|
31 | function getDirectory (cb) {
|
32 | fs.readdir(file, function(err, files) {
|
33 | if (!files) return;
|
34 |
|
35 | files.sort(sort);
|
36 | files.forEach(fullPath);
|
37 | cb(files);
|
38 | });
|
39 | }
|
40 |
|
41 | function fullPath (v,i,o) {
|
42 | o[i]= [file, '/', v].join('');
|
43 | }
|
44 |
|
45 | function sort (a,b) {
|
46 | a= a.toLowerCase();
|
47 | b= b.toLowerCase();
|
48 | if (a > b) return -1;
|
49 | if (a < b) return 1;
|
50 | else return 0;
|
51 | }
|
52 | }
|
53 |
|
54 |
|
55 | var ctr= 0;
|
56 | function callBack (file) { console.log( ["[", ++ctr, "] ", file].join('') ) };
|
57 |
|
58 | process.argv.forEach(function(val, index, array) {
|
59 | if (index > 1) walk(val, callBack);
|
60 | });
|