-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
21 lines (19 loc) · 806 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"use strict";
const fs = require("fs");
const promisify = require("util").promisify;
const path = require("path");
/**
* Create an array of filenames of specific type.
* @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
*/
module.exports = (fileType, filePath = "./", recursive = false) => {
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))
);
};