Skip to content

Instantly share code, notes, and snippets.

@GeKiStolyarov
Created April 30, 2021 05:04
Show Gist options
  • Save GeKiStolyarov/fe5f8c2afb926685ffdee7badfc994db to your computer and use it in GitHub Desktop.
Save GeKiStolyarov/fe5f8c2afb926685ffdee7badfc994db to your computer and use it in GitHub Desktop.
Find files recursive using only node.js
import { resolve, extname, basename } from 'path';
import fsPromises from 'fs/promises';
// @ts-ignore
async function* getFiles(dir: string) {
const dirents = await fsPromises.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
yield* getFiles(res);
} else {
yield res;
}
}
}
(async () => {
for await (const f of getFiles('./packages/')) {
const isNotNodeModules = !f.includes('node_modules');
const isMDFiles = extname(f) === '.md' || extname(f) === '.MD';
const isChangelogFile = basename(f).toUpperCase() === 'CHANGELOG.MD';
if (isNotNodeModules && isMDFiles && isChangelogFile) {
console.log(f);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment