1.0.0 • Published 5 years ago

dirtree2json v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

dirtree2json

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

Installation

npm install dirtree2json --save

API

Usage

var dirtree2json = require('dirtree2json');
var result = dirtree2json.dirTojson(path [,options]);

Options

OptionTypeDefaultDescription
attributeName.absolutePathstring"absolutePath"overwrites the name of the 'absolute path' attribute
attributeName.childstring"child"overwrites the name of the 'children' attribute
attributeName.creationTimestring"creationTime"overwrites the name of the 'creation time' attribute
attributeName.dirFlagstring"isDir"overwrites the name of the 'is directory flag' attribute
attributeName.extensionstring"ext"overwrites the name of the 'extension' attribute
attributeName.fileNamestring"name"overwrites the name of the 'file name' attribute
attributeName.modificationTimestring"modificationTime"overwrites the name of the 'modification time' attribute
attributeName.pathstring"path"overwrites the name of the 'path' attribute
attributeName.sizestring"size"overwrites the name of the 'size' attribute
excludeEmptyFoldersbooleanfalseexclude all folders without a file. This is also excludes folders with just empty folders
filter.fileExtensionregex or stringnullfilter files by file extension name
filter.fileNameregex or stringnullfilter files by name
filter.folderNameregex or stringnullfilter folders by name
includeAbsolutePathbooleanfalseinclude the absolute path of the folder/file
includeCreationTimebooleanfalseinclude the creation time of the folder/file
includeDirFlagbooleantrueinclude is directory flag
includeExtensionbooleantrueinclude the extension of the file
includeModificationTimebooleanfalseinclude the modification time of the folder/file
includeSizebooleanfalseinclude the size of the folder/file
rootNamestringname of the relative pathoverwrites the name of the root folder
rootPathstringrelative path of the folderoverwrites 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

var dirtree2json = require('dirtree2json');

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

var result = dirtree2json.dirTojson(path, options);

Result:

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

var dirtree2json = require('dirtree2json');

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

var result = dirtree2json.dirTojson(path, options);

Result:

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

var dirtree2json = require('dirtree2json');

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

var result = dirtree2json.dirTojson(path, options);

Result:

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

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:

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