1 |
|
2 | import * as pathUtil from 'path'
|
3 | import ignorePatterns from 'ignorepatterns'
|
4 |
|
5 | interface IgnoreOpts {
|
6 |
|
7 | ignorePaths?: false | Array<string>
|
8 |
|
9 | ignoreHiddenFiles?: boolean
|
10 |
|
11 | ignoreCommonPatterns?: boolean | RegExp
|
12 |
|
13 | ignoreCustomPatterns?: false | RegExp
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function isIgnoredPath(path: string, opts: IgnoreOpts = {}) {
|
24 |
|
25 | const basename = pathUtil.basename(path)
|
26 |
|
27 |
|
28 | if (opts.ignorePaths) {
|
29 | for (let i = 0; i < opts.ignorePaths.length; ++i) {
|
30 | const ignorePath = opts.ignorePaths[i]
|
31 | if (path.indexOf(ignorePath) === 0) {
|
32 | return true
|
33 | }
|
34 | }
|
35 | }
|
36 |
|
37 |
|
38 | if (opts.ignoreHiddenFiles && basename[0] === '.') {
|
39 | return true
|
40 | }
|
41 |
|
42 |
|
43 | if (opts.ignoreCommonPatterns == null || opts.ignoreCommonPatterns === true) {
|
44 | return (
|
45 | ignorePatterns.test(path) ||
|
46 | (path !== basename && ignorePatterns.test(basename))
|
47 | )
|
48 | } else if (opts.ignoreCommonPatterns) {
|
49 | const ignoreCommonPatterns = opts.ignoreCommonPatterns
|
50 | return (
|
51 | ignoreCommonPatterns.test(path) ||
|
52 | (path !== basename && ignoreCommonPatterns.test(basename))
|
53 | )
|
54 | }
|
55 |
|
56 |
|
57 | if (opts.ignoreCustomPatterns) {
|
58 | const ignoreCustomPatterns = opts.ignoreCustomPatterns
|
59 | return (
|
60 | ignoreCustomPatterns.test(path) ||
|
61 | (path !== basename && ignoreCustomPatterns.test(basename))
|
62 | )
|
63 | }
|
64 |
|
65 |
|
66 | return false
|
67 | }
|