Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Iterate all the files
var iterateFiles = require("iterateFiles"),
path = require("path")
// Load all files in the test folder or any of their sub folders
iterateFile(path.join(process.cwd(), "./test"), function (fileName) {
// run code for each recursively found js file
}, function (err) {
// run code when all files have been found recursively
})
var fs = require("fs"),
path = require("path"),
isJsFile = /.js$/
function iterateFiles(uri, callback, done) {
var counter = 1
fs.readdir(uri, errorProxy(done, readFiles))
function readFiles(err, files) {
counter += files.length
files.forEach(isDirOrFile)
next()
}
function isDirOrFile(fileName) {
fileName = path.join(uri, fileName)
fs.stat(fileName, errorProxy(done, readOrRecurse))
function readOrRecurse(err, stat) {
if (stat.isDirectory()) {
iterateFiles(fileName, callback, errorProxy(done, next))
} else if (stat.isFile() && isJsFile.test(fileName)) {
callback(fileName)
next()
} else {
next()
}
}
}
function next() {
if (--counter === 0) {
done(null)
}
}
}
function errorProxy(errorHandler, callback) {
return proxy
function proxy(err) {
if (err) {
return errorHandler(err)
}
return callback.apply(this, arguments)
}
}
@tommedema

This comment has been minimized.

Copy link

commented May 13, 2012

I think you should pass the error to the done callback, not the per file callback.

@Raynos

This comment has been minimized.

Copy link
Owner Author

commented May 13, 2012

Really not sure what's ideal in this situation.

@tommedema

This comment has been minimized.

Copy link

commented May 13, 2012

Abort on error and call done callback with this error?

@tommedema

This comment has been minimized.

Copy link

commented May 13, 2012

On line 30 you must check for an error passed to next.

@Raynos

This comment has been minimized.

Copy link
Owner Author

commented May 13, 2012

Thanks, fixed the error handling to be better

@tommedema

This comment has been minimized.

Copy link

commented May 13, 2012

Nice, two comments:

  1. I don't think you should hard code the regexp. What if people want to require coffee script files or whatever? Or maybe all files ending with .run.js. You could add an optional regexp parameter which defaults to the one you have hardcoded now when not supplied.
  2. Wouldn't it be more descriptive to rename error() to errorProxy()? At first I didn't realize it was a proxy.
@Raynos

This comment has been minimized.

Copy link
Owner Author

commented May 13, 2012

  1. People who want coffee can go die. Not adding complexity to the code for the sake of having flexibility on the regexp.
@tommedema

This comment has been minimized.

Copy link

commented May 13, 2012

In that case you should rename iterateFiles to iterateJSFiles or whatever? Actually a much better argument is that people may want to iterate over non-js files, such as .txt files or whatever.

@tommedema

This comment has been minimized.

Copy link

commented May 13, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.