Created
March 2, 2017 06:01
-
-
Save tscanlin/3b8d020f05e7b37bb95e23190eac323d to your computer and use it in GitHub Desktop.
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') | |
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