Created
July 1, 2017 14:03
-
-
Save itshaadi/50289d0010c90bd882cf9cd08becd255 to your computer and use it in GitHub Desktop.
list all files in a recursive way
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
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