add fun dirForEach

This commit is contained in:
jiaming743 2019-07-05 16:18:40 +08:00
parent 98536da7d1
commit 084f4c4c9c
1 changed files with 32 additions and 1 deletions

View File

@ -309,6 +309,36 @@ async function writeFile (src, string, encoding = 'utf8') {
}) })
} }
async function dirForEach (src, callback) {
if (!src || !callback) {
console.error('dirForEach missing parameters!')
return false
}
const paths = await readDir(src)
if (!paths) {
console.error('Exception in dirForEach: paths!')
return false
}
for (let i = 0; i < paths.length; i++) {
const fullSrc = src + '/' + paths[i]
const stats = await stat(fullSrc)
if (!stats) {
console.error('Exception in dirForEach: stats!')
return false
}
if (stats.isDirectory()) await callback(fullSrc)
}
return true
}
module.exports = { module.exports = {
readDir, readDir,
stat, stat,
@ -319,5 +349,6 @@ module.exports = {
copyDir, copyDir,
fileForEach, fileForEach,
readFile, readFile,
writeFile writeFile,
dirForEach
} }