Skip to content

Instantly share code, notes, and snippets.

@Marak
Forked from cloudhead/gist:395526
Created May 10, 2010 00:21
Show Gist options
  • Save Marak/395527 to your computer and use it in GitHub Desktop.
Save Marak/395527 to your computer and use it in GitHub Desktop.
//
// Recursively traverse a hierarchy, returning
// a list of all relevant .js files.
//
function paths(dir) {
var paths = [];
try { fs.statSync(dir) }
catch (e) { return [] }
(function traverse(dir, stack) {
stack.push(dir);
fs.readdirSync(stack.join('/')).forEach(function (file) {
var path = stack.concat([file]).join('/'),
stat = fs.statSync(path);
if (file[0] == '.' || file === 'vendor') {
return;
} else if (stat.isFile() && /\.js$/.test(file)) {
paths.push(path);
} else if (stat.isDirectory()) {
traverse(file, stack);
}
});
stack.pop();
})(dir || '.', []);
return paths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment