directory2json
v1.0.1
Published
Creates a JSON file with file tree information from a directory.
Downloads
6
Maintainers
Readme
directory2json
Creates a JSON file with file tree information from a directory.
Usage
API
directory2json( directory, options, callback )
The function is a asynchronous function, with a optional callback function. Both will return the same output.
directory
The relative file path to generate a JSON file tree from.
options (optional)
Currently, the only option is debug
(true/false)
Returns a directory object of the directory referenced in the function arguments
Reference
Directory Object
{
"directory":"examplefolder",
"files":{
"*File Name*": *File Object*
},
"folders":{
"*Folder Name*": *Directory Object*
},
"allfiles":[
*Array of file or directory objects*
],
"type":"folder"
}
File Object
{
"directory":"examplefolder/image.png",
"filename":"image.png",
"parentpath":"examplefolder",
"isfile":true,
"isfolder":false,
"type":"file"
}
Example
const directory2json = require("directory2json");
// Use `await` in a async function
init()
async function init() {
var json = await directory2json("directory")
console.log(json) // prints into console
const fs = require("fs")
fs.writeFile("directory.json",JSON.stringify(json),function(err){
console.log(err)
})
}
// Or use a callback
directory2json("directory",undefined,function(json) {
console.log(json)
})