|
1 | 1 | import fs from 'fs'; |
2 | 2 | import path from 'path'; |
3 | | -import glob from 'glob'; |
| 3 | +import glob from 'tiny-glob'; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * Replace Windows with posix style paths |
@@ -30,20 +30,17 @@ function convertPathToPosix(filepath) { |
30 | 30 | * matches all files with the provided extensions if |
31 | 31 | * pathname is a directory. |
32 | 32 | */ |
33 | | -function processPath(extensions) { |
| 33 | +function processPath(extensions = ['.js']) { |
34 | 34 | const cwd = process.cwd(); |
35 | | - extensions = extensions || ['.js']; |
36 | 35 |
|
37 | | - extensions = extensions.map(function (ext) { |
38 | | - return ext.replace(/^\./, ''); |
39 | | - }); |
| 36 | + extensions = extensions.map(ext => ext.replace(/^\./, '')); |
40 | 37 |
|
41 | | - let suffix = '/**'; |
| 38 | + let suffix = '/**/*.'; |
42 | 39 |
|
43 | 40 | if (extensions.length === 1) { |
44 | | - suffix += '/*.' + extensions[0]; |
| 41 | + suffix += extensions[0]; |
45 | 42 | } else { |
46 | | - suffix += '/*.{' + extensions.join(',') + '}'; |
| 43 | + suffix += `{${extensions.join(',')}}`; |
47 | 44 | } |
48 | 45 |
|
49 | 46 | /** |
@@ -79,51 +76,33 @@ function resolveFileGlobPatterns(patterns, extensions) { |
79 | 76 | return patterns.map(processPathExtensions); |
80 | 77 | } |
81 | 78 |
|
| 79 | +const cwd = process.cwd(); |
| 80 | +const globOptions = { |
| 81 | + filesOnly: true, |
| 82 | + dot: true, |
| 83 | + cwd |
| 84 | +}; |
| 85 | + |
82 | 86 | /** |
83 | 87 | * Build a list of absolute filenames on which ESLint will act. |
84 | 88 | * Ignored files are excluded from the results, as are duplicates. |
85 | 89 | * |
86 | 90 | * @param globPatterns Glob patterns. |
87 | 91 | * @returns Resolved absolute filenames. |
88 | 92 | */ |
89 | | -function listFilesToProcess(globPatterns) { |
90 | | - const files = []; |
91 | | - const added = new Set(); |
92 | | - |
93 | | - const cwd = process.cwd(); |
94 | | - |
95 | | - /** |
96 | | - * Executes the linter on a file defined by the `filename`. Skips |
97 | | - * unsupported file extensions and any files that are already linted. |
98 | | - * @param {string} filename The file to be processed |
99 | | - * @returns {void} |
100 | | - */ |
101 | | - function addFile(filename) { |
102 | | - if (added.has(filename)) { |
103 | | - return; |
104 | | - } |
105 | | - files.push(filename); |
106 | | - added.add(filename); |
107 | | - } |
108 | | - |
109 | | - globPatterns.forEach(function (pattern) { |
| 93 | +async function listFilesToProcess(globPatterns) { |
| 94 | + const promises = globPatterns.map(async pattern => { |
110 | 95 | const file = path.resolve(cwd, pattern); |
111 | 96 | if (fs.existsSync(file) && fs.statSync(file).isFile()) { |
112 | | - addFile(fs.realpathSync(file)); |
113 | | - } else { |
114 | | - const globOptions = { |
115 | | - nodir: true, |
116 | | - dot: true, |
117 | | - cwd |
118 | | - }; |
119 | | - |
120 | | - glob.sync(pattern, globOptions).forEach(function (globMatch) { |
121 | | - addFile(path.resolve(cwd, globMatch)); |
122 | | - }); |
| 97 | + return fs.realpathSync(file); |
123 | 98 | } |
| 99 | + return (await glob(pattern, globOptions)).map(globMatch => |
| 100 | + path.resolve(cwd, globMatch) |
| 101 | + ); |
124 | 102 | }); |
125 | 103 |
|
126 | | - return files; |
| 104 | + const files = (await Promise.all(promises)).flat(); |
| 105 | + return Array.from(new Set(files)); |
127 | 106 | } |
128 | 107 |
|
129 | 108 | export default function smartGlob(indexes, extensions) { |
|
0 commit comments