1 | /**
|
2 | * @fileoverview Responsible for loading ignore config files and managing ignore patterns
|
3 | * @author Jonathan Rajavuori
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | var fs = require("fs"),
|
12 | debug = require("debug"),
|
13 | minimatch = require("minimatch"),
|
14 | FileFinder = require("./file-finder");
|
15 |
|
16 | debug = debug("eslint:ignored-paths");
|
17 |
|
18 |
|
19 | //------------------------------------------------------------------------------
|
20 | // Constants
|
21 | //------------------------------------------------------------------------------
|
22 |
|
23 | var ESLINT_IGNORE_FILENAME = ".eslintignore";
|
24 |
|
25 |
|
26 | //------------------------------------------------------------------------------
|
27 | // Helpers
|
28 | //------------------------------------------------------------------------------
|
29 |
|
30 | /**
|
31 | * Load and parse ignore patterns from the file at the given path
|
32 | * @param {string} filepath Path to the ignore file.
|
33 | * @returns {string[]} An array of ignore patterns or an empty array if no ignore file.
|
34 | */
|
35 | function loadIgnoreFile(filepath) {
|
36 | var ignorePatterns = [];
|
37 |
|
38 | function nonEmpty(line) {
|
39 | return line.trim() !== "" && line[0] !== "#";
|
40 | }
|
41 |
|
42 | if (filepath) {
|
43 | try {
|
44 | ignorePatterns = fs.readFileSync(filepath, "utf8").split(/\r?\n/).filter(nonEmpty);
|
45 | } catch (e) {
|
46 | e.message = "Cannot read ignore file: " + filepath + "\nError: " + e.message;
|
47 | throw e;
|
48 | }
|
49 | }
|
50 |
|
51 | ignorePatterns.forEach(function (pattern) {
|
52 | ignorePatterns.push(pattern + "/**");
|
53 | });
|
54 |
|
55 | return ignorePatterns;
|
56 | }
|
57 |
|
58 | var ignoreFileFinder;
|
59 |
|
60 | /**
|
61 | * Find an ignore file in the current directory.
|
62 | * @returns {string|boolean} path of ignore file if found, or false if no ignore is found
|
63 | */
|
64 | function findIgnoreFile() {
|
65 | if (!ignoreFileFinder) {
|
66 | ignoreFileFinder = new FileFinder(ESLINT_IGNORE_FILENAME);
|
67 | }
|
68 |
|
69 | return ignoreFileFinder.findInDirectory();
|
70 | }
|
71 |
|
72 |
|
73 | //------------------------------------------------------------------------------
|
74 | // Public Interface
|
75 | //------------------------------------------------------------------------------
|
76 |
|
77 | /**
|
78 | * IgnoredPaths
|
79 | * @constructor
|
80 | * @class IgnoredPaths
|
81 | * @param {Array} patterns to be matched against file paths
|
82 | */
|
83 | function IgnoredPaths(patterns) {
|
84 | this.patterns = patterns;
|
85 | }
|
86 |
|
87 | /**
|
88 | * IgnoredPaths initializer
|
89 | * @param {Object} options object containing 'ignore' and 'ignorePath' properties
|
90 | * @returns {IgnoredPaths} object, with patterns loaded from the ignore file
|
91 | */
|
92 | IgnoredPaths.load = function (options) {
|
93 | var patterns;
|
94 |
|
95 | options = options || {};
|
96 |
|
97 | if (options.ignore) {
|
98 | patterns = loadIgnoreFile(options.ignorePath || findIgnoreFile());
|
99 | } else {
|
100 | patterns = [];
|
101 | }
|
102 |
|
103 | return new IgnoredPaths(patterns);
|
104 | };
|
105 |
|
106 | /**
|
107 | * Determine whether a file path is included in the configured ignore patterns
|
108 | * @param {string} filepath Path to check
|
109 | * @returns {boolean} true if the file path matches one or more patterns, false otherwise
|
110 | */
|
111 | IgnoredPaths.prototype.contains = function (filepath) {
|
112 | if (this.patterns === null) {
|
113 | throw new Error("No ignore patterns loaded, call 'load' first");
|
114 | }
|
115 |
|
116 | filepath = filepath.replace("\\", "/");
|
117 | return this.patterns.some(function (pattern) {
|
118 | var result = minimatch(filepath, pattern);
|
119 | debug("Minimatch " + result);
|
120 | return result;
|
121 | });
|
122 | };
|
123 |
|
124 | module.exports = IgnoredPaths;
|