Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active August 29, 2015 14:08
Show Gist options
  • Save jmervine/2b2b4dbf28c960021339 to your computer and use it in GitHub Desktop.
Save jmervine/2b2b4dbf28c960021339 to your computer and use it in GitHub Desktop.
JavaScript: tree -- traverse directories.
function tree(loc) {
// windows support
var seg = loc.split(path.sep);
var diskLoc, realLoc;
if (seg[0] === 'public') {
diskLoc = seg.join(path.sep);
realLoc = seg.slice(1).join(path.sep);
} else {
diskLoc = path.join('public', seg.join(path.sep));
realLoc = seg.join(path.sep);
}
var files = fs.readdirSync(diskLoc);
files.forEach(function(file) {
var fileLoc = path.join(diskLoc, file);
var stats = fs.statSync(fileLoc);
if (stats.isDirectory()) {
tree(fileLoc);
} else {
targets.push(path.join(realLoc, file));
}
});
}
tree(p);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment