1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const mm = require('micromatch');
|
5 |
|
6 | const matchers = [];
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function addIgnorePattern(val) {
|
15 | if (val && typeof val === 'string' && val[0] !== '#') {
|
16 | let pattern = val;
|
17 | if (pattern.indexOf('/') === -1) {
|
18 | matchers.push('**/' + pattern);
|
19 | } else if (pattern[pattern.length-1] === '/') {
|
20 | matchers.push('**/' + pattern + '**');
|
21 | matchers.push(pattern + '**');
|
22 | }
|
23 | matchers.push(pattern);
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function addIgnoreFromInput(input) {
|
32 | let patterns = [];
|
33 | if (input) {
|
34 | patterns = patterns.concat(input);
|
35 | }
|
36 | patterns.forEach(addIgnorePattern);
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function addIgnoreFromFile(input) {
|
44 | let lines = [];
|
45 | let files = [];
|
46 | if (input) {
|
47 | files = files.concat(input);
|
48 | }
|
49 |
|
50 | files.forEach(function(config) {
|
51 | const stats = fs.statSync(config);
|
52 | if (stats.isFile()) {
|
53 | const content = fs.readFileSync(config, 'utf8');
|
54 | lines = lines.concat(content.split(/\r?\n/));
|
55 | }
|
56 | });
|
57 |
|
58 | lines.forEach(addIgnorePattern);
|
59 | }
|
60 |
|
61 | function shouldIgnore(path) {
|
62 | const matched = matchers.length ? mm.isMatch(path, matchers, { dot:true }) : false;
|
63 | return matched;
|
64 | }
|
65 |
|
66 | exports.add = addIgnoreFromInput;
|
67 | exports.addFromFile = addIgnoreFromFile;
|
68 | exports.shouldIgnore = shouldIgnore;
|