Skip to content

Instantly share code, notes, and snippets.

@itshaadi
Created July 1, 2017 14:03
Show Gist options
  • Save itshaadi/50289d0010c90bd882cf9cd08becd255 to your computer and use it in GitHub Desktop.
Save itshaadi/50289d0010c90bd882cf9cd08becd255 to your computer and use it in GitHub Desktop.
list all files in a recursive way
module.exports = function scandir (dir, cb) {
const fs = require('fs')
const path = require('path')
let result = []
fs.readdir(dir, (err, filesList) => {
if (err) return cb(err)
let pending = filesList.length
if (!pending) return cb(null, result)
filesList.forEach((file) => {
file = path.resolve(dir, file)
fs.stat(file, (err, stat) => {
if (stat && stat.isDirectory()) {
scandir(file, (err, res) => {
if (err) cb(err)
result = result.concat(res)
if (!--pending) cb(null, result)
})
} else {
result.push(file)
if (!--pending) cb(null, result)
/* extention filtering
let extentions = ['.mkv', '.MKV', '.mp4', '.MP4', '.avi', '.AVI', '.flv', '.FLV']
for (let extention of extentions) {
if (file.endsWith(extention)) result.push(file)
}
*/
}
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment