1
1
import * as core from '@actions/core'
2
2
import { readFileSync } from 'fs'
3
+ import ignore from 'ignore'
3
4
4
5
export class Exclude {
5
6
constructor ( ) {
6
7
this . path = core . getInput ( 'exclude_file' )
7
8
this . gitTrackedOnly = core . getBooleanInput ( 'use_gitignore' )
8
9
9
10
// initialize the exclude array
10
- this . exclude = [ ]
11
+ this . ignore = ignore ( )
11
12
12
13
// read the exclude file if it was used
13
14
if ( this . path && this . path !== '' ) {
14
15
core . debug ( `loading exclude_file: ${ this . path } ` )
15
-
16
- this . exclude = readFileSync ( this . path , 'utf8' )
17
- // split the exclude file into an array of strings and trim each string
18
- this . exclude = this . exclude . split ( '\n' ) . map ( item => item . trim ( ) )
19
- // remove any empty strings
20
- this . exclude = this . exclude . filter ( item => item !== '' )
21
- // remove any comments
22
- this . exclude = this . exclude . filter ( item => ! item . startsWith ( '#' ) )
23
-
24
- core . debug ( `loaded exclude patterns: ${ this . exclude } ` )
16
+ this . ignore . add ( readFileSync ( this . path , 'utf8' ) . toString ( ) )
17
+ core . debug ( `loaded custom exclude patterns` )
25
18
}
26
19
27
20
// if gitTrackOnly is true, add the git exclude patterns from the .gitignore file if it exists
28
21
if ( this . gitTrackedOnly ) {
29
22
core . debug (
30
- `use_gitignore: ${ this . gitTrackedOnly } - only using git tracked files `
23
+ `use_gitignore: ${ this . gitTrackedOnly } `
31
24
)
32
25
33
26
const gitIgnorePath = core . getInput ( 'git_ignore_path' )
34
- var gitIgnoreExclude = [ ]
35
- try {
36
- const gitIgnore = readFileSync ( gitIgnorePath , 'utf8' )
37
- // split the git ignore file into an array of strings and trim each string
38
- const gitIgnorePatterns = gitIgnore . split ( '\n' ) . map ( item => item . trim ( ) )
39
- // remove any empty strings
40
- gitIgnoreExclude = gitIgnorePatterns . filter ( item => item !== '' )
41
- // remove any comments
42
- gitIgnoreExclude = gitIgnoreExclude . filter (
43
- item => ! item . startsWith ( '#' )
44
- )
27
+ core . debug ( `loading .gitignore file from path: ${ gitIgnorePath } ` )
45
28
46
- // add the git ignore patterns to the exclude patterns
47
- this . exclude = this . exclude . concat ( gitIgnoreExclude )
29
+ try {
30
+ this . ignore . add ( readFileSync ( gitIgnorePath ) . toString ( ) )
48
31
} catch ( error ) {
49
32
core . warning ( `error reading .gitignore file: ${ error } ` )
50
33
}
@@ -54,62 +37,7 @@ export class Exclude {
54
37
isExcluded ( file ) {
55
38
// use .gitignore style matching
56
39
// https://git-scm.com/docs/gitignore
57
-
58
- if ( this . exclude . length === 0 ) {
59
- return false
60
- }
61
-
62
- // remove the leading ./ if it exists
63
- if ( file . startsWith ( './' ) ) {
64
- file = file . replace ( './' , '' )
65
- }
66
-
67
- // loop through each exclude pattern
68
- for ( const pattern of this . exclude ) {
69
- // if the file exactly matches the pattern, return true
70
- if ( file === pattern ) {
71
- core . debug ( `file exactly matches exclude pattern: ${ pattern } ` )
72
- return true
73
- }
74
-
75
- // if the pattern is a negation, check if the file matches the negation
76
- if ( pattern . startsWith ( '!' ) ) {
77
- const regex = new RegExp ( pattern . replace ( / ^ ! / , '' ) )
78
- if ( file . match ( regex ) ) {
79
- core . debug ( `file matches exclude negation pattern: ${ pattern } ` )
80
- return false
81
- }
82
- }
83
-
84
- // if the pattern is a directory, check if the file is in that directory
85
- if ( pattern . endsWith ( '/' ) ) {
86
- if ( file . startsWith ( pattern ) ) {
87
- core . debug ( `file is in exclude directory: ${ pattern } ` )
88
- return true
89
- }
90
- }
91
-
92
- // if the pattern is a glob, check if the file matches the glob
93
- if ( pattern . includes ( '*' ) ) {
94
- const regex = new RegExp ( pattern . replace ( / \* / g, '.*' ) )
95
- if ( file . match ( regex ) ) {
96
- core . debug ( `file matches exclude glob pattern: ${ pattern } ` )
97
- return true
98
- }
99
- }
100
-
101
- // if the pattern is a regex, check if the file matches the regex
102
- if ( pattern . startsWith ( '/' ) && pattern . endsWith ( '/' ) ) {
103
- const regex = new RegExp ( pattern . replace ( / \/ / g, '' ) )
104
- if ( file . match ( regex ) ) {
105
- core . debug ( `file matches exclude regex pattern: ${ pattern } ` )
106
- return true
107
- }
108
- }
109
- }
110
-
111
- // if the file did not match any exclude patterns, return false
112
- core . debug ( `file '${ file } ' did not match any exclude patterns` )
113
- return false
40
+ // https://github.com/kaelzhang/node-ignore
41
+ return this . ignore . ignores ( file )
114
42
}
115
43
}
0 commit comments