walk.js/README.md

1.4 KiB

Walk.js (@root/walk)

A port of Go's filepath.Walk for Node.js v10+ (which introduced fs.readdir withFileTypes).

await Walk.walk(rootpath, walkFunc);

Example

import Walk from "walk";

Walk.walk("./", function (err, pathname, dirent) {
  if (err) {
    throw err;
  }

  // ignore dot files
  if (dirent.name.startsWith(".")) {
    return Walk.skipDir;
  }
});

API Documentation

Walk.walk walks rootpath (inclusive) and calls walkFunc for each file system entry.

It can be used with Promises:

Walk.walk(rootpath, promiseWalker).then(doMore);

Or with async / await:

await Walk.walk(rootpath, asyncWalker);

The behavior should exactly match Go's filepath.Walk with 3 exceptions:

  • uses JavaScript Promises/async/await
  • receives dirent rather than lstat (for performance)

walkFunc

Handles each directory entry

function walker(err, pathname, dirent) {
  // `err` is a file system stat error
  // `pathname` is the full pathname, including the file name
  // `dirent` is an fs.Dirent with a `name`, `isDirectory`, `isFile`, etc
  return null;
}