Created
February 2, 2018 02:26
-
-
Save nkbt/44308e8c7d1afcd9da6f528679615f37 to your computer and use it in GitHub Desktop.
My own slow and unoptimised glob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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