├── .gitignore ├── package.json ├── README.md └── bin └── treeplus.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | npm-debug.log* 4 | yarn-debug.log* 5 | yarn-error.log* 6 | /test/unit/coverage/ 7 | /test/e2e/reports/ 8 | selenium-debug.log 9 | .history 10 | 11 | # Editor directories and files 12 | .idea 13 | .vscode 14 | *.svn 15 | *.suo 16 | *.ntvs* 17 | *.njsproj 18 | *.sln -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "treeplus", 3 | "version": "1.1.3", 4 | "description": "treeplus is a node module for creating and manipulating hierarchical tree structures.", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git@github.com:artiely/treeplus.git" 11 | }, 12 | "bin": { 13 | "treeplus": "bin/treeplus.js", 14 | "tp": "bin/treeplus.js" 15 | }, 16 | "engines": { 17 | "node": ">= 0.10" 18 | }, 19 | "readmeFilename": "README.md", 20 | "keywords": [ 21 | "tree", 22 | "treeplus", 23 | "tree structures", 24 | "文件树", 25 | "文件树", 26 | "树目录", 27 | "目录树", 28 | "文件结构", 29 | "项目结构" 30 | ], 31 | "author": "artiely", 32 | "license": "ISC", 33 | "dependencies": { 34 | "commander": "^2.15.0", 35 | "yargs": "^11.0.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > treeplus is a node module for creating and manipulating hierarchical tree structures. 2 | 3 | > 很多时候我们在输出项目结构, 但是我们需要排除一些文件或者只需要文件夹结构,treeplus 就很好的实现了这些。 4 | 5 | # Install 6 | 7 | ```bash 8 | $ npm install treeplus -g 9 | ``` 10 | 11 | # Usage 12 | 13 | ```bash 14 | $ [tp|treeplus] 15 | # or 16 | $ [tp|treeplus] --help 查看帮助 17 | ``` 18 | 19 | # example 20 | 21 | ```bash 22 | $ tp -i node_modules 23 | # 指定参数,排除单个目录 24 | # Returns 25 | 26 | ├──bin 27 | │ └──treeplus.js 28 | ├──package-lock.json 29 | ├──package.json 30 | └──README.md 31 | ``` 32 | 33 | ```bash 34 | # 指定参数,排除多个目录 35 | $ tp -i node_modules bin 36 | 37 | # Returns 38 | 39 | ├──package-lock.json 40 | ├──package.json 41 | └──README.md 42 | ``` 43 | 44 | ```bash 45 | # 指定参数,只打印文件夹 46 | $ tp -d 47 | 48 | # Returns 49 | 50 | ├──bin 51 | ``` 52 | 53 | ```bash 54 | # 指定参数,打印指定的层级,参数大于0 55 | $ tp -l 1 56 | 57 | # Returns 58 | 59 | ├──bin 60 | ``` 61 | 62 | ```bash 63 | # 多参数结合使用 64 | $ tp -d -l 2 -i node_modules 65 | 66 | # Returns 67 | 68 | ├──bin 69 | ``` 70 | 71 | ```bash 72 | $ tp 73 | 74 | # Returns 75 | 76 | ├──bin 77 | │ └──treeplus.js 78 | ├──node_modules 79 | │ ├──.bin 80 | │ │ ├──index.js 81 | │ │ ├──LICENSE 82 | │ │ ├──package.json 83 | │ │ ├──README.md 84 | │ │ └──yargs.js 85 | │ ├──yargs-parser 86 | │ │ ├──lib 87 | │ │ │ └──tokenize-arg-string.js 88 | │ │ ├──CHANGELOG.md 89 | │ │ ├──index.js 90 | │ │ ├──LICENSE.txt 91 | │ │ ├──package.json 92 | │ │ └──README.md 93 | ├──package-lock.json 94 | ├──package.json 95 | └──README.md 96 | ``` 97 | -------------------------------------------------------------------------------- /bin/treeplus.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const argv = require('yargs') 4 | .usage('Usage: tp [options]') 5 | .example('tp -i node_modules', '排除node_modules文件夹') 6 | .alias('i', 'ignore') 7 | .alias('d', 'dir') 8 | .alias('l', 'lev') 9 | .describe('i', '您要排除显示的文件夹') 10 | .describe('d', '只打印文件夹') 11 | .describe('l', '指定打印的层级') 12 | .help('help').argv 13 | const fs = require('fs') 14 | const path = require('path') 15 | const target = path.join(process.cwd()) 16 | 17 | function deep(target, depth, ignore) { 18 | var prefix = new Array(depth + 1).join('│ ') 19 | const dirinfo = fs.readdirSync(target) 20 | var dirs = [] 21 | var files = [] 22 | dirinfo.forEach(info => { 23 | var stats = fs.statSync(path.join(target, info)) 24 | if (stats.isFile()) { 25 | files.push(info) 26 | } else { 27 | dirs.push(info) 28 | } 29 | }) 30 | 31 | if (ignore && ignore.length > 0) { 32 | for (var i = 0; i < ignore.length; i++) { 33 | for (var j = 0; j < dirs.length; j++) { 34 | if (dirs[j] == ignore[i]) { 35 | dirs.splice(j, 1) 36 | j-- 37 | } 38 | } 39 | } 40 | } 41 | 42 | dirs.forEach(dir => { 43 | console.log(`${prefix}├──${dir}`) 44 | // console.log('TCL: argv', argv.l, depth) 45 | if (!argv.l) { 46 | deep(path.join(target, dir), depth + 1) 47 | } else { 48 | if (argv.l > depth + 1) { 49 | deep(path.join(target, dir), depth + 1) 50 | } 51 | } 52 | }) 53 | 54 | var count = files.length - 1 55 | if (!argv.d || !argv.dir) { 56 | files.forEach(file => { 57 | console.log(`${prefix}${count-- ? '├─' : '└─'}─${file}`) 58 | }) 59 | } 60 | } 61 | 62 | if (argv.ignore) { 63 | if (argv.ignore !== true) { 64 | var _ignore = argv._ 65 | _ignore.push(argv.ignore) 66 | deep(target, 0, _ignore) 67 | } else { 68 | deep(target, 0) 69 | } 70 | } else { 71 | deep(target, 0) 72 | } 73 | --------------------------------------------------------------------------------