Extract walkDir

master
Frédéric Matte 2025-06-02 18:39:52 -04:00
parent 46282ac834
commit ccd64a4367
2 changed files with 25 additions and 17 deletions

View File

@ -1,21 +1,5 @@
import { readdir } from 'node:fs/promises' import { walkDir } from './walkDir.js'
import { join } from 'node:path' import { join } from 'node:path'
const walkDir = async function* (dir = '.', exclude = []) {
const files = await readdir(dir, { withFileTypes: true })
for (const file of files) {
if (exclude.includes(file.name)) continue
if (file.isDirectory()) {
yield* walkDir(join(dir, file.name))
} else if (file.isSymbolicLink()) {
yield* walkDir(join(dir, file.name))
} else {
yield join(dir, file.name)
}
}
}
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path' import { dirname } from 'node:path'
const getStackAbsolutePaths = () => { const getStackAbsolutePaths = () => {

24
lib/walkDir.js Normal file
View File

@ -0,0 +1,24 @@
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 }