šŸš€ Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

dir-parser

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dir-parser

Parse a directory and generate it's structure tree.

Source
npmnpm
Version
1.1.6
Version published
Weekly downloads
190
75.93%
Maintainers
1
Weekly downloads
Ā 
Created
Source

Dir Parser

npm

Parse a directory and generate it's structure tree.

Quick Start

Demo Image

Dir Parser Demo

Install dir-parser

$ npm install dir-parser -g

Get help

$ parser -h

Usage: parser [options]

Options:
  -V, --version                output the version number
  -v, --version
  -d, --directory [directory]  Target directory, default: "./"
  -o, --output [output]        Output path, default: "./"
  -l, --lineType [lineType]    Line type of tree (dashed | solid), default: solid.
  -e, --excludes [excludes]    Exclude some directories or files by name.
  -x, --excPaths [excPaths]    Exclude some directories or files by path.
  -r, --patterns [patterns]    Exclude some directories or files by RegExp.
  -c, --config [config]        Parser config file.
  -f, --filesFirst             Print files first, before than directories.
  -n, --noNum                  Not show file and directory number.
  -s, --silent                 Not print the parse-result in terminal.
  -h, --help                   output usage information

Parse your dir

$ cd your/demo/app
$ parser

app ( directories: 7, Files: 9 )
 ā”œā”€ bin
 │ └─ www
 ā”œā”€ public
 │ ā”œā”€ images
 │ ā”œā”€ javascripts
 │ └─ stylesheets
 │   └─ style.css
 ā”œā”€ routes
 │ ā”œā”€ index.js
 │ └─ users.js
 ā”œā”€ views
 │ ā”œā”€ error.pug
 │ ā”œā”€ index.pug
 │ └─ layout.pug
 ā”œā”€ app.js
 └─ package.json

Parse your dir with params

$ parser -l dashed -f

app ( directories: 7, Files: 9 )
 +-- app.js
 +-- package.json
 +-- bin
 ¦   +-- www
 +-- public
 ¦   +-- images
 ¦   +-- javascripts
 ¦   +-- stylesheets
 ¦       +-- style.css
 +-- routes
 ¦   +-- index.js
 ¦   +-- users.js
 +-- views
     +-- error.pug
     +-- index.pug
     +-- layout.pug

$ parser -e bin,public -n -s
$ cat dir-info.txt

app
 ā”œā”€ routes
 │ ā”œā”€ index.js
 │ └─ users.js
 ā”œā”€ views
 │ ā”œā”€ error.jade
 │ ā”œā”€ index.jade
 │ └─ layout.jade
 ā”œā”€ app.js
 └─ package.json

Recommend usages

Usage 01 There should no white space in the excludes series! $ parser -e .git,node_modules -x bin/www

Usage 02 There should no white space in the excludes Array! $ parser -e ['.git','node_modules'] -x ['bin/www']

Usage 03 Parse by a config file $ vi parser.conf.json

{
  "filesFirst": false,
  "noNum": false,
  "silent": false,
  "directory": "your/demo/app",
  "output": "your/output/dir",
  "excludes": [ ".git", "node_modules" ]
}

$ parser -c ./parser.conf.json

Use dir-parser in javaScript code

parser(dirPath: string, ptions: Options): Promise<Parsed>

interface Options {
  output?: string;               // path string
  lineType?: 'solid' | 'dashed';
  excludes?: Array<string>;      // eg: [ '.git', 'node_modules', '.idea' ];
  excPaths?: Array<string>;      // eg: [ 'src/app' ];
  patterns?: Array<string>;      // eg: [ 'src/*.js ]';
  filesFirst?: boolean;
  noNum?: boolean;
  files?: boolean;
  children?: boolean;
  dirTree?: boolean;
}

interface Parsed extends DirInfo {
  dirTree: string;
  children: Array<DirInfo|FileInfo>
  files: Array<FileInfo>
}

interface DirInfo {
  name: string = name;
  type: 'directory';
  size: number;
  size_kb: number;
  path: string;
  absPath: string;
  dir: string;
  absDir: string;
  dirNum: number;
  fileNum: number;
  children: Array<DirInfo|FileInfo>
}

interface FileInfo {
  name: string = name;
  base: string = infos.name;
  ext: string = infos.ext;
  type: 'file';
  size: number;
  size_kb: number;
  path: string;
  absPath: string;
  dir: string;
  absDir: string;
}

$ npm install dir-parser funclib
$ vi test.js

  const fn = require('funclib');
  const parser = require('dir-parser');

 /**
  * ============================================================
  * Get parsed dir-tree
  * ============================================================
  */
  const excludes = ['.git', 'dir-info.txt', 'package-lock.json'];
  parser('./', {
    excludes: excludes,
    // lineType: 'dashed',
    // filesFirst: true,
  }).then(parsed => {
    fn.log(parsed.dirTree, '# parsed.dirTree');
    fn.log(fn.pick(parsed, prop => prop !== 'dirTree'), '# parsed result info');
    // fn.log(parsed.children, '# parsed.children');
    // fn.log(parsed.files, '# parsed.files');
  });

$ node test.js

==================================================================
                  [17:06:57] # parsed.dirTree
------------------------------------------------------------------
dir-parser ( directories: 8, Files: 30 )
 ā”œā”€ bin
 │ └─ parser.js
 ā”œā”€ node_modules
 │ ā”œā”€ commander
 │ │ ā”œā”€ typings
 │ │ │ └─ index.d.ts
 │ │ ā”œā”€ CHANGELOG.md
 │ │ ā”œā”€ index.js
 │ │ ā”œā”€ LICENSE
 │ │ ā”œā”€ package.json
 │ │ └─ Readme.md
 │ ā”œā”€ funclib
 │ │ ā”œā”€ funclib.core.js
 │ │ ā”œā”€ funclib.js
 │ │ ā”œā”€ funclib.min.js
 │ │ ā”œā”€ index.d.ts
 │ │ ā”œā”€ index.js
 │ │ ā”œā”€ package.json
 │ │ └─ README.md
 │ └─ progress
 │   ā”œā”€ lib
 │   │ └─ node-progress.js
 │   ā”œā”€ CHANGELOG.md
 │   ā”œā”€ index.js
 │   ā”œā”€ LICENSE
 │   ā”œā”€ Makefile
 │   ā”œā”€ package.json
 │   └─ Readme.md
 ā”œā”€ src
 │ ā”œā”€ base.js
 │ └─ dir-parser.js
 ā”œā”€ .gitignore
 ā”œā”€ dir-parser.png
 ā”œā”€ index.js
 ā”œā”€ package.json
 ā”œā”€ parser.conf.json
 ā”œā”€ README.md
 └─ test.js
==================================================================


==================================================================
                [17:06:57] # parsed result info
------------------------------------------------------------------
{
  "name": "dir-parser",
  "type": "directory",
  "path": "./",
  "absPath": "E:\\work\\code\\dir-parser",
  "dir": ".",
  "absDir": "E:\\work\\code",
  "dirNum": 8,
  "fileNum": 30
}
==================================================================

$ npm install dir-parser funclib
$ vi test.js

  const fn = require('funclib');
  const parser = require('dir-parser');

  /**
   * ============================================================
   * Get parsed dir-info (children & files)
   * ============================================================
   */
  const excludes = ['.git', 'node_modules', 'dir-info.txt', 'package-lock.json'];
  parser('./', {
    excludes: excludes,
    files: true,       // Default is false, If true, returns will conatins an array of all subfiles's info;
    children: true,    // Default is false, If true, returns will conatins an object of all children's info;
    dirTree: false     // Default is true, returns will conatins a tree of the directory;
  }).then(parsed => {
    fn.log(parsed.children, '# parsed.children');
    fn.log(parsed.files, '# parsed.files');
  });

$ node test.js

==================================================================
                  [17:06:57] # parsed.children
------------------------------------------------------------------
[
  {
    "name": "bin",
    "type": "directory",
    "size": 2920,
    "size_kb": "2.85kb",
    "path": "bin",
    "absPath": "E:\\work\\code\\dir-parser\\bin",
    "dir": ".",
    "absDir": "E:\\work\\code\\dir-parser",
    "dirNum": 0,
    "fileNum": 1,
    "children": [
      {
        "name": "parser.js",
        "base": "parser",
        "ext": ".js",
        "type": "file",
        "size": 2920,
        "size_kb": "2.85kb",
        "path": "bin\\parser.js",
        "absPath": "E:\\work\\code\\dir-parser\\bin\\parser.js",
        "dir": "bin",
        "absDir": "E:\\work\\code\\dir-parser\\bin"
      }
    ]
  },
  {
    "name": "src",
    "type": "directory",
    "size": 6488,
    "size_kb": "6.34kb",
    "path": "src",
    "absPath": "E:\\work\\code\\dir-parser\\src",
    "dir": ".",
    "absDir": "E:\\work\\code\\dir-parser",
    "dirNum": 0,
    "fileNum": 2,
    "children": [
      {
        "name": "base.js",
        "base": "base",
        "ext": ".js",
        "type": "file",
        "size": 1038,
        "size_kb": "1.01kb",
        "path": "src\\base.js",
        "absPath": "E:\\work\\code\\dir-parser\\src\\base.js",
        "dir": "src",
        "absDir": "E:\\work\\code\\dir-parser\\src"
      },
      {
        "name": "dir-parser.js",
        "base": "dir-parser",
        "ext": ".js",
        "type": "file",
        "size": 5450,
        "size_kb": "5.32kb",
        "path": "src\\dir-parser.js",
        "absPath": "E:\\work\\code\\dir-parser\\src\\dir-parser.js",
        "dir": "src",
        "absDir": "E:\\work\\code\\dir-parser\\src"
      }
    ]
  },
  {
    "name": ".gitignore",
    "base": ".gitignore",
    "ext": "",
    "type": "file",
    "size": 34,
    "size_kb": "0.03kb",
    "path": ".gitignore",
    "absPath": "E:\\work\\code\\dir-parser\\.gitignore",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "dir-parser.png",
    "base": "dir-parser",
    "ext": ".png",
    "type": "file",
    "size": 76316,
    "size_kb": "74.53kb",
    "path": "dir-parser.png",
    "absPath": "E:\\work\\code\\dir-parser\\dir-parser.png",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "index.js",
    "base": "index",
    "ext": ".js",
    "type": "file",
    "size": 45,
    "size_kb": "0.04kb",
    "path": "index.js",
    "absPath": "E:\\work\\code\\dir-parser\\index.js",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "package.json",
    "base": "package",
    "ext": ".json",
    "type": "file",
    "size": 732,
    "size_kb": "0.71kb",
    "path": "package.json",
    "absPath": "E:\\work\\code\\dir-parser\\package.json",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "parser.conf.json",
    "base": "parser.conf",
    "ext": ".json",
    "type": "file",
    "size": 111,
    "size_kb": "0.11kb",
    "path": "parser.conf.json",
    "absPath": "E:\\work\\code\\dir-parser\\parser.conf.json",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "README.md",
    "base": "README",
    "ext": ".md",
    "type": "file",
    "size": 11467,
    "size_kb": "11.2kb",
    "path": "README.md",
    "absPath": "E:\\work\\code\\dir-parser\\README.md",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "test.js",
    "base": "test",
    "ext": ".js",
    "type": "file",
    "size": 1196,
    "size_kb": "1.17kb",
    "path": "test.js",
    "absPath": "E:\\work\\code\\dir-parser\\test.js",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  }
]
==================================================================


==================================================================
                   [17:06:57] # parsed.files
------------------------------------------------------------------
[
  {
    "name": "parser.js",
    "base": "parser",
    "ext": ".js",
    "type": "file",
    "size": 2920,
    "size_kb": "2.85kb",
    "path": "bin\\parser.js",
    "absPath": "E:\\work\\code\\dir-parser\\bin\\parser.js",
    "dir": "bin",
    "absDir": "E:\\work\\code\\dir-parser\\bin"
  },
  {
    "name": "base.js",
    "base": "base",
    "ext": ".js",
    "type": "file",
    "size": 1038,
    "size_kb": "1.01kb",
    "path": "src\\base.js",
    "absPath": "E:\\work\\code\\dir-parser\\src\\base.js",
    "dir": "src",
    "absDir": "E:\\work\\code\\dir-parser\\src"
  },
  {
    "name": "dir-parser.js",
    "base": "dir-parser",
    "ext": ".js",
    "type": "file",
    "size": 5450,
    "size_kb": "5.32kb",
    "path": "src\\dir-parser.js",
    "absPath": "E:\\work\\code\\dir-parser\\src\\dir-parser.js",
    "dir": "src",
    "absDir": "E:\\work\\code\\dir-parser\\src"
  },
  {
    "name": ".gitignore",
    "base": ".gitignore",
    "ext": "",
    "type": "file",
    "size": 34,
    "size_kb": "0.03kb",
    "path": ".gitignore",
    "absPath": "E:\\work\\code\\dir-parser\\.gitignore",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "dir-parser.png",
    "base": "dir-parser",
    "ext": ".png",
    "type": "file",
    "size": 76316,
    "size_kb": "74.53kb",
    "path": "dir-parser.png",
    "absPath": "E:\\work\\code\\dir-parser\\dir-parser.png",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "index.js",
    "base": "index",
    "ext": ".js",
    "type": "file",
    "size": 45,
    "size_kb": "0.04kb",
    "path": "index.js",
    "absPath": "E:\\work\\code\\dir-parser\\index.js",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "package.json",
    "base": "package",
    "ext": ".json",
    "type": "file",
    "size": 732,
    "size_kb": "0.71kb",
    "path": "package.json",
    "absPath": "E:\\work\\code\\dir-parser\\package.json",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "parser.conf.json",
    "base": "parser.conf",
    "ext": ".json",
    "type": "file",
    "size": 111,
    "size_kb": "0.11kb",
    "path": "parser.conf.json",
    "absPath": "E:\\work\\code\\dir-parser\\parser.conf.json",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "README.md",
    "base": "README",
    "ext": ".md",
    "type": "file",
    "size": 11467,
    "size_kb": "11.2kb",
    "path": "README.md",
    "absPath": "E:\\work\\code\\dir-parser\\README.md",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  },
  {
    "name": "test.js",
    "base": "test",
    "ext": ".js",
    "type": "file",
    "size": 1196,
    "size_kb": "1.17kb",
    "path": "test.js",
    "absPath": "E:\\work\\code\\dir-parser\\test.js",
    "dir": "",
    "absDir": "E:\\work\\code\\dir-parser"
  }
]
==================================================================

Keywords

dir-parser

FAQs

Package last updated on 24 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts