All Projects → bcomnes → async-folder-walker

bcomnes / async-folder-walker

Licence: MIT License
A recursive async iterator of the files and directories in a given directory. Can take multiple directories and files, limit walk depth and filter based on path names and stat results.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to async-folder-walker

readdir
Recursively read a directory, blazing fast. Use with picomatch or micromatch to match globs.
Stars: ✭ 37 (+105.56%)
Mutual labels:  fs, dir
export-size
Analysis bundle cost for each export of a package
Stars: ✭ 90 (+400%)
Mutual labels:  esm
webpacker
🔸 Webpack configuration manager
Stars: ✭ 18 (+0%)
Mutual labels:  esm
pythonwhat
Verify Python code submissions and auto-generate meaningful feedback messages.
Stars: ✭ 55 (+205.56%)
Mutual labels:  fs
anzip
Simple async unzip library for Node.js
Stars: ✭ 61 (+238.89%)
Mutual labels:  esm
watcher
The file system watcher that strives for perfection, with no native dependencies and optional rename detection support.
Stars: ✭ 37 (+105.56%)
Mutual labels:  fs
PowerColorLS
PowerShell script to display a colorized directory and file listing with icons
Stars: ✭ 35 (+94.44%)
Mutual labels:  dir
bash-glob
Bash-powered globbing for node.js. Alternative to node-glob. Does not work on Windows 9 and lower.
Stars: ✭ 13 (-27.78%)
Mutual labels:  fs
proSeed
ProSeed modification for Farming Simulator 2022
Stars: ✭ 63 (+250%)
Mutual labels:  fs
pate
Modern and light-fast CLI app that scans all the desired files of a certain folder and returns a list with the name of the files which contain a certain RegEx pattern
Stars: ✭ 23 (+27.78%)
Mutual labels:  fs
npm-es-modules
Breakdown of 7 different ways to use ES modules with npm today.
Stars: ✭ 67 (+272.22%)
Mutual labels:  esm
vue-fs
A Vue file management client, complete with a node/express/FS backend.
Stars: ✭ 40 (+122.22%)
Mutual labels:  fs
copy
Copy files using glob patterns. Sync, async, promise or streams. (node.js utility)
Stars: ✭ 84 (+366.67%)
Mutual labels:  fs
python-ls
Think about Python's dir builtin with recursive search capabilities
Stars: ✭ 44 (+144.44%)
Mutual labels:  dir
greenwood
Greenwood is your workbench for the web, focused on supporting modern web standards and development to help you create your next project.
Stars: ✭ 48 (+166.67%)
Mutual labels:  esm
xv
❌ ✔️ zero-config test runner for simple projects
Stars: ✭ 588 (+3166.67%)
Mutual labels:  esm
fs
Reactive filesystem powered by @vue/reactivity
Stars: ✭ 45 (+150%)
Mutual labels:  fs
SimpleOS
Operating System Coded in Assembly and C
Stars: ✭ 72 (+300%)
Mutual labels:  fs
gof
Yet another simple Go filesystem wrapper
Stars: ✭ 13 (-27.78%)
Mutual labels:  fs
korefile
File System API for Local/GitHub.
Stars: ✭ 29 (+61.11%)
Mutual labels:  fs

async-folder-walker

Actions Status

A recursive async iterator of the files and directories in a given folder. Can take multiple folders, limit walk depth and filter based on path names and stat results.

Installation

npm install async-folder-walker

Usage

const { asyncFolderWalker, allFiles } = require('async-folder-walker')

async function iterateFiles () {
  const walker = asyncFolderWalker(['.git', 'node_modules'])
  for await (const file of walker) {
    console.log(file) // logs the file path!
  }
}

async function getAllFiles () {
  const allFilepaths = await allFiles(['.git', 'node_modules'])
  console.log(allFilepaths)
}

iterateFiles().then(() => getAllFiles())

API

const { asyncFolderWalker, allFiles } = require('async-folder-walker')

Import asyncFolderWalker or allFiles.

async-gen = asyncFolderWalker(paths, [opts])

Return an async generator that will iterate over all of files inside of a directory. paths can be a string path or an Array of string paths.

You can iterate over each file and directory individually using a for-await...of loop. Note, you must be inside an async function statement.

const { asyncFolderWalker } = require('async-folder-walker')
async function iterateFiles () {
  const walker = asyncFolderWalker(['.git', 'node_modules'])
  for await (const file of walker) {
    console.log(file) // logs the file path!
  }
}

iterateFiles()

Opts include:

{
  fs: require('fs'),
  pathFilter: filepath => true,
  statFilter st => true,
  ignore: [],
  maxDepth: Infinity,
  shaper: ({ root, filepath, stat, relname, basename }) => filepath
}

The pathFilter function allows you to filter files from additional async stat operations. Return false to filter the file.

{ // exclude node_modules
  pathFilter: filepath => !filepath.includes(node_modules)
}

The statFilter function allows you to filter files based on the internal stat operation. Return false to filter the file.

{ // exclude all directories:
  statFilter: st => !st.isDirectory()
}

The ignore option can be a string or or array of strings that should follow gitignore style ignore strings. Files and directories that match are ignored.

{ // Ignore node_modules at any depth and any file starting with a .
  ['node_modules', '.*']
}

The shaper function lets you change the shape of the returned value based on data accumulaed during the iteration. To return the same shape as okdistribute/folder-walker use the following function:

{ // Return the same shape as folder-walker
  shaper: fwData => fwData
}

Example of a fwData object for a directory:

{
  root: '/Users/bret/repos/async-folder-walker/fixtures',
  filepath: '/Users/bret/repos/async-folder-walker/fixtures/sub-folder/sub-sub-folder',
  stat: Stats {
    dev: 16777220,
    mode: 16877,
    nlink: 3,
    uid: 501,
    gid: 20,
    rdev: 0,
    blksize: 4096,
    ino: 30244023,
    size: 96,
    blocks: 0,
    atimeMs: 1574381262779.8396,
    mtimeMs: 1574380914743.5474,
    ctimeMs: 1574380914743.5474,
    birthtimeMs: 1574380905232.5996,
    atime: 2019-11-22T00:07:42.780Z,
    mtime: 2019-11-22T00:01:54.744Z,
    ctime: 2019-11-22T00:01:54.744Z,
    birthtime: 2019-11-22T00:01:45.233Z
  },
  relname: 'sub-folder/sub-sub-folder',
  basename: 'sub-sub-folder'
}

and another example for a file on windows:

{
  root: 'D:\\a\\async-folder-walker\\async-folder-walker\\fixtures',
  filepath: 'D:\\a\\async-folder-walker\\async-folder-walker\\fixtures\\sub-folder\\sub-sub-folder\\sub-sub-folder-file.json',
  stat: Stats {
    dev: 1321874112,
    mode: 33206,
    nlink: 1,
    uid: 0,
    gid: 0,
    rdev: 0,
    blksize: 4096,
    ino: 562949953421580,
    size: 37,
    blocks: 0,
    atimeMs: 1577476819530.035,
    mtimeMs: 1577476819530.035,
    ctimeMs: 1577476819530.035,
    birthtimeMs: 1577476819530.035,
    atime: 2019-12-27T20:00:19.530Z,
    mtime: 2019-12-27T20:00:19.530Z,
    ctime: 2019-12-27T20:00:19.530Z,
    birthtime: 2019-12-27T20:00:19.530Z
  },
  relname: 'sub-folder\\sub-sub-folder\\sub-sub-folder-file.json',
  basename: 'sub-sub-folder-file.json'
}

The stat property is an instance of fs.Stats so it has extra methods not listed here.

files = await allFiles(paths, [opts])

Get an Array of all files inside of a directory. paths can be a single string path or an array of string paths.

opts Is the same as asyncFolderWalker.

See also

This module is effectivly a rewrite of okdistribute/folder-walker using async generators instead of Node streams, and a few tweaks to the underlying options to make the results a bit more flexible.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].