1 | /**
|
2 | * @fileoverview Utilities for working with globs and the filesystem.
|
3 | * @author Ian VanSchooten
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const lodash = require("lodash"),
|
12 | fs = require("fs"),
|
13 | path = require("path"),
|
14 | GlobSync = require("./glob"),
|
15 |
|
16 | pathUtil = require("./path-util"),
|
17 | IgnoredPaths = require("../ignored-paths");
|
18 |
|
19 | const debug = require("debug")("eslint:glob-util");
|
20 |
|
21 | //------------------------------------------------------------------------------
|
22 | // Helpers
|
23 | //------------------------------------------------------------------------------
|
24 |
|
25 | /**
|
26 | * Checks if a provided path is a directory and returns a glob string matching
|
27 | * all files under that directory if so, the path itself otherwise.
|
28 | *
|
29 | * Reason for this is that `glob` needs `/**` to collect all the files under a
|
30 | * directory where as our previous implementation without `glob` simply walked
|
31 | * a directory that is passed. So this is to maintain backwards compatibility.
|
32 | *
|
33 | * Also makes sure all path separators are POSIX style for `glob` compatibility.
|
34 | *
|
35 | * @param {Object} [options] An options object
|
36 | * @param {string[]} [options.extensions=[".js"]] An array of accepted extensions
|
37 | * @param {string} [options.cwd=process.cwd()] The cwd to use to resolve relative pathnames
|
38 | * @returns {Function} A function that takes a pathname and returns a glob that
|
39 | * matches all files with the provided extensions if
|
40 | * pathname is a directory.
|
41 | */
|
42 | function processPath(options) {
|
43 | const cwd = (options && options.cwd) || process.cwd();
|
44 | let extensions = (options && options.extensions) || [".js"];
|
45 |
|
46 | extensions = extensions.map(ext => ext.replace(/^\./, ""));
|
47 |
|
48 | let suffix = "/**";
|
49 |
|
50 | if (extensions.length === 1) {
|
51 | suffix += `/*.${extensions[0]}`;
|
52 | } else {
|
53 | suffix += `/*.{${extensions.join(",")}}`;
|
54 | }
|
55 |
|
56 | /**
|
57 | * A function that converts a directory name to a glob pattern
|
58 | *
|
59 | * @param {string} pathname The directory path to be modified
|
60 | * @returns {string} The glob path or the file path itself
|
61 | * @private
|
62 | */
|
63 | return function(pathname) {
|
64 | let newPath = pathname;
|
65 | const resolvedPath = path.resolve(cwd, pathname);
|
66 |
|
67 | if (fs.existsSync(resolvedPath) && fs.statSync(resolvedPath).isDirectory()) {
|
68 | newPath = pathname.replace(/[/\\]$/, "") + suffix;
|
69 | }
|
70 |
|
71 | return pathUtil.convertPathToPosix(newPath);
|
72 | };
|
73 | }
|
74 |
|
75 | //------------------------------------------------------------------------------
|
76 | // Public Interface
|
77 | //------------------------------------------------------------------------------
|
78 |
|
79 | /**
|
80 | * Resolves any directory patterns into glob-based patterns for easier handling.
|
81 | * @param {string[]} patterns File patterns (such as passed on the command line).
|
82 | * @param {Object} options An options object.
|
83 | * @returns {string[]} The equivalent glob patterns and filepath strings.
|
84 | */
|
85 | function resolveFileGlobPatterns(patterns, options) {
|
86 |
|
87 | const processPathExtensions = processPath(options);
|
88 |
|
89 | return patterns.filter(p => p.length).map(processPathExtensions);
|
90 | }
|
91 |
|
92 | const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/;
|
93 |
|
94 | /**
|
95 | * Build a list of absolute filesnames on which ESLint will act.
|
96 | * Ignored files are excluded from the results, as are duplicates.
|
97 | *
|
98 | * @param {string[]} globPatterns Glob patterns.
|
99 | * @param {Object} [options] An options object.
|
100 | * @param {string} [options.cwd] CWD (considered for relative filenames)
|
101 | * @param {boolean} [options.ignore] False disables use of .eslintignore.
|
102 | * @param {string} [options.ignorePath] The ignore file to use instead of .eslintignore.
|
103 | * @param {string} [options.ignorePattern] A pattern of files to ignore.
|
104 | * @returns {string[]} Resolved absolute filenames.
|
105 | */
|
106 | function listFilesToProcess(globPatterns, options) {
|
107 | options = options || { ignore: true };
|
108 | const files = [],
|
109 | added = {};
|
110 |
|
111 | const cwd = (options && options.cwd) || process.cwd();
|
112 |
|
113 | const getIgnorePaths = lodash.memoize(
|
114 | optionsObj =>
|
115 | new IgnoredPaths(optionsObj)
|
116 | );
|
117 |
|
118 | /**
|
119 | * Executes the linter on a file defined by the `filename`. Skips
|
120 | * unsupported file extensions and any files that are already linted.
|
121 | * @param {string} filename The file to be processed
|
122 | * @param {boolean} shouldWarnIgnored Whether or not a report should be made if
|
123 | * the file is ignored
|
124 | * @param {IgnoredPaths} ignoredPaths An instance of IgnoredPaths
|
125 | * @returns {void}
|
126 | */
|
127 | function addFile(filename, shouldWarnIgnored, ignoredPaths) {
|
128 | let ignored = false;
|
129 | let isSilentlyIgnored;
|
130 |
|
131 | if (ignoredPaths.contains(filename, "default")) {
|
132 | ignored = (options.ignore !== false) && shouldWarnIgnored;
|
133 | isSilentlyIgnored = !shouldWarnIgnored;
|
134 | }
|
135 |
|
136 | if (options.ignore !== false) {
|
137 | if (ignoredPaths.contains(filename, "custom")) {
|
138 | if (shouldWarnIgnored) {
|
139 | ignored = true;
|
140 | } else {
|
141 | isSilentlyIgnored = true;
|
142 | }
|
143 | }
|
144 | }
|
145 |
|
146 | if (isSilentlyIgnored && !ignored) {
|
147 | return;
|
148 | }
|
149 |
|
150 | if (added[filename]) {
|
151 | return;
|
152 | }
|
153 | files.push({ filename, ignored });
|
154 | added[filename] = true;
|
155 | }
|
156 |
|
157 | debug("Creating list of files to process.");
|
158 | globPatterns.forEach(pattern => {
|
159 | const file = path.resolve(cwd, pattern);
|
160 |
|
161 | if (fs.existsSync(file) && fs.statSync(file).isFile()) {
|
162 | const ignoredPaths = getIgnorePaths(options);
|
163 |
|
164 | addFile(fs.realpathSync(file), true, ignoredPaths);
|
165 | } else {
|
166 |
|
167 | // regex to find .hidden or /.hidden patterns, but not ./relative or ../relative
|
168 | const globIncludesDotfiles = dotfilesPattern.test(pattern);
|
169 | let newOptions = options;
|
170 |
|
171 | if (!options.dotfiles) {
|
172 | newOptions = Object.assign({}, options, { dotfiles: globIncludesDotfiles });
|
173 | }
|
174 |
|
175 | const ignoredPaths = getIgnorePaths(newOptions);
|
176 | const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
|
177 | const globOptions = {
|
178 | nodir: true,
|
179 | dot: true,
|
180 | cwd
|
181 | };
|
182 |
|
183 | new GlobSync(pattern, globOptions, shouldIgnore).found.forEach(globMatch => {
|
184 | addFile(path.resolve(cwd, globMatch), false, ignoredPaths);
|
185 | });
|
186 | }
|
187 | });
|
188 |
|
189 | return files;
|
190 | }
|
191 |
|
192 | module.exports = {
|
193 | resolveFileGlobPatterns,
|
194 | listFilesToProcess
|
195 | };
|