Skip to content

Instantly share code, notes, and snippets.

@davidicus
Created October 24, 2018 01:21
Show Gist options
  • Save davidicus/606d419232f0dc388e23a61124825627 to your computer and use it in GitHub Desktop.
Save davidicus/606d419232f0dc388e23a61124825627 to your computer and use it in GitHub Desktop.
Get files of specific type using node
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