Created
April 23, 2010 21:43
-
-
Save aconbere/377225 to your computer and use it in GitHub Desktop.
This file contains 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
path.walk = function (start, callback) { | |
fs.stat(start, function (err, stat) { | |
if (stat.isDirectory()) { | |
fs.readdir(start, function (err, files) { | |
var coll = files.reduce(function (acc, i) { | |
var abspath = path.join(start, i); | |
if (fs.statSync(abspath).isDirectory()) { | |
path.walk(abspath, callback); | |
acc.dirs.push(abspath); | |
} else { | |
acc.names.push(abspath); | |
} | |
return acc; | |
}, {"names": [], "dirs": []}); | |
callback(start, coll.dirs, coll.names); | |
}); | |
} else { | |
return null; | |
} | |
}); | |
}; | |
path.walkSync = function (start, callback) { | |
var stat = fs.statSync(start); | |
if (stat.isDirectory()) { | |
var coll = fs.readdirSync(start).reduce(function (acc, i) { | |
var abspath = path.join(start, i); | |
if (fs.statSync(abspath).isDirectory()) { | |
path.walk(abspath, callback); | |
acc.dirs.push(abspath); | |
} else { | |
acc.names.push(abspath); | |
} | |
return acc; | |
}, {"names": [], "dirs": []}); | |
return callback(start, coll.dirs, coll.names); | |
} else { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment