var glob = require("glob")
// options is optionalglob("**/*.js", options, function (er, files) {// files is an array of filenames.// If the `nonull` option is set, and nothing// was found, then files is ["**/*.js"]// er is an error object or null.})
var DIR = '/usr/local/bin';
// 1. List all files in DIRfileList(DIR);// => ['/usr/local/bin/babel', '/usr/local/bin/bower', ...]
// 2. List all file names in DIRfileList(DIR).map((file) => file.split(path.sep).slice(-1)[0]);// => ['babel', 'bower', ...]
import * as fs from 'fs';import * as Path from 'path';
function getFilenames(path, extension) {return fs.readdirSync(path).filter(item =>fs.statSync(Path.join(path, item)).isFile() &&(extension === undefined || Path.extname(item) === extension)).sort();}
import * as FsExtra from 'fs-extra'
/*** Finds files in the folder that match filePattern, optionally passing back errors .* If folderDepth isn't specified, only the first level is searched. Otherwise anything up* to Infinity is supported.** @static* @param {string} folder The folder to start in.* @param {string} [filePattern='.*'] A regular expression of the files you want to find.* @param {(Error[] | undefined)} [errors=undefined]* @param {number} [folderDepth=0]* @returns {Promise<string[]>}* @memberof FileHelper*/public static async findFiles(folder: string,filePattern: string = '.*',errors: Error[] | undefined = undefined,folderDepth: number = 0): Promise<string[]> {const results: string[] = []
// Get all files from the folderlet items = await FsExtra.readdir(folder).catch(error => {if (errors) {errors.push(error) // Save errors if we wish (e.g. folder perms issues)}
return results})
// Go through to the required depth and no furtherfolderDepth = folderDepth - 1
// Loop through the results, possibly recursefor (const item of items) {try {const fullPath = Path.join(folder, item)
if (FsExtra.statSync(fullPath).isDirectory() &&folderDepth > -1)) {// Its a folder, recursively get the child folders' filesresults.push(...(await FileHelper.findFiles(fullPath, filePattern, errors, folderDepth)))} else {// Filter by the file name pattern, if there is oneif (filePattern === '.*' || item.search(new RegExp(filePattern, 'i')) > -1) {results.push(fullPath)}}} catch (error) {if (errors) {errors.push(error) // Save errors if we wish}}}
return results}