Skip to content

Instantly share code, notes, and snippets.

Forked from Raynos/0usage.js
Created May 13, 2012 10:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Iterate all the files
var fs = require("fs"),
path = require("path");
function iterateFiles(uri, callback, done, fileRegexp) {
var counter = 1
fs.readdir(uri, errorProxy(done, readFiles))
function readFiles(err, files) {
counter += files.length
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), fileRegexp)
} else if (stat.isFile() && (!fileRegexp || fileRegexp.test(fileName))) {
} else {
function next() {
if (--counter === 0) {
function errorProxy(errorHandler, callback) {
return proxy
function proxy(err) {
if (err) {
return errorHandler(err)
return callback.apply(this, arguments)
var iterateFiles = require("fileIterator"),
path = require("path")
// Load all javascript files in the test folder or any of their sub folders
iterateFiles(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
}, /.js$/)
Copy link

Updated not to default to .js but simply allow all files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment