1 | /**
|
2 | * @fileoverview Utilities for working with globs and the filesystem.
|
3 | * @author Ian VanSchooten
|
4 | * @copyright 2015 Ian VanSchooten. All rights reserved.
|
5 | * See LICENSE in root directory for full license.
|
6 | */
|
7 | ;
|
8 |
|
9 | //------------------------------------------------------------------------------
|
10 | // Requirements
|
11 | //------------------------------------------------------------------------------
|
12 |
|
13 | var debug = require("debug"),
|
14 | fs = require("fs"),
|
15 | glob = require("glob"),
|
16 | shell = require("shelljs"),
|
17 |
|
18 | IgnoredPaths = require("../ignored-paths");
|
19 |
|
20 | debug = debug("eslint:glob-util");
|
21 |
|
22 | //------------------------------------------------------------------------------
|
23 | // Helpers
|
24 | //------------------------------------------------------------------------------
|
25 |
|
26 | /**
|
27 | * Checks if a provided path is a directory and returns a glob string matching
|
28 | * all files under that directory if so, the path itself otherwise.
|
29 | *
|
30 | * Reason for this is that `glob` needs `/**` to collect all the files under a
|
31 | * directory where as our previous implementation without `glob` simply walked
|
32 | * a directory that is passed. So this is to maintain backwards compatibility.
|
33 | *
|
34 | * Also makes sure all path separators are POSIX style for `glob` compatibility.
|
35 | *
|
36 | * @param {string[]} extensions An array of accepted extensions
|
37 | * @returns {Function} A function that takes a pathname and returns a glob that
|
38 | * matches all files with the provided extensions if
|
39 | * pathname is a directory.
|
40 | */
|
41 | function processPath(extensions) {
|
42 | var suffix = "/**";
|
43 |
|
44 | if (extensions.length === 1) {
|
45 | suffix += "/*." + extensions[0];
|
46 | } else {
|
47 | suffix += "/*.{" + extensions.join(",") + "}";
|
48 | }
|
49 |
|
50 | /**
|
51 | * A function that converts a directory name to a glob pattern
|
52 | *
|
53 | * @param {string} pathname The directory path to be modified
|
54 | * @returns {string} The glob path or the file path itself
|
55 | * @private
|
56 | */
|
57 | return function(pathname) {
|
58 | var newPath = pathname;
|
59 |
|
60 | if (shell.test("-d", pathname)) {
|
61 | newPath = pathname.replace(/[\/\\]$/, "") + suffix;
|
62 | }
|
63 |
|
64 | return newPath.replace(/\\/g, "/").replace(/^\.\//, "");
|
65 | };
|
66 | }
|
67 |
|
68 | //------------------------------------------------------------------------------
|
69 | // Public Interface
|
70 | //------------------------------------------------------------------------------
|
71 |
|
72 | /**
|
73 | * Resolves the patterns into glob-based patterns for easier handling.
|
74 | * @param {string[]} patterns File patterns (such as passed on the command line).
|
75 | * @param {string[]} extensions List of valid file extensions.
|
76 | * @returns {string[]} The equivalent glob patterns.
|
77 | */
|
78 | function resolveFileGlobPatterns(patterns, extensions) {
|
79 | extensions = extensions || [".js"];
|
80 |
|
81 | extensions = extensions.map(function(ext) {
|
82 | return ext.charAt(0) === "." ? ext.substr(1) : ext;
|
83 | });
|
84 |
|
85 | var processPathExtensions = processPath(extensions);
|
86 | return patterns.map(processPathExtensions);
|
87 | }
|
88 |
|
89 | /**
|
90 | * Build a list of absolute filesnames on which ESLint will act.
|
91 | * Ignored files are excluded from the results, as are duplicates.
|
92 | *
|
93 | * @param {string[]} globPatterns Glob patterns.
|
94 | * @param {Object} [options] An options object.
|
95 | * @param {boolean} [options.ignore] False disables use of .eslintignore.
|
96 | * @param {string} [options.ignorePath] The ignore file to use instead of .eslintignore.
|
97 | * @param {string} [options.ignorePattern] A pattern of files to ignore.
|
98 | * @returns {string[]} Resolved absolute filenames.
|
99 | */
|
100 | function listFilesToProcess(globPatterns, options) {
|
101 | var ignoredPaths,
|
102 | ignoredPathsList,
|
103 | files = [],
|
104 | added = {},
|
105 | globOptions,
|
106 | rulesKey = "_rules";
|
107 |
|
108 | /**
|
109 | * Executes the linter on a file defined by the `filename`. Skips
|
110 | * unsupported file extensions and any files that are already linted.
|
111 | * @param {string} filename The file to be processed
|
112 | * @returns {void}
|
113 | */
|
114 | function addFile(filename) {
|
115 | if (ignoredPaths.contains(filename)) {
|
116 | return;
|
117 | }
|
118 | filename = fs.realpathSync(filename);
|
119 | if (added[filename]) {
|
120 | return;
|
121 | }
|
122 | files.push(filename);
|
123 | added[filename] = true;
|
124 | }
|
125 |
|
126 | options = options || { ignore: true, dotfiles: true };
|
127 | ignoredPaths = new IgnoredPaths(options);
|
128 | ignoredPathsList = ignoredPaths.ig.custom[rulesKey].map(function(rule) {
|
129 | return rule.pattern;
|
130 | });
|
131 | globOptions = {
|
132 | nodir: true,
|
133 | ignore: ignoredPathsList
|
134 | };
|
135 |
|
136 | debug("Creating list of files to process.");
|
137 | globPatterns.forEach(function(pattern) {
|
138 | if (shell.test("-f", pattern)) {
|
139 | addFile(pattern);
|
140 | } else {
|
141 | glob.sync(pattern, globOptions).forEach(addFile);
|
142 | }
|
143 | });
|
144 |
|
145 | return files;
|
146 | }
|
147 |
|
148 | module.exports = {
|
149 | resolveFileGlobPatterns: resolveFileGlobPatterns,
|
150 | listFilesToProcess: listFilesToProcess
|
151 | };
|