├── test ├── fixtures │ ├── file.js │ ├── readme.md │ ├── .hiddenfile │ ├── a │ │ ├── b │ │ │ ├── file_b1.js │ │ │ └── c │ │ │ │ └── file_c1.js │ │ ├── file_a1.js │ │ └── file_a2.js │ └── .hiddendir │ │ └── file.js ├── helpers │ └── index.js └── walk.js ├── .travis.yml ├── .gitignore ├── powerwalker.png ├── .github ├── funding.yml └── workflows │ └── ci.yml ├── appveyor.yml ├── example.js ├── package.json ├── license ├── index.js └── readme.md /test/fixtures/file.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/.hiddenfile: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/a/b/file_b1.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/a/file_a1.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/a/file_a2.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/.hiddendir/file.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/a/b/c/file_c1.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 8 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | package-lock.json 4 | -------------------------------------------------------------------------------- /powerwalker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terkelg/powerwalker/HEAD/powerwalker.png -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: terkelg 4 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | nodejs_version: "8" 3 | 4 | install: 5 | - ps: Install-Product node $env:nodejs_version 6 | - npm install --global npm@latest 7 | - npm install 8 | 9 | test_script: 10 | - node --version 11 | - npm --version 12 | - npm test 13 | 14 | build: off 15 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const { walk, walkSync } = require('./src'); 2 | const path = require('path'); 3 | 4 | (async function() { 5 | let ls = walkSync('../'); 6 | console.log(ls.length) 7 | 8 | ls = await walk('../', { cwd: 'test/fixtures/a/b', relative: true }); 9 | console.log(ls) 10 | 11 | })().catch(console.log); 12 | -------------------------------------------------------------------------------- /test/helpers/index.js: -------------------------------------------------------------------------------- 1 | const isWin = process.platform === 'win32'; 2 | 3 | // unixify path for cross-platform testing 4 | function unixify(arr) { 5 | return isWin ? arr.map(str => { 6 | return Array.isArray(str) ? unixify(str) : str.replace(/\\/g, '/'); 7 | }) : arr; 8 | } 9 | 10 | function toIgnore(str) { 11 | return !str.includes('.DS_Store'); 12 | } 13 | 14 | function order(arr) { 15 | return arr.filter(str => { 16 | return Array.isArray(str) ? order(str) : toIgnore(str); 17 | }).sort(); 18 | } 19 | 20 | module.exports = { unixify, order }; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "powerwalker", 3 | "version": "0.1.2", 4 | "description": "walk directories recursively", 5 | "homepage": "https://github.com/terkelg/powerwalker", 6 | "main": "index.js", 7 | "author": { 8 | "name": "Terkel Gjervig", 9 | "email": "terkel@terkel.com", 10 | "url": "https://terkel.com" 11 | }, 12 | "scripts": { 13 | "test": "tape test/*.js | tap-spec" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/terkelg/powerwalker" 18 | }, 19 | "keywords": [ 20 | "walk", 21 | "walker", 22 | "directories", 23 | "files", 24 | "list", 25 | "dir" 26 | ], 27 | "license": "MIT", 28 | "dependencies": { 29 | "1d": "^0.1.1" 30 | }, 31 | "devDependencies": { 32 | "tap-spec": "^5.0.0", 33 | "tape": "^4.13.2" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: push 4 | 5 | jobs: 6 | test: 7 | name: Node.js v${{ matrix.nodejs }} (${{ matrix.os }}) 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | matrix: 11 | nodejs: [10, 12] 12 | os: [ubuntu-latest] 13 | steps: 14 | - uses: actions/checkout@v1 15 | with: 16 | fetch-depth: 1 17 | 18 | - uses: actions/setup-node@v1 19 | with: 20 | node-version: ${{ matrix.nodejs }} 21 | 22 | - name: Install 23 | run: | 24 | npm install 25 | npm install -g nyc 26 | 27 | - name: Test w/ Coverage 28 | run: npx nyc npm test 29 | 30 | - name: Report 31 | if: matrix.nodejs >= 12 32 | run: | 33 | npx nyc report --reporter=text-lcov > coverage.lcov 34 | bash <(curl -s https://codecov.io/bash) 35 | env: 36 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 37 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Terkel Gjervig Nielsen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const flat = require('1d'); 3 | const path = require('path'); 4 | const { promisify } = require('util'); 5 | 6 | const stat = promisify(fs.lstat); 7 | const readdir = promisify(fs.readdir); 8 | 9 | /** 10 | * List all files in a directory recursively, async 11 | * @param {String} dir Start directory 12 | * @param {Object} [options] Options object 13 | * @param {String} [options.maxdepth=Infinity] Max walker depth 14 | * @param {String} [options.flatten=true] Flatten the output array 15 | * @param {String} [options.relative=true] Use relative or absolute paths 16 | * @param {String} [options.cwd='.'] Define custom current working directory 17 | * @returns {Array} List of files and directories 18 | */ 19 | async function walk(dir, {maxdepth = Infinity, flatten = true, filesonly = false, relative = true, cwd = '.'} = {}) { 20 | const format = file => relative ? path.relative(cwd, file) : file; 21 | async function walker(dir, depth = 0) { 22 | if (dir === '') dir = cwd; 23 | if (depth >= maxdepth) return format(dir); 24 | if ((await stat(dir)).isDirectory()) { 25 | depth++; 26 | const files = await readdir(dir); 27 | const arr = await Promise.all(files.map(async file => walker(path.join(dir, file), depth))); 28 | if (filesonly) return arr 29 | if (cwd === dir && relative) { 30 | const {dir:prev, base} = path.parse(dir); 31 | return arr.concat(path.relative(prev, base) || path.relative(cwd, base)); 32 | } 33 | return arr.concat(format(dir)); 34 | } 35 | return format(dir); 36 | } 37 | const joined = path.isAbsolute(dir) ? dir : path.join(cwd=path.resolve(cwd), dir); 38 | const start = relative ? joined : path.resolve(joined); 39 | return flatten ? flat(await walker(start)) : walker(start); 40 | } 41 | 42 | module.exports = walk; 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
Walk directories recursively
23 | 24 |