Created
March 22, 2018 10:27
-
-
Save 360disrupt/fc4a40bf7c66aadd9b446c1919ecad9b to your computer and use it in GitHub Desktop.
Creates a Json of a Folder Structure
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
#async version of: http://researchhubs.com/post/computing/javascript/convert-directory-structure--to-json-with-node-js.html | |
inspect = require('util').inspect | |
chalk = require('chalk') | |
debug = require('debug')('helper:folderStructure') | |
fs = require('fs') | |
path = require('path') | |
async = require('async') | |
self = @ | |
exports.directoryToJson = (directoryName, callback) -> | |
fs.lstat directoryName, (err, stats)-> | |
info = | |
path: directoryName | |
name: path.basename(directoryName) | |
if stats.isDirectory() | |
info.type = 'folder' | |
fs.readdir directoryName, (err, directories)-> | |
return callback err if err? | |
async.map directories, (child, next)-> | |
self.directoryToJson directoryName + '/' + child, (err, childInfo)-> | |
return next err, childInfo | |
, (err, childInfos)-> | |
return callback err if err? | |
info.children = childInfos | |
debug info | |
return callback null, info | |
else | |
info.type = 'file' | |
debug info | |
return callback null, info |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment