Parse a directory and generate it's structure tree.
Read this in other languages: English | 简体中文
- dir-parser
👍👍👍Dir parser is a powerful folder analysis tool based on nodejs, which can be used in command line or JavaScript code. There are many practical parameters that can be set to help you get the formatted folder tree and internal information.
- yarn:
$ yarn global add dir-parser
- npm:
$ npm install -g dir-parser
- yarn:
$ yarn add dir-parser
or$ yarn add dir-parser -D
- npm:
$ npm install dir-parser
or$ npm install dir-parser -D
$ parser -h
(or: $ parser --help
)
Usage: parser [options]
Options:
-V, --version output the version number
-v, --version output the version number
-c, --config [config] config file, Optional.
-i, --input <input> target directory (default: "./")
-o, --output <output> output path (default: "./")
-d, --depth <depth> depth of a parse process, 0 means no limit (default: 0)
-l, --lineType <lineType> line type of tree, "dash" or "solid" (default: "solid")
-e, --excludes <excludes..> exclude some directories or files by name.
-x, --excPaths <excPaths..> exclude directories or files by path.
-p, --patterns <patterns...> filter directories or files by RegExp.
-g, --generate [fileName] generate a dir-info file to the output path, "dir-info.txt" is default.
-r, --reverse reverse the parsed dir-tree nodes.
-s, --silent not show the parsed dir-tree in terminal.
-f, --fileFirst print files first, before directories.
-F, --fileOnly pase files only.
-D, --dirOnly pase directories only, and it only takes effect when fileOnly is false.
-I, --ignores <ignores..> ignore some directories or files by name.
-N, --no-dirInfo hide file and directory number info on the result top.
-G, --glob <glob> filter files with glob patterns.
--paths <paths..> filter directories or files by path.
--includes <includes..> filter directories or files by name.
--excPatterns <excPatterns...> exclude directories or files by RegExp.
-H, --Help output chinese usage information.(打印中文帮助信息.)
-h, --help output usage information
To run demo, you need to install express-generator
:
Run:
$ npm install -g express-generator
$ express myapp
$ cd myapp
$ parser
myapp ( Directories: 7, Files: 9 )
├── bin
│ └── www
├── public
│ ├── images/
│ ├── javascripts/
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
└── package.json
👉 Exclude some directories or files by name.
$ # git init
$ npm install
$ parser -e .git,node_modules,public
or: $ parser --excludes .git,node_modules,public
myapp ( Directories: 3, Files: 9 )
├── bin
│ └── www
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package-lock.json
└── package.json
Name has white space:
$ touch 'white space.txt'
$ parser -e '[".git", "node_modules", "public", "white space.txt"]'
$ rm -rf white\ space.txt
myapp ( Directories: 3, Files: 9 )
├── bin
│ └── www
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package-lock.json
└── package.json
👉 Ignore some directories or files by name.
$ parser -e node_modules -I bin,public
or: $ parser -e node_modules --ignores bin,public
myapp ( Directories: 4, Files: 8 )
├── bin/
├── public/
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package-lock.json
└── package.json
👉 Filter by glob pattern. Note: glob pattern must be enclosed in quotation marks
$ parser -e node_modules -G '**/*.js'
or: $ parser -e node_modules --glob '**/*.js'
myapp ( Directories: 1, Files: 3 )
├── routes
│ ├── index.js
│ └── users.js
└── app.js
👉 Filter directories or files by RegExp.
$ parser -e node_modules -p .js$
or: $ parser -e node_modules --patterns .js$
myapp ( Directories: 1, Files: 3 )
├── routes
│ ├── index.js
│ └── users.js
└── app.js
👉 Line type of tree, "dash" or "solid" (default: "solid")
$ parser -e bin,node_modules -l dash
or: $ parser -e bin,node_modules --lineType dash
myapp ( Directories: 6, Files: 9 )
+-- public
¦ +-- images/
¦ +-- javascripts/
¦ +-- stylesheets
¦ +-- style.css
+-- routes
¦ +-- index.js
¦ +-- users.js
+-- views
¦ +-- error.jade
¦ +-- index.jade
¦ +-- layout.jade
+-- app.js
+-- package-lock.json
+-- package.json
👉 Depth of a parse process, 0 means no limit (default: 0)
$ parser -e node_modules,views -d 2
or: $ parser -e node_modules,views --depth 2
myapp ( Directories: 6, Files: 6 )
├── bin
│ └── www
├── public
│ ├── images/
│ ├── javascripts/
│ └── stylesheets/*
├── routes
│ ├── index.js
│ └── users.js
├── app.js
├── package-lock.json
└── package.json
👉 Reverse the parsed dir-tree nodes.
$ parser -e node_modules,views -d 2 -r
or: $ parser -e node_modules,views -d 2 --reverse
myapp ( Directories: 6, Files: 6 )
├── routes
│ ├── users.js
│ └── index.js
├── public
│ ├── stylesheets/*
│ ├── javascripts/
│ └── images/
├── bin
│ └── www
├── package.json
├── package-lock.json
└── app.js
👉 Print files first, before directories.
$ parser -e node_modules,bin,views -f
or: $ parser -e node_modules,bin,views --fileFirst
myapp ( Directories: 5, Files: 6 )
├── app.js
├── package-lock.json
├── package.json
├── public
│ ├── images/
│ ├── javascripts/
│ └── stylesheets
│ └── style.css
└── routes
├── index.js
└── users.js
👉 Pase files only.
$ parser -e node_modules,bin,views -F
or: $ parser -e node_modules,bin,views --fileOnly
myapp ( Directories: 3, Files: 6 )
├── public
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
├── app.js
├── package-lock.json
└── package.json
👉 Pase directories only, and it only takes effect when fileOnly is false.
$ parser -e node_modules,bin,views -D
or: $ parser -e node_modules,bin,views --dirOnly
myapp ( Directories: 5 )
├── public
│ ├── images/
│ ├── javascripts/
│ └── stylesheets/
└── routes/
👉 Hide file and directory number info on the result top.
$ parser -e node_modules,bin,public -N
or: $ parser -e node_modules,bin,public --no-dirInfo
myapp
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package-lock.json
└── package.json
👉 Exclude directories or files by path.
$ parser -e node_modules,bin -x myapp/public
or: $ parser -e node_modules,bin -excPath myapp/public
myapp ( Directories: 2, Files: 8 )
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package-lock.json
└── package.json
👉 Exclude directories or files by RegExp.
$ parser -e node_modules,bin --excPatterns .jade$,.css$
myapp ( Directories: 6, Files: 5 )
├── public
│ ├── images/
│ ├── javascripts/
│ └── stylesheets/
├── routes
│ ├── index.js
│ └── users.js
├── views/
├── app.js
├── package-lock.json
└── package.json
👉 Not show the parsed dir-tree in terminal.
$ parser -e node_modules,bin,public -s
or: $ parser -e node_modules,bin,public --silent
👉 Generate a dir-info file to the output path, "dir-info.txt" is default.
$ parser -e node_modules,bin,public -sg
or: $ parser -e node_modules,bin,public -s --generate
$ cat dir-info.txt
myapp ( Directories: 2, Files: 8 )
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package-lock.json
└── package.json
👉 Config file, Optional.
$ touch parser.conf.json
$ vi parser.conf.json
{
"directory": "./",
"excludes": [ ".git", "node_modules", "bin", "public", "parser.conf.json" ],
"depth": "2",
"generate": "info.txt"
}
$ parser -c ./parser.conf.json
myapp ( Directories: 2, Files: 9 )
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── dir-info.txt
├── package-lock.json
└── package.json
parser -e node_modules,bin -I views -d 2 -Nr
myapp
├── views/
├── routes
│ ├── users.js
│ └── index.js
├── public
│ ├── stylesheets/*
│ ├── javascripts/
│ └── images/
├── parser.conf.json
├── package.json
├── package-lock.json
├── info.txt
├── dir-info.txt
└── app.js
parser(dirPath: string, options: Options): Promise<Parsed>
interface Options {
depth?: number;
reverse?: boolean;
fileFirst?: boolean;
fileOnly?: boolean;
dirOnly?: boolean;
getFiles?: boolean;
getChildren?: boolean;
dirTree?: boolean; // default: true
dirInfo?: boolean; // default: true
lineType?: 'solid' | 'dash'; // default: 'solid'
excludes?: Array<string>; // eg: [ '.git', 'node_modules', '.idea' ];
excPaths?: Array<string>; // eg: [ 'src/app' ];
excPatterns?: Array<string>; // eg: [ 'src/*.js ]';
ignores: Array<string>; // eg: [ 'public' ];
includes: Array<string>; // eg: [ 'app.js' ];
paths?: Array<string>; // eg: [ 'src/public' ];
patterns?: Array<string>; // eg: [ '*.js ]';
glob?: string; // eg: '**/*.js';
}
interface Parsed extends DirInfo {
dirTree: string;
children: Array<DirInfo | FileInfo>
files: Array<FileInfo>
}
interface DirInfo {
name: string;
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;
base: string;
ext: string;
type: 'file';
size: number;
size_kb: number;
path: string;
absPath: string;
dir: string;
absDir: string;
}
$ npm install dir-parser funclib
$ touch test.js
$ vi test.js
const fn = require('funclib');
const parser = require('dir-parser');
parser('./', {
excludes: ['.git', 'node_modules'],
// lineType: 'dash',
// fileFirst: 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
==================================================================
[09:48:55] # parsed.dirTree
------------------------------------------------------------------
myapp
├── bin
│ └── www
├── public
│ ├── images/
│ ├── javascripts/
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── dir-info.txt
├── info.txt
├── package-lock.json
├── package.json
├── parser.conf.json
└── test.js
==================================================================
$ vi test.js
parser('./', {
excludes: ['.git', 'node_modules'],
// lineType: 'dash',
// fileFirst: true,
}).then(parsed => {
console.log(fn.pretty(fn.pick(parsed, prop => prop !== 'dirTree')));
// fn.log(parsed.dirTree, '# parsed.dirTree');
// fn.log(parsed.children, '# parsed.children');
// fn.log(parsed.files, '# parsed.files');
});
$ node test.js
{
"name": "myapp",
"type": "directory",
"path": "./",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp",
"dir": ".",
"absDir": "/Users/cntower/code/@cntower/dir-parser",
"dirNum": 7,
"fileNum": 14
}
$ vi test.js
parser('./', {
excludes: ['.git', 'node_modules', 'public'],
getFiles: true, // Default is false, If true, returns will conatins an array of all subfiles's info;
getChildren: 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 => {
console.log(fn.pretty(parsed.children));
// fn.log(parsed.files, '# parsed.files');
});
$ node test.js
[
{
"name": "bin",
"type": "directory",
"size": 1591,
"size_kb": "1.55kb",
"path": "bin",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/bin",
"dir": ".",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp",
"dirNum": 0,
"fileNum": 1,
"children": [
{
"name": "www",
"base": "www",
"ext": "",
"type": "file",
"size": 1591,
"size_kb": "1.55kb",
"path": "bin/www",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/bin/www",
"dir": "bin",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/bin"
}
]
},
{
"name": "routes",
"type": "directory",
"size": 408,
"size_kb": "0.4kb",
"path": "routes",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/routes",
"dir": ".",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp",
"dirNum": 0,
"fileNum": 2,
"children": [
{
"name": "index.js",
"base": "index",
"ext": ".js",
"type": "file",
"size": 205,
"size_kb": "0.2kb",
"path": "routes/index.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/routes/index.js",
"dir": "routes",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/routes"
},
{
"name": "users.js",
"base": "users",
"ext": ".js",
"type": "file",
"size": 203,
"size_kb": "0.2kb",
"path": "routes/users.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/routes/users.js",
"dir": "routes",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/routes"
}
]
},
{
"name": "app.js",
"base": "app",
"ext": ".js",
"type": "file",
"size": 1075,
"size_kb": "1.05kb",
"path": "app.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/app.js",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "dir-info.txt",
"base": "dir-info",
"ext": ".txt",
"type": "file",
"size": 277,
"size_kb": "0.27kb",
"path": "dir-info.txt",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/dir-info.txt",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "info.txt",
"base": "info",
"ext": ".txt",
"type": "file",
"size": 301,
"size_kb": "0.29kb",
"path": "info.txt",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/info.txt",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "package-lock.json",
"base": "package-lock",
"ext": ".json",
"type": "file",
"size": 68550,
"size_kb": "66.94kb",
"path": "package-lock.json",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/package-lock.json",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "package.json",
"base": "package",
"ext": ".json",
"type": "file",
"size": 347,
"size_kb": "0.34kb",
"path": "package.json",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/package.json",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "parser.conf.json",
"base": "parser.conf",
"ext": ".json",
"type": "file",
"size": 145,
"size_kb": "0.14kb",
"path": "parser.conf.json",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/parser.conf.json",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "test.js",
"base": "test",
"ext": ".js",
"type": "file",
"size": 554,
"size_kb": "0.54kb",
"path": "test.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/test.js",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
}
]
$ vi test.js
parser('./', {
excludes: ['.git', 'node_modules', 'public'],
getFiles: true,
getChildren: true,
dirTree: false
}).then(parsed => {
// fn.log(parsed.children, '# parsed.children');
console.log(fn.pretty(parsed.files));
});
$ node test.js
[
{
"name": "www",
"base": "www",
"ext": "",
"type": "file",
"size": 1591,
"size_kb": "1.55kb",
"path": "bin/www",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/bin/www",
"dir": "bin",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/bin"
},
{
"name": "index.js",
"base": "index",
"ext": ".js",
"type": "file",
"size": 205,
"size_kb": "0.2kb",
"path": "routes/index.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/routes/index.js",
"dir": "routes",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/routes"
},
{
"name": "users.js",
"base": "users",
"ext": ".js",
"type": "file",
"size": 203,
"size_kb": "0.2kb",
"path": "routes/users.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/routes/users.js",
"dir": "routes",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/routes"
},
{
"name": "error.jade",
"base": "error",
"ext": ".jade",
"type": "file",
"size": 84,
"size_kb": "0.08kb",
"path": "views/error.jade",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/views/error.jade",
"dir": "views",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/views"
},
{
"name": "index.jade",
"base": "index",
"ext": ".jade",
"type": "file",
"size": 66,
"size_kb": "0.06kb",
"path": "views/index.jade",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/views/index.jade",
"dir": "views",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/views"
},
{
"name": "layout.jade",
"base": "layout",
"ext": ".jade",
"type": "file",
"size": 125,
"size_kb": "0.12kb",
"path": "views/layout.jade",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/views/layout.jade",
"dir": "views",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp/views"
},
{
"name": "app.js",
"base": "app",
"ext": ".js",
"type": "file",
"size": 1075,
"size_kb": "1.05kb",
"path": "app.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/app.js",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "dir-info.txt",
"base": "dir-info",
"ext": ".txt",
"type": "file",
"size": 277,
"size_kb": "0.27kb",
"path": "dir-info.txt",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/dir-info.txt",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "info.txt",
"base": "info",
"ext": ".txt",
"type": "file",
"size": 301,
"size_kb": "0.29kb",
"path": "info.txt",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/info.txt",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "package-lock.json",
"base": "package-lock",
"ext": ".json",
"type": "file",
"size": 68550,
"size_kb": "66.94kb",
"path": "package-lock.json",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/package-lock.json",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "package.json",
"base": "package",
"ext": ".json",
"type": "file",
"size": 347,
"size_kb": "0.34kb",
"path": "package.json",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/package.json",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "parser.conf.json",
"base": "parser.conf",
"ext": ".json",
"type": "file",
"size": 145,
"size_kb": "0.14kb",
"path": "parser.conf.json",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/parser.conf.json",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
},
{
"name": "test.js",
"base": "test",
"ext": ".js",
"type": "file",
"size": 303,
"size_kb": "0.3kb",
"path": "test.js",
"absPath": "/Users/cntower/code/@cntower/dir-parser/myapp/test.js",
"dir": "",
"absDir": "/Users/cntower/code/@cntower/dir-parser/myapp"
}
]