node.jsでディレクトリ内のファイル全てに対して処理を回す関数

function eachFiles(filepath, rootPath, callback) {
    if (!rootPath) {
        rootPath = filepath;
    }
    var stat = fs.statSync(filepath);
    if (!stat) {

    } else if (stat.isDirectory()) {
        try {
            var files = fs.readdirSync(filepath);
            if (!files) {

            } else {
                for (var _i in files)(function (i) {
                    var file = files[i];
                    if (filepath.match(/.*\/$/)) {
                        eachFiles(filepath + file, rootPath, callback);
                    } else {
                        eachFiles(filepath + "/" + file, rootPath, callback);
                    }
                }(_i));
            }
        } catch (e1) {
            console.error("Directory " + filepath + " is unreadable.");
        }
    } else if (stat.isFile()) {
        if (callback) {
            callback.call(this, filepath, rootPath);
        }
    } else {
        console.error(filepath + " is not file or directory");
    }
}

使い方

eachFiles("/path/to/show", null, function(filePath, rootPath) {
  console.log(filepath);
});