Coder Social home page Coder Social logo

dir-to-json's Introduction

dir-to-json Build Status

Asynchronously convert directory tree structure into a javascript object.

Getting Started

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

API

dirToJson( path [, options ] [, callback ] )

path

  • type: string
  • description: Path to the directory you would like to obtain a tree object from.

options (optional)

  • type: object
  • description: Allows output to be customized.
  • Accepted properties:
    • sortType boolean (Default: true) - If true, directories will be listed before files in all children arrays. If false, array contents will be listed in the order which they are returned from fs.readdir().

callback( err, directoryTree ) (optional)

  • type: function
  • description: Callback function
    • err - Error object on fail. null on success.
    • directoryTree - Object containing heirarchical directory data.

Structure of output

{
	"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"
		}]
	}]
}

dir-to-json's People

Contributors

traviswimer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.