Skip to content

Instantly share code, notes, and snippets.

@evanlucas
Created October 14, 2020 14:00
Show Gist options
  • Save evanlucas/cd9fccb0d8fa2e60485e9702734bb205 to your computer and use it in GitHub Desktop.
Save evanlucas/cd9fccb0d8fa2e60485e9702734bb205 to your computer and use it in GitHub Desktop.
'use strict'
const fs = require('fs').promises
const path = require('path')
async function* getPaths(p, depth = 0) {
const dir = await fs.opendir(p, {
bufferSize: 1
})
for await (const entry of dir) {
if (!entry.isDirectory()) continue
yield entry
if (depth < 2) {
const fp = path.join(p, entry.name)
yield* getPaths(fp, depth + 1)
}
}
}
;(async () => {
for await (const thing of getPaths('/usr/sbin')) {
console.log(thing)
}
})()
.then(() => console.log('done'))
.catch((err) => {
console.error(err)
})
@evanlucas
Copy link
Author

The output of that for me is this:

Dirent { name: 'iRATBW.mlmodelc', [Symbol(type)]: 2 }
Dirent { name: 'model0', [Symbol(type)]: 2 }
Dirent { name: 'model1', [Symbol(type)]: 2 }
Dirent { name: 'authserver', [Symbol(type)]: 2 }
[Error: EACCES: permission denied, opendir '/usr/sbin/authserver'] {
  errno: -13,
  code: 'EACCES',
  syscall: 'opendir',
  path: '/usr/sbin/authserver'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment