directory2json

1.0.1 • Public • Published

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)
})

Package Sidebar

Install

npm i directory2json

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

3.52 kB

Total Files

4

Last publish

Collaborators

  • stellasphere