readdir
Version:
Reads a directory and return results with the ability to use Ant style file match patterns
58 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* For the supplied paths list, matches against the supplied filters and returns a new array of paths that
* are ordered as the list of filters would imply they should be. The filters can include * as a match-anything in
* one directory or ** for match any file in any directory. All filters are treated as an ends-with match.
*/
var read_dir_options_1 = require("../read-dir-options");
var sort_1 = require("./sort");
var path_1 = require("path");
function file_list_filter(paths, filters) {
if (!filters.length) {
return paths;
}
return filters.reduce(function (result, filter) {
var filterRegex = regularExpressionFromFilter(filter);
paths.forEach(function (path) {
if (result.indexOf(path) < 0 && path.match(filterRegex)) {
result.push(path);
}
});
return result;
}, []);
}
exports.file_list_filter = file_list_filter;
/**
* Changes the values in the supplied paths array to be absolute URIs
*/
function prepend_paths(prefix, paths) {
paths.forEach(function (path, index) { return paths[index] = prefix + path; });
}
exports.prepend_paths = prepend_paths;
function apply_filters(basePath, allFiles, includeFilters, options) {
if (Array.isArray(includeFilters)) {
allFiles = file_list_filter(allFiles, includeFilters);
}
if (read_dir_options_1.ReadDirOptions.ABSOLUTE_PATHS & options) {
prepend_paths(path_1.resolve(process.cwd(), basePath) + '/', allFiles);
}
if (read_dir_options_1.ReadDirOptions.CASELESS_SORT & options) {
allFiles = sort_1.sort_paths(allFiles, sort_1.caseless_sort);
}
if (read_dir_options_1.ReadDirOptions.CASE_SORT & options) {
allFiles = sort_1.sort_paths(allFiles, sort_1.case_sort);
}
return allFiles;
}
exports.apply_filters = apply_filters;
function regularExpressionFromFilter(filter) {
return new RegExp('^' +
filter.replace(/\./g, '\\.')
.replace(/(\*?)(\*)(?!\*)/g, regularExpressionMatch)
.replace(/\*\*/g, '\.*') + '$', 'i');
}
function regularExpressionMatch(match, prefix) {
return prefix === '*' ? match : '[^\\/]*';
}
//# sourceMappingURL=filters.js.map