npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-folder-walker

v3.0.5

Published

An async iterator for walking directories, with helpful options

Downloads

336

Readme

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