Skip to content

Instantly share code, notes, and snippets.

@tscanlin
Created March 2, 2017 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tscanlin/3b8d020f05e7b37bb95e23190eac323d to your computer and use it in GitHub Desktop.
Save tscanlin/3b8d020f05e7b37bb95e23190eac323d to your computer and use it in GitHub Desktop.
var fs = require('fs')
var path = require('path')
var argCount = process.argv.length
var lastArg = process.argv[argCount - 1]
// From
// http://stackoverflow.com/questions/11194287/convert-a-directory-structure-in-the-filesystem-to-json-with-node-js
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
}
var text = JSON.stringify(dirTree(lastArg))
process.stdout.write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment