24 lines
553 B
JavaScript
24 lines
553 B
JavaScript
import { readdir, stat } from 'fs/promises'
|
|
import { join } from 'path/posix'
|
|
|
|
const walkDir = async function* (dir = '.', exclude = []) {
|
|
const files = await readdir(dir)
|
|
|
|
for await (const file of files) {
|
|
if (exclude.includes(file)) continue
|
|
|
|
try {
|
|
const stats = await stat(join(dir, file))
|
|
|
|
if (stats.isDirectory()) {
|
|
yield* walkDir(join(dir, file))
|
|
} else {
|
|
yield join(dir, file)
|
|
}
|
|
}
|
|
catch (err) { } //Like file not found when symlink to a file that do not exist
|
|
}
|
|
}
|
|
|
|
|
|
export { walkDir } |