Last active
December 24, 2015 06:58
-
-
Save fillano/6760131 to your computer and use it in GitHub Desktop.
list files in specified directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(le, cb, payload) { | |
var count = 0; | |
this.getRecurser = function() { | |
return (function(f) { | |
return f(f); | |
})(function(f) { | |
count++; | |
return le(function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
return f(f).apply(f, args); | |
}) | |
}); | |
}; | |
this.run = { | |
payload: payload, | |
done: function() { | |
count--; | |
if(count===0) { | |
cb(this.payload); | |
} | |
} | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), p = require('path'), r = require('./async_recurser'); | |
module.exports = function(path, options, cb) { | |
var opt = { | |
withpath: false, | |
fileonly: true | |
}; | |
if(!!options && typeof options==='object') { | |
for(var i in options) { | |
if(options.hasOwnProperty(i)) { | |
opt[i] = options[i]; | |
} | |
} | |
} | |
if(!!options && typeof options==='function') { | |
cb = options; | |
} | |
var r1 = new r(walkdir, cb, []); | |
var walker = r1.getRecurser(); | |
walker(path, opt, cb, r1.run); | |
} | |
function walkdir(self) { | |
return function (path, options, cb, run) { | |
fs.readdir(path, function(err, entries) { | |
if(!err) { | |
entries.forEach(function(entry, index) { | |
var fullpath = p.join(path, entry); | |
fs.stat(fullpath, function(err, stats) { | |
if(!err) { | |
if(stats.isFile()) { | |
if(options.withpath) { | |
run.payload.push(fullpath); | |
} else { | |
run.payload.push(p.basename(fullpath)); | |
} | |
} | |
if(stats.isDirectory()) { | |
if(!options.fileonly) { | |
if(options.withpath) { | |
run.payload.push(fullpath); | |
} else { | |
run.payload.push(p.basename(fullpath)); | |
} | |
} | |
self(fullpath, options, cb, run); | |
} | |
} | |
if(entries.length-index===1) { | |
run.done(); | |
} | |
}); | |
}); | |
if(entries.length===0) { | |
run.done(); | |
} | |
} else { | |
run.done(); | |
} | |
}); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ls = require('./listfile'); | |
var target = process.argv[2]; | |
ls(target, {withpath:true,fileonly:false}, function(files) { | |
console.log(files); | |
console.log(files.length); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment