Skip to content

Instantly share code, notes, and snippets.

@voznik
Forked from qwtel/getFiles.js
Created February 13, 2019 16:17
Show Gist options
  • Save voznik/af45a77321ecb0dec51dc670318d2274 to your computer and use it in GitHub Desktop.
Save voznik/af45a77321ecb0dec51dc670318d2274 to your computer and use it in GitHub Desktop.
[node.js 8+] Recursively get all files in a directory
const { promisify } = require('util');
const { resolve } = require('path');
const fs = require('fs');
const readdir = promisify(fs.readdir);
const rename = promisify(fs.rename);
const stat = promisify(fs.stat);
async function getFiles(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getFiles(res) : res;
}));
return files.reduce((a, f) => a.concat(f), []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment