Last active
December 8, 2019 20:46
-
-
Save mikaelbalin/bc8b1c348eeb4a31a31299c690b6ccb4 to your computer and use it in GitHub Desktop.
Get files inside certain directory
This file contains hidden or 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 fs = require('fs'); | |
const path = require('path'); | |
/** | |
* Returns file paths inside of certain directory | |
* @param {String} directory Files directory | |
* @param {Function} callback Do something with files | |
*/ | |
exports.getFiles = (directory, callback) => { | |
if (fs.existsSync(directory)) { | |
fs.readdir(directory, (err, files) => { | |
if (err) console.error(err); | |
const paths = files.map(file => path.join(directory, file)); | |
if (callback) callback(paths); | |
}); | |
} else { | |
console.error('> Directory does\'t exist'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment