Skip to content

Instantly share code, notes, and snippets.

@danneu
Created November 9, 2017 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/d38f43357564025b0d1a6140a88d1b51 to your computer and use it in GitHub Desktop.
Save danneu/d38f43357564025b0d1a6140a88d1b51 to your computer and use it in GitHub Desktop.
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