Skip to main content
Travis Wimer
Directory to json

dir-to-json

dir-to-json

NPM package for asynchronously converting a directory tree structure into a JavaScript object.

Published / By Travis Wimer
Last updated

View project on GitHub

About this Project

dir-to-json is an NPM package for asynchronously converting a directory tree structure into a JavaScript object.

I first published this package back in 2014 and I recently updated it to add TypeScript support and cleaner code.

It's a simple API:

import dirToJson from "dir-to-json";
async function run() {
	const dirTree = await dirToJson("./path/to/my/dir");
	console.log(JSON.stringify(dirTree, null, "\t"));
}
run();

This would be expected to log something like this:

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

Tech Stack

  • NPM - A package manager for Node.js.
  • NodeJS - A JavaScript runtime for use-cases outside of a browser environment.
  • TypeScript - A strongly typed programming language that builds on JavaScript.
  • Jest - A JavaScript testing framework.

Be the first to know when I post articles like this.

Follow Me On LinkedIn