Asynchronously convert directory tree structure into a javascript object.
Install with NPM:
npm install dir-to-json --save
Use in your project like this:
var dirToJson = require('dir-to-json');
dirToJson( "./path/to/my/dir", function( err, dirTree ){
if( err ){
throw err;
}else{
console.log( dirTree );
}
});
// If you prefer, you can also use promises
dirToJson( "./path/to/my/dir" )
.then( function( dirTree ){
console.log( dirTree );
})
.catch( function( err ){
throw err;
});
dirToJson( path [, options ] [, callback ] )
- type: string
- description: Path to the directory you would like to obtain a tree object from.
- type: object
- description: Allows output to be customized.
- Accepted properties:
- sortType boolean (Default:
true
) - Iftrue
, directories will be listed before files in allchildren
arrays. Iffalse
, array contents will be listed in the order which they are returned fromfs.readdir()
.
- sortType boolean (Default:
- type: function
- description: Callback function
- err - Error object on fail.
null
on success. - directoryTree - Object containing heirarchical directory data.
- err - Error object on fail.
{
"parent": "..",
"path": "",
"name": "coverage",
"type": "directory",
"children": [{
"parent": "",
"path": "coverage-final.json",
"name": "coverage-final.json",
"type": "file"
}, {
"parent": "",
"path": "index.html",
"name": "index.html",
"type": "file"
}, {
"parent": "",
"path": "lcov-report",
"name": "lcov-report",
"type": "directory",
"children": [{
"parent": "lcov-report",
"path": "lcov-report/index.html",
"name": "index.html",
"type": "file"
}, {
"parent": "lcov-report",
"path": "lcov-report/prettify.css",
"name": "prettify.css",
"type": "file"
}, {
"parent": "lcov-report",
"path": "lcov-report/prettify.js",
"name": "prettify.js",
"type": "file"
}, {
"parent": "lcov-report",
"path": "lcov-report/src",
"name": "src",
"type": "directory",
"children": [{
"parent": "lcov-report/src",
"path": "lcov-report/src/createDirectoryObject.js.html",
"name": "createDirectoryObject.js.html",
"type": "file"
}, {
"parent": "lcov-report/src",
"path": "lcov-report/src/index.html",
"name": "index.html",
"type": "file"
}, {
"parent": "lcov-report/src",
"path": "lcov-report/src/main.js.html",
"name": "main.js.html",
"type": "file"
}]
}]
}, {
"parent": "",
"path": "lcov.info",
"name": "lcov.info",
"type": "file"
}, {
"parent": "",
"path": "prettify.css",
"name": "prettify.css",
"type": "file"
}, {
"parent": "",
"path": "prettify.js",
"name": "prettify.js",
"type": "file"
}, {
"parent": "",
"path": "src",
"name": "src",
"type": "directory",
"children": [{
"parent": "src",
"path": "src/createDirectoryObject.js.html",
"name": "createDirectoryObject.js.html",
"type": "file"
}, {
"parent": "src",
"path": "src/index.html",
"name": "index.html",
"type": "file"
}, {
"parent": "src",
"path": "src/main.js.html",
"name": "main.js.html",
"type": "file"
}]
}]
}