├── .npmrc ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .editorconfig ├── test.js ├── license ├── package.json ├── readme.md └── index.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | .yo-rc.* 4 | yarn.lock 5 | .nyc_output 6 | coverage 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | script: 5 | - npm test 6 | after_script: 7 | - './node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov && ./node_modules/.bin/codecov' 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test('list this project', async t => { 5 | let res = await m(__dirname, {match:["*node_modules"],ignore:["*.git"]}) 6 | t.is(!!res.length, true) 7 | }); 8 | 9 | test('list this project', async t => { 10 | let res = await m(undefined, {match:["*node_modules"],ignore:["*.git"]}) 11 | t.is(!!res.length, true) 12 | }); 13 | 14 | test('list this project', async t => { 15 | let res = await m(__dirname, {match:"*node_modules", ignore:"*.git"}) 16 | t.is(!!res.length, true) 17 | }); 18 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) chinanf-boy <865501259@qq.com> (llever.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dirs-list", 3 | "version": "0.0.6", 4 | "description": "get dir path list", 5 | "license": "MIT", 6 | "main": "dist/dirs-list.js", 7 | "module": "dist/dirs-list.m.js", 8 | "repository": "chinanf-boy/dirs-list", 9 | "author": { 10 | "name": "chinanf-boy", 11 | "email": "865501259@qq.com", 12 | "url": "llever.com" 13 | }, 14 | "engines": { 15 | "node": ">=4" 16 | }, 17 | "scripts": { 18 | "test": "nyc ava -v", 19 | "dev": "microbundle watch", 20 | "precommit": "lint-staged", 21 | "prepublish": "npm run build", 22 | "build": "microbundle", 23 | "pub": "npm run test && npm run npmUp && npm run git", 24 | "npmUp": "npm version patch && npm publish", 25 | "git": "git push && git push --tags" 26 | }, 27 | "size-limit": [ 28 | { 29 | "path": "dist/dirs-list.js", 30 | "gzip": false 31 | } 32 | ], 33 | "lint-staged": { 34 | "*.{js,json,css,md}": [ 35 | "prettier --single-quote --trailing-comma es5 --write", 36 | "git add" 37 | ] 38 | }, 39 | "files": [], 40 | "keywords": [ 41 | "" 42 | ], 43 | "dependencies": { 44 | "matcher": "^1.1.1", 45 | "mz": "^2.7.0" 46 | }, 47 | "devDependencies": { 48 | "codecov": "^3.0.2", 49 | "nyc": "^11.8.0", 50 | "lint-staged": "^7.0.0", 51 | "microbundle": "^0.4.4", 52 | "prettier": "^1.12.1", 53 | "ava": "^0.25.0" 54 | }, 55 | "nyc": { 56 | "reporter": [ 57 | "lcov", 58 | "text" 59 | ] 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # dirs-list [![Build Status](https://travis-ci.org/chinanf-boy/dirs-list.svg?branch=master)](https://travis-ci.org/chinanf-boy/dirs-list) [![codecov](https://codecov.io/gh/chinanf-boy/dirs-list/badge.svg?branch=master)](https://codecov.io/gh/chinanf-boy/dirs-list?branch=master) 2 | 3 | > get dir path list 4 | 5 | [中文](./readme.md) | ~~[english](./readme.en.md)~~ 6 | 7 | ## Install 8 | 9 | 10 | 11 | ``` 12 | npm install dirs-list 13 | ``` 14 | 15 | ``` 16 | yarn add dirs-list 17 | ``` 18 | 19 | 20 | 21 | 22 | ## Usage 23 | 24 | ```js 25 | const dirsList = require('dirs-list'); 26 | 27 | let res = await dirsList(__dirname, {match:["*node_modules"],ignore:["*.git"]}) 28 | //=> __dirname all node_modules, ignore *.git 29 | ``` 30 | 31 | 32 | ## API 33 | 34 | ### dirsList(dir, options) 35 | 36 | #### dir 37 | 38 | name: | dir 39 | ---------|---------- 40 | Type: | `string` 41 | Desc: | dir path 42 | 43 | #### options 44 | 45 | ##### match 46 | 47 | name: | match 48 | ---------|---------- 49 | Type: | `string`\|`Array` 50 | Desc: | match every, add the path 51 | 52 | - [use matcher](https://github.com/sindresorhus/matcher) 53 | 54 | ##### ignore 55 | 56 | name: | ignore 57 | ---------|---------- 58 | Type: | `string`\|`Array` 59 | Desc: | ignore some, pass the path 60 | 61 | - [use matcher](https://github.com/sindresorhus/matcher) 62 | 63 | ## use by 64 | 65 | - [node-modules-size](https://github.com/chinanf-boy/node-modules-size) get all node_modules size 66 | 67 | 68 | 69 | 70 | 71 | ## License 72 | 73 | MIT © [chinanf-boy](http://llever.com) 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('mz/fs') 3 | const matcher = require('matcher'); 4 | const path = require('path') 5 | let allRunPath = 0 6 | 7 | const readMdir = async (Dir) => { 8 | 9 | var dirlist = await fs.readdir(Dir, 'utf8').then(files => files).catch(err => err) 10 | return dirlist 11 | 12 | } 13 | 14 | /** 15 | * @description get contentDir dir path 16 | * @param {string} contentDir 17 | * @param {string|Array} options.match 18 | * @param {string|Array} options.ignore 19 | * @param {Array} output = [] 20 | * @returns output 21 | */ 22 | const Listmd = async (contentDir, options, output = []) => { 23 | contentDir = contentDir || process.cwd() 24 | 25 | let match = options.match || [] 26 | let ignore = options.ignore || [] 27 | 28 | if (typeof match == 'string') { 29 | let m1 = match 30 | match = [] 31 | match.push(m1) 32 | } 33 | if (typeof ignore == 'string') { 34 | let i1 = ignore 35 | ignore = [] 36 | ignore.push(i1) 37 | } 38 | 39 | 40 | let input = [] 41 | 42 | let isFile = await fs.lstat(contentDir).then(x => x.isFile()) 43 | 44 | if (isFile) { 45 | input.push(path.basename(contentDir)) 46 | contentDir = path.dirname(contentDir) 47 | } else { 48 | 49 | 50 | if (!contentDir.endsWith('/')) { 51 | contentDir += '/' 52 | } 53 | input = await readMdir(contentDir) 54 | } 55 | 56 | if (input instanceof Error) 57 | return Promise.reject(input) 58 | 59 | while (input.length) { 60 | allRunPath++ 61 | let path_string = input.shift() 62 | let absp = path.resolve(contentDir, path_string) 63 | let isDir = await fs.lstat(absp).then(x => x.isDirectory()) 64 | 65 | let m = match.every(m => { 66 | return matcher.isMatch(absp, m) 67 | }) 68 | 69 | let ignorePath = ignore.some(i =>{ 70 | return matcher.isMatch(absp, i) 71 | }) 72 | 73 | if (isDir && m) { 74 | output.push(absp) 75 | } else if (isDir) { 76 | 77 | if(!ignorePath){ 78 | await Listmd(absp, options, output) 79 | } 80 | 81 | } 82 | } 83 | 84 | return Promise.resolve(output) 85 | 86 | } 87 | 88 | module.exports = Listmd 89 | --------------------------------------------------------------------------------