Created
March 8, 2017 17:52
-
-
Save jrop/91a96353d4f67a5de488c61f641c37e0 to your computer and use it in GitHub Desktop.
async readdir (Node.JS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flatten = require('flatten') | |
const fs = require('mz/fs') | |
const path = require('path') | |
async function isDirectory(f) { | |
return (await fs.stat(f)).isDirectory() | |
} | |
async function readdir(filePath) { | |
const files = await Promise.all((await fs.readdir(filePath)).map(async f => { | |
const fullPath = path.join(filePath, f) | |
return (await isDirectory(fullPath)) ? await readdir(fullPath) : fullPath | |
})) | |
return flatten(files) | |
} | |
readdir('.') | |
.then(files => { | |
files.forEach(f => console.log(f)) | |
}) | |
.catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flatten = require('flatten') | |
const fs = require('fs') | |
const path = require('path') | |
function isDirectory(f, callback) { | |
fs.stat(f, function (err, stat) { | |
if (err) return callback(err) | |
callback(null, stat.isDirectory()) | |
}) | |
} | |
function readdir(filePath, callback) { | |
let allFiles = [] | |
let overallError = null | |
fs.readdir(filePath, function (err, files) { | |
if (err) return callback(err) | |
let pending = 0 | |
files.forEach(function (file) { | |
++pending | |
const fullPath = path.join(filePath, file) | |
isDirectory(fullPath, function (err, isDir) { | |
if (err) { | |
overallError = overallError || err | |
--pending | |
return finish() | |
} | |
if (isDir) { | |
readdir(fullPath, function (err, files) { | |
--pending | |
if (err) { | |
overallError = overallError || err | |
return finish() | |
} | |
allFiles.push(files) | |
finish() | |
}) | |
} else { | |
--pending | |
allFiles.push(fullPath) | |
finish() | |
} | |
}) | |
}) | |
function finish() { | |
if (finish.called || pending) return | |
if (overallError) { | |
finish.called = true | |
return callback(overallError) | |
} | |
finish.called = true | |
callback(null, flatten(allFiles)) | |
} | |
finish.called = false | |
finish() | |
}) | |
} | |
readdir('.', function (err, files) { | |
if (err) { | |
console.error(err.stack) | |
process.exitCode = 1 | |
return | |
} | |
files.forEach(f => console.log(f)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const async = require('async') | |
const flatten = require('flatten') | |
const fs = require('fs') | |
const path = require('path') | |
function isDirectory(f, callback) { | |
fs.stat(f, function (err, stat) { | |
if (err) return callback(err) | |
callback(null, stat.isDirectory()) | |
}) | |
} | |
function readdir(filePath, callback) { | |
const allFiles = [] | |
async.waterfall([ | |
done => fs.readdir(filePath, done), | |
(files, done) => | |
async.map(files, function recurseOnFile(file, done) { | |
const fullPath = path.join(filePath, file) | |
isDirectory(fullPath, (e, isDir) => { | |
if (e) return done(e) | |
isDir ? readdir(fullPath, done) : done(null, fullPath) | |
}) | |
}, done), | |
(allFiles, done) => done(null, flatten(allFiles)), | |
], function (err, files) { | |
callback(err, files) | |
}) | |
} | |
readdir('.', function (err, results) { | |
if (err) { | |
console.error(err.stack) | |
process.exitCode = 1 | |
return | |
} | |
results.forEach(f => console.log(f)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flatten = require('flatten') | |
const fs = require('mz/fs') | |
const path = require('path') | |
function isDirectory(f) { | |
return fs.stat(f).then(stat => stat.isDirectory()) | |
} | |
function readdir(filePath) { | |
return fs.readdir(filePath) | |
.then(files => { | |
return Promise.all(files.map(file => { | |
const fullPath = path.join(filePath, file) | |
return isDirectory(fullPath) | |
.then(isDir => isDir ? readdir(fullPath) : fullPath) | |
})) | |
}) | |
.then(files => flatten(files)) | |
} | |
readdir('.') | |
.then(files => { | |
files.forEach(f => console.log(f)) | |
}) | |
.catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment