Created
June 26, 2012 17:37
-
-
Save hagope/2997316 to your computer and use it in GitHub Desktop.
Output the file system as a JSON using node.js
This file contains hidden or 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
var fs = require('fs'), | |
path = require('path') | |
function dirTree(filename) { | |
var stats = fs.lstatSync(filename), | |
info = { | |
path: filename, | |
name: path.basename(filename) | |
}; | |
if (stats.isDirectory()) { | |
info.type = "folder"; | |
info.children = fs.readdirSync(filename).map(function(child) { | |
return dirTree(filename + '/' + child); | |
}); | |
} else { | |
// Assuming it's a file. In real life it could be a symlink or | |
// something else! | |
info.type = "file"; | |
} | |
return info; | |
} | |
if (module.parent == undefined) { | |
// node dirTree.js ~/foo/bar | |
var util = require('util'); | |
console.log(util.inspect(dirTree(process.argv[2]), false, null)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment