yadda
Version:
A true BDD framework for JavaScript
62 lines (49 loc) • 1.64 kB
JavaScript
var shims = require('./shims/index');
var path = shims.path;
var fs = shims.fs;
var $ = require('./Array');
// Searches for files in the given directories and their sub-directories, matching at least one of the given patterns
var FileSearch = function (directories, patterns) {
// eslint-disable-next-line no-redeclare
var patterns = patterns || /.*/;
this.each = function (fn) {
this.list().forEach(fn);
};
this.list = function () {
return $(directories).inject($(), function (files, directory) {
return files.concat(list_files(directory).find_all(by_pattern));
});
};
var list_files = function (directory) {
return $(list_immediate_files(directory).concat(list_sub_directory_files(directory)));
};
var list_immediate_files = function (directory) {
return ls(directory).find_all(by_file);
};
var list_sub_directory_files = function (directory) {
return ls(directory)
.find_all(by_directory)
.inject($(), function (files, directory) {
return files.concat(list_files(directory));
});
};
var ls = function (directory) {
if (!fs.existsSync(directory)) return $();
return $(fs.readdirSync(directory)).collect(function (file) {
return path.join(directory, file);
});
};
var by_file = function (file) {
return !by_directory(file);
};
var by_directory = function (file) {
return fs.statSync(file).isDirectory();
};
var by_pattern = function (filename) {
return $(patterns).find(function (pattern) {
return new RegExp(pattern).test(filename);
});
};
};
module.exports = FileSearch;
;