1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const glob = require('glob');
|
4 |
|
5 | /**
|
6 | * Replace Windows with posix style paths
|
7 | *
|
8 | * @param {string} filepath Path to convert
|
9 | * @returns {string} Converted filepath
|
10 | */
|
11 | function convertPathToPosix(filepath) {
|
12 | const normalizedFilepath = path.normalize(filepath);
|
13 | const posixFilepath = normalizedFilepath.replace(/\\/g, '/');
|
14 |
|
15 | return posixFilepath;
|
16 | }
|
17 |
|
18 | /**
|
19 | * Checks if a provided path is a directory and returns a glob string matching
|
20 | * all files under that directory if so, the path itself otherwise.
|
21 | *
|
22 | * Reason for this is that `glob` needs `/**` to collect all the files under a
|
23 | * directory where as our previous implementation without `glob` simply walked
|
24 | * a directory that is passed. So this is to maintain backwards compatibility.
|
25 | *
|
26 | * Also makes sure all path separators are POSIX style for `glob` compatibility.
|
27 | *
|
28 | * @param {string[]} [extensions=['.js']] An array of accepted extensions
|
29 | * @returns {Function} A function that takes a pathname and returns a glob that
|
30 | * matches all files with the provided extensions if
|
31 | * pathname is a directory.
|
32 | */
|
33 | function processPath(extensions) {
|
34 | const cwd = process.cwd();
|
35 | extensions = extensions || ['.js'];
|
36 |
|
37 | extensions = extensions.map(function(ext) {
|
38 | return ext.replace(/^\./, '');
|
39 | });
|
40 |
|
41 | let suffix = '/**';
|
42 |
|
43 | if (extensions.length === 1) {
|
44 | suffix += '/*.' + extensions[0];
|
45 | } else {
|
46 | suffix += '/*.{' + extensions.join(',') + '}';
|
47 | }
|
48 |
|
49 | /**
|
50 | * A function that converts a directory name to a glob pattern
|
51 | *
|
52 | * @param {string} pathname The directory path to be modified
|
53 | * @returns {string} The glob path or the file path itself
|
54 | * @private
|
55 | */
|
56 | return function(pathname) {
|
57 | let newPath = pathname;
|
58 | const resolvedPath = path.resolve(cwd, pathname);
|
59 |
|
60 | if (
|
61 | fs.existsSync(resolvedPath) &&
|
62 | fs.lstatSync(resolvedPath).isDirectory()
|
63 | ) {
|
64 | newPath = pathname.replace(/[/\\]$/, '') + suffix;
|
65 | }
|
66 |
|
67 | return convertPathToPosix(newPath);
|
68 | };
|
69 | }
|
70 |
|
71 | /**
|
72 | * Resolves any directory patterns into glob-based patterns for easier handling.
|
73 | * @param {string[]} patterns File patterns (such as passed on the command line).
|
74 | * @param {Array<string>} extensions A list of file extensions
|
75 | * @returns {string[]} The equivalent glob patterns and filepath strings.
|
76 | */
|
77 | function resolveFileGlobPatterns(patterns, extensions) {
|
78 | const processPathExtensions = processPath(extensions);
|
79 | return patterns.map(processPathExtensions);
|
80 | }
|
81 |
|
82 | /**
|
83 | * Build a list of absolute filenames on which ESLint will act.
|
84 | * Ignored files are excluded from the results, as are duplicates.
|
85 | *
|
86 | * @param globPatterns Glob patterns.
|
87 | * @returns Resolved absolute filenames.
|
88 | */
|
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) {
|
110 | const file = path.resolve(cwd, pattern);
|
111 | 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 | });
|
123 | }
|
124 | });
|
125 |
|
126 | return files;
|
127 | }
|
128 |
|
129 | function smartGlob(indexes, extensions) {
|
130 | return listFilesToProcess(resolveFileGlobPatterns(indexes, extensions));
|
131 | }
|
132 |
|
133 | module.exports = smartGlob;
|