# dirtree2json

> Transform a directory into a json

Latest version **1.0.0** (published 2019-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install dirtree2json
pnpm add dirtree2json
yarn add dirtree2json
bun add dirtree2json
```

Provides the command `dirtree2json`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-01-09 |
| First published | 2017-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 31.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Alisson Marques Alvarenga |
| Maintainers | alissonmbr |
| Keywords | directory, tree, folder, json |

## Links

- npm: https://www.npmjs.com/package/dirtree2json
- Repository: https://github.com/alissonmbr/dirtree2json
- Homepage: https://github.com/alissonmbr/dirtree2json#readme
- Issues: https://github.com/alissonmbr/dirtree2json/issues
- npm.io page: https://npm.io/package/dirtree2json

## Dependencies (2)

- [command-line-args](https://npm.io/package/command-line-args.md) ^5.0.2
- [command-line-usage](https://npm.io/package/command-line-usage.md) ^5.0.5

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-01-09
- 0.0.2 — 2017-03-06
- 0.0.1 — 2017-03-05

## README

# dirtree2json
Simple, flexible lib to convert a directory tree into a json.

# Installation
```
npm install dirtree2json --save
```

# API
## Usage
``` javascript
var dirtree2json = require('dirtree2json');
var result = dirtree2json.dirTojson(path [,options]);
```

## Options

| Option | Type | Default | Description |
|------|:----:|:-------:|-----------|
|attributeName.absolutePath | string | "absolutePath" | overwrites the name of the 'absolute path' attribute|
|attributeName.child | string | "child" | overwrites the name of the 'children' attribute|
|attributeName.creationTime | string | "creationTime" | overwrites the name of the 'creation time' attribute|
|attributeName.dirFlag | string | "isDir" | overwrites the name of the 'is directory flag' attribute|
|attributeName.extension | string | "ext" | overwrites the name of the 'extension' attribute|
|attributeName.fileName | string | "name" | overwrites the name of the 'file name' attribute|
|attributeName.modificationTime | string | "modificationTime" | overwrites the name of the 'modification time' attribute|
|attributeName.path | string | "path" | overwrites the name of the 'path' attribute|
|attributeName.size | string | "size" | overwrites the name of the 'size' attribute|
|excludeEmptyFolders | boolean | false | exclude all folders without a file. This is also excludes folders with just empty folders|
|filter.fileExtension | regex or string | null | filter files by file extension name|
|filter.fileName | regex or string | null | filter files by name|
|filter.folderName | regex or string | null | filter folders by name|
|includeAbsolutePath | boolean | false | include the absolute path of the folder/file |
|includeCreationTime | boolean | false | include the creation time of the folder/file|
|includeDirFlag | boolean | true | include is directory flag|
|includeExtension | boolean | true | include the extension of the file|
|includeModificationTime | boolean | false | include the modification time of the folder/file|
|includeSize | boolean | false | include the size of the folder/file|
|rootName | string | name of the relative path | overwrites the name of the root folder|
|rootPath | string | relative path of the folder | overwrites the path attribute of the  the root|

## Examples
All examples use this folder structure
```
testDir
├── dir1
│   ├── css1.css
│   ├── file1.txt
│   ├── file2.txt
│   ├── index.html
│   └── js1.js
├── dir2
│   ├── file1.txt
│   └── file2.txt
├── dir3
└── file1.txt
```
### Example 1: Include size, extension and exclude isDir flag
``` javascript
var dirtree2json = require('dirtree2json');

var path = __dirname + '/test/testDir';
var options = {
    includeSize: true,
    includeDirFlag: false,
    includeExtension: true
};

var result = dirtree2json.dirTojson(path, options);
```
Result:
``` javascript
{
  "ext": "",
  "path": "testDir",
  "name": "testDir",
  "child": [
    {
      "ext": "",
      "path": "testDir/dir1",
      "name": "dir1",
      "child": [
        {
          "ext": "css",
          "path": "testDir/dir1/css1.css",
          "name": "css1.css",
          "size": 0
        },
        {
          "ext": "txt",
          "path": "testDir/dir1/file1.txt",
          "name": "file1.txt",
          "size": 10
        },
        {
          "ext": "txt",
          "path": "testDir/dir1/file2.txt",
          "name": "file2.txt",
          "size": 11
        },
        {
          "ext": "html",
          "path": "testDir/dir1/index.html",
          "name": "index.html",
          "size": 0
        },
        {
          "ext": "js",
          "path": "testDir/dir1/js1.js",
          "name": "js1.js",
          "size": 0
        }
      ],
      "size": 21
    },
    {
      "ext": "",
      "path": "testDir/dir2",
      "name": "dir2",
      "child": [
        {
          "ext": "txt",
          "path": "testDir/dir2/file1.txt",
          "name": "file1.txt",
          "size": 10
        },
        {
          "ext": "txt",
          "path": "testDir/dir2/file2.txt",
          "name": "file2.txt",
          "size": 11
        }
      ],
      "size": 21
    },
    {
      "ext": "",
      "path": "testDir/dir3",
      "name": "dir3",
      "child": [],
      "size": 0
    },
    {
      "ext": "txt",
      "path": "testDir/file1.txt",
      "name": "file1.txt",
      "size": 13
    }
  ],
  "size": 55
}
```

### Example 2: Override the root path and name
``` javascript
var dirtree2json = require('dirtree2json');

var path = __dirname + '/test/testDir';
var options = {
    rootPath: ".",
    rootName: "root"
};

var result = dirtree2json.dirTojson(path, options);
```
Result:
``` javascript
{
  "ext": "",
  "isDir": true,
  "path": ".",
  "name": "root",
  "child": [
    {
      "ext": "",
      "path": "./dir1",
      "name": "dir1",
      "isDir": true,
      "child": [...]
    },
    ...
  ]
}
```

### Example 3: Filter by file name and exclude empty folders
``` javascript
var dirtree2json = require('dirtree2json');

var path = __dirname + '/test/testDir';
var options = {
    filter: {
        fileExtension: /html/i
    },
    excludeEmptyFolders: true
};

var result = dirtree2json.dirTojson(path, options);
```
Result:
``` javascript
{
  "ext": "",
  "isDir": true,
  "path": "testDir",
  "name": "testDir",
  "child": [
    {
      "ext": "",
      "path": "testDir/dir1",
      "name": "dir1",
      "isDir": true,
      "child": [
        {
          "ext": "html",
          "path": "testDir/dir1/index.html",
          "name": "index.html",
          "isDir": false
        }
      ]
    }
  ]
}
```

### Example 4: Override attribute names
``` javascript
var dirtree2json = require('dirtree2json');

var path = __dirname + '/test/testDir';
var options = {
    attributeName: {
        path: "relativePath",
        fileName: "folderFileName",
        extension: "extension",
        dirFlag: "isFolder",
        child: "children"
    }
};

var result = dirtree2json.dirTojson(path, options);
```
Result:
``` javascript
{
  "extension": "",
  "isFolder": true,
  "relativePath": "testDir",
  "folderFileName": "testDir",
  "children": [
    {
      "extension": "",
      "relativePath": "testDir/dir1",
      "folderFileName": "dir1",
      "isFolder": true,
      "children": [
        {
          "extension": "css",
          "relativePath": "testDir/dir1/css1.css",
          "folderFileName": "css1.css",
          "isFolder": false
        },
        ...
      ]
    },
    ...
  ]
}

```

# CLI

---
_Source: https://npm.io/package/dirtree2json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
