Skip to content

Instantly share code, notes, and snippets.

@nkbt
Created February 2, 2018 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nkbt/44308e8c7d1afcd9da6f528679615f37 to your computer and use it in GitHub Desktop.
Save nkbt/44308e8c7d1afcd9da6f528679615f37 to your computer and use it in GitHub Desktop.
My own slow and unoptimised glob
const process = require('process');
const fs = require('fs');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const realpath = util.promisify(fs.realpath);
const stat = util.promisify(fs.stat);
const ls = async (cwd, paths = []) => {
for (let p of (await readdir(cwd))) {
const rp = await realpath(`${cwd}/${p}`);
paths.push(rp);
if ((await stat(rp)).isDirectory()) {
await ls(rp, paths);
}
}
return paths;
};
const glob = async (cwd, regexp) => (await ls(cwd)).filter(p => p.match(regexp));
(async () => console.log(await glob(process.cwd(), /package.json$/)))();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment