Created
October 14, 2013 22:28
-
-
Save lcaballero/6983319 to your computer and use it in GitHub Desktop.
Recursive + async traversal of a directory using Promises and Node
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
Q = require('q') | |
fs = require('fs') | |
_ = require("lodash") | |
isFile = (name) -> | |
fs.statSync(name).isFile() | |
isDirectory = (name) -> | |
fs.statSync(name).isDirectory() | |
withDir = (dir) -> | |
(files) -> _.map(files, (f) -> "#{dir}/#{f}") | |
read = (dir) -> | |
Q.nfcall(fs.readdir, dir) | |
.then(withDir dir) | |
.then((files) -> Q.all(toPromises files)) | |
toPromises = (files) -> | |
for f in files | |
if isFile f then Q(f) else read(f) #.then((m) -> m.concat("d " + f)) | |
read("../../WebUI/node_modules", []) | |
.then((files) -> _.flatten(files)) | |
.then((r) -> console.log r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment