Created
October 24, 2018 01:21
-
-
Save davidicus/606d419232f0dc388e23a61124825627 to your computer and use it in GitHub Desktop.
Get files of specific type using node
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 promisify = require('util').promisify; | |
const path = require('path'); | |
// @param: fileType - the file extension you are searching for | |
// @param: filePath - the path to directory you would like to search. Defaults to __dirname | |
// @returns: <Promise> which will eventually resolve to an array with either the files names or empty | |
const getFileType = (fileType, filePath = './') => { | |
const match = '([a-zA-Z0-9\\s_\\.\\-():])+('; | |
const regEx = new RegExp(match + fileType + ')$', 'i'); | |
const fsReadDir = promisify(fs.readdir); | |
const directoryPath = path.resolve(__dirname, filePath);; | |
return fsReadDir(directoryPath).then(files => files.filter(file => file.match(regEx))); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment