-
-
Save danneu/d38f43357564025b0d1a6140a88d1b51 to your computer and use it in GitHub Desktop.
This file contains 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 fs = require('fs') | |
const { join } = require('path') | |
module.exports = { crawlSync, crawlAsync } | |
function crawlSync(path) { | |
let cache = new Map() | |
const stats = fs.statSync(path) // <-- A | |
if (stats.isFile()) { | |
cache.set(path, stats) | |
} else if (stats.isDirectory()) { | |
for (const childPath of fs.readdirSync(path)) { // <-- B | |
cache = new Map([...cache, ...crawlSync(join(path, childPath))]) | |
} | |
} | |
return cache | |
} | |
async function crawlAsync(path) { | |
let cache = new Map() | |
const stats = await fs.stat(path) // <-- A | |
if (stats.isFile()) { | |
cache.set(path, stats) | |
} else if (stats.isDirectory()) { | |
for (const childPath of await fs.readdir(path)) { // <-- B | |
cache = new Map([...cache, ...(await crawlAsync(join(path, childPath)))]) | |
} | |
} | |
return cache | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment