Skip to content

Instantly share code, notes, and snippets.

@quantumsheep
Forked from kethinov/walksync.js
Created October 15, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantumsheep/9bf7375c6f71d1ea2d982301b8822731 to your computer and use it in GitHub Desktop.
Save quantumsheep/9bf7375c6f71d1ea2d982301b8822731 to your computer and use it in GitHub Desktop.
List all files in a directory in Node.js recursively in a synchronous fashion
// List all files in a directory in Node.js recursively in an asynchronous fashion
const fs = require('fs').promises;
const path = require('path');
const walk = async (dir, filelist = []) => {
const files = await fs.readdir(dir);
for (file of files) {
const filepath = path.join(dir, file);
const stat = await fs.stat(filepath);
if (stat.isDirectory()) {
filelist = await walk(filepath, filelist);
} else {
filelist.push(file);
}
}
return filelist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment