Skip to content

Instantly share code, notes, and snippets.

@hagope
Created June 26, 2012 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagope/2997316 to your computer and use it in GitHub Desktop.
Save hagope/2997316 to your computer and use it in GitHub Desktop.
Output the file system as a JSON using node.js
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