fs.walk for Node.js (a port of Go's filepath.Walk)
Go to file
AJ ONeal 3a125de3a5 initial commit 2020-12-08 17:28:11 -07:00
bin initial commit 2020-12-08 17:28:11 -07:00
.eslintrc.json initial commit 2020-12-08 17:28:11 -07:00
.prettierrc.json initial commit 2020-12-08 17:28:11 -07:00
README.md initial commit 2020-12-08 17:28:11 -07:00
create.mjs initial commit 2020-12-08 17:28:11 -07:00
package.json initial commit 2020-12-08 17:28:11 -07:00
walk.mjs initial commit 2020-12-08 17:28:11 -07:00

README.md

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;
}