├── .gitignore ├── example.js ├── README.md ├── package.json ├── index.js └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const tree = require('./') 2 | const ite = tree('.') 3 | 4 | ite.next(function loop (err, node) { 5 | if (err) throw err 6 | if (!node) return // done 7 | console.log('entry', node.path, node.stat) 8 | ite.next(loop) 9 | }) 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fs-tree-iterator 2 | 3 | Basic recursive file tree [nanoiterator](https://github.com/mafintosh/nanoiterator). 4 | 5 | ``` 6 | npm install fs-tree-iterator 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` js 12 | const tree = require('fs-tree-iterator') 13 | 14 | const ite = tree('.') 15 | 16 | ite.next(function loop (err, node) { 17 | if (err) throw err 18 | if (!node) return // done 19 | console.log('entry', node.path, node.stat) 20 | ite.next(loop) 21 | }) 22 | ``` 23 | 24 | ## License 25 | 26 | MIT 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fs-tree-iterator", 3 | "version": "1.1.0", 4 | "description": "Recursive file tree nanoiterator", 5 | "main": "index.js", 6 | "dependencies": { 7 | "nanoiterator": "^1.2.0" 8 | }, 9 | "devDependencies": { 10 | "standard": "^12.0.1" 11 | }, 12 | "scripts": { 13 | "test": "standard" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/mafintosh/fs-tree-iterator.git" 18 | }, 19 | "author": "Mathias Buus (@mafintosh)", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/mafintosh/fs-tree-iterator/issues" 23 | }, 24 | "homepage": "https://github.com/mafintosh/fs-tree-iterator" 25 | } 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const nanoiterator = require('nanoiterator') 2 | const path = require('path') 3 | const fs = require('fs') 4 | 5 | module.exports = tree 6 | 7 | function tree (folder) { 8 | const stack = [ folder ] 9 | return nanoiterator({ next }) 10 | 11 | function next (cb) { 12 | if (stack.length === 0) return cb(null, null) 13 | const top = stack.pop() 14 | fs.lstat(top, function (_, st) { 15 | if (st && st.isDirectory()) return ondir(top, st, cb) 16 | if (!st) return next(cb) 17 | cb(null, { path: top, stat: st }) 18 | }) 19 | } 20 | 21 | function ondir (top, st, cb) { 22 | fs.readdir(top, function (err, entries) { 23 | if (err) return cb(err) 24 | for (let i = entries.length - 1; i >= 0; i--) stack.push(path.join(top, entries[i])) 25 | cb(null, { path: top, stat: st }) 26 | }) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Mathias Buus 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 13 | all 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 21 | THE SOFTWARE. 22 | --------------------------------------------------------------------------------