UNPKG

1.89 kBJavaScriptView Raw
1"use strict";
2
3var fs = require('fs');
4var path = require('path');
5var walk = require('walk');
6var expandTilde = require('expand-tilde');
7var globToRegExp = require('glob-to-regexp');
8
9var jsOrJsxRegex = /^[^.](.+?)\.jsx?$/i;
10
11function ignoreList(ignores) {
12 ignores = ignores || [];
13 // minimist, why do you give me two different types
14 // depending on the input? :(
15 ignores = ignores.split ? ignores.split(',') : ignores;
16 return ignores.map(function(i) {
17 return globToRegExp(expandTilde(i));
18 });
19}
20
21function shouldIgnore(path, ignore) {
22 return ignore.some(function(i) { return i.test(path); });
23}
24
25module.exports = function filesFromMixedPaths(paths, options) {
26 options = options || {};
27 var files = [];
28 var directories = [];
29 var ignore = ignoreList(options.ignore);
30
31 paths.forEach(function(path) {
32 path = expandTilde(path);
33 var lstat = fs.lstatSync(path);
34 if (lstat.isDirectory()) {
35 directories.push(path);
36 } else if (lstat.isFile()) {
37 files.push(path);
38 }
39 });
40
41 directories.forEach(function(dirPath) {
42 var options = {
43 listeners: {
44 directories: function(root, dirStatsArray, next) {
45 next();
46 },
47 file: function(root, fileStats, next) {
48 if (jsOrJsxRegex.test(fileStats.name)) {
49 files.push(path.join(root, fileStats.name));
50 }
51 next();
52 },
53 errors: function(root, nodeStatsArray, next) {
54 console.error('Error with', nodeStatsArray);
55 next();
56 }
57 }
58 }
59 walk.walkSync(dirPath, options);
60 });
61
62 return files.filter(function(path) {
63 return !shouldIgnore(path, ignore)
64 });
65};