UNPKG

filepaths

Version:

Get paths to all files in dirs/subdirs

109 lines (88 loc) 2.72 kB
var nodePath = require('path'), sep = nodePath.sep, fs = require('fs'); /** * Recurses through the supplied paths, returning an array of file paths found * in each. Paths may be a string, or an array of strings. The paths may be * strings pointing to files or directories. Accepts an options object with * the following keys: ignore and ext. opts.ignore takes either a single * string pattern or array of patterns to ignore. opts.ext accepts either * a string, or array of strings corresponding to filename extensions to * retrieve. * * @param {string|string[]} paths The paths to traverse * @param {object} opts Options object with ignore and ext keys */ exports.getSync = function(paths, opts) { var results, ignorePatterns, i; paths = paths || []; opts = opts || {}; results = []; if (!(paths instanceof Array)) { paths = [paths]; } ignorePatterns = []; if (opts.ignore) { if (!(opts.ignore instanceof Array)) { opts.ignore = [opts.ignore]; } for (i = 0; i < opts.ignore.length; i++) { ignorePatterns.push(new RegExp(opts.ignore[i])); } } if (!opts.ext) { opts.ext = []; } else if (!(opts.ext instanceof Array)) { opts.ext = [opts.ext]; } paths.forEach(function(path) { if (!fs.existsSync(path)) { throw new Error('No such file or directory: ' + path); } else if (fs.statSync(path).isFile()) { return results.push(path); } walkSync(path, function(dirPath, dirs, files) { files.forEach(function(file) { var filePath; var ext = nodePath.extname(file); if (opts.ext.length && opts.ext.indexOf(ext) === -1) { return; } if (dirPath.slice(-1) !== sep) { dirPath += sep; } if (dirPath.indexOf(sep) !== 0 && dirPath.indexOf('.') !== 0) { dirPath = './' + dirPath; } filePath = dirPath + file; for (var i = 0; i < ignorePatterns.length; i++) { if (ignorePatterns[i].test(filePath)) return; } results.push(filePath); }); }); }); return results; }; function walkSync(dir, fn) { if (!fs.statSync(dir).isDirectory()) { throw new Error(dir + ' is not a directory'); } var dirs = []; var files = []; fs.readdirSync(dir).forEach(function(name) { var stat = fs.lstatSync(nodePath.join(dir, name)); // Don't follow symbolic links if (stat.isSymbolicLink()) { return; } else if (stat.isDirectory()) { dirs.push(name); } else { files.push(name); } }); fn(dir, dirs, files); dirs.forEach(function(subdir) { walkSync(nodePath.join(dir, subdir), fn); }); };