Last active
August 29, 2015 14:08
-
-
Save jmervine/2b2b4dbf28c960021339 to your computer and use it in GitHub Desktop.
JavaScript: tree -- traverse directories.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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