@@ -4,6 +4,7 @@ import fg from '@snyk/fast-glob';
44import micromatch from 'micromatch' ;
55import crypto from 'crypto' ;
66import union from 'lodash.union' ;
7+ import difference from 'lodash.difference' ;
78import util from 'util' ;
89import { Cache } from './cache' ;
910import { HASH_ALGORITHM , ENCODE_TYPE , MAX_PAYLOAD , IGNORES_DEFAULT , IGNORE_FILES_NAMES , CACHE_KEY } from './constants' ;
@@ -76,11 +77,14 @@ export function parseFileIgnores(path: string): string[] {
7677 }
7778
7879 return rules . map ( rule => {
79- if ( rule . startsWith ( '/' ) || rule . startsWith ( '**' ) ) {
80- return nodePath . posix . join ( dirname , rule ) ;
80+ let prefix = "" ;
81+ if ( rule . startsWith ( '!' ) ) {
82+ rule = rule . substring ( 1 ) ;
83+ prefix = "!" ;
8184 }
82-
83- return nodePath . posix . join ( dirname , '**' , rule ) ;
85+ return rule . startsWith ( '/' ) || rule . startsWith ( '**' )
86+ ? prefix + nodePath . posix . join ( dirname , rule , '**' )
87+ : prefix + nodePath . posix . join ( dirname , '**' , rule , '**' ) ;
8488 } ) ;
8589}
8690
@@ -107,7 +111,6 @@ export async function collectIgnoreRules(
107111 IGNORE_FILES_NAMES . map ( i => `*${ i } ` ) ,
108112 {
109113 ...fgOptions ,
110- ignore : fileIgnores ,
111114 cwd : folder ,
112115 followSymbolicLinks : symlinksEnabled ,
113116 } ,
@@ -132,25 +135,22 @@ export function determineBaseDir(paths: string[]): string {
132135 return '' ;
133136}
134137
135- function searchFiles (
138+ async function * searchFiles (
136139 patterns : string [ ] ,
137140 cwd : string ,
138141 symlinksEnabled : boolean ,
139142 ignores : string [ ] ,
140- ) : NodeJS . ReadableStream {
141- const relIgnores = ignores . map ( i => {
142- if ( i . startsWith ( cwd ) ) {
143- return i . slice ( cwd . length + 1 ) ;
144- }
145- return i ;
146- } ) ;
147-
148- return fg . stream ( patterns , {
143+ ) : AsyncGenerator < string | Buffer > {
144+ const searcher = fg . stream ( patterns , {
149145 ...fgOptions ,
150146 cwd,
151- ignore : relIgnores ,
152147 followSymbolicLinks : symlinksEnabled ,
153148 } ) ;
149+ for await ( const filePath of searcher ) {
150+ if ( filterIgnoredFiles ( [ filePath . toString ( ) ] , ignores ) . length ) {
151+ yield filePath ;
152+ }
153+ }
154154}
155155
156156/**
@@ -224,19 +224,14 @@ export async function prepareExtendingBundle(
224224 // Filter for supported extensions/files only
225225 let processingFiles : string [ ] = filterSupportedFiles ( files , supportedFiles ) ;
226226
227- // Exclude files to be ignored based on ignore rules. We assume here, that ignore rules have not been changed
228- processingFiles = micromatch (
229- processingFiles ,
230- fileIgnores . map ( p => `!${ p } ` ) ,
231- microMatchOptions ,
232- ) ;
227+ // Exclude files to be ignored based on ignore rules. We assume here, that ignore rules have not been changed.
228+ processingFiles = filterIgnoredFiles ( processingFiles , fileIgnores ) ;
233229
234230 if ( processingFiles . length ) {
235231 // Determine existing files (minus removed)
236232 const entries = await fg ( processingFiles , {
237233 ...fgOptions ,
238234 cwd : baseDir ,
239- ignore : fileIgnores ,
240235 followSymbolicLinks : symlinksEnabled ,
241236 objectMode : true ,
242237 stats : true ,
@@ -391,3 +386,11 @@ export function* composeFilePayloads(files: IFileInfo[], bucketSize = MAX_PAYLOA
391386 yield bucket . files ;
392387 }
393388}
389+
390+ export function filterIgnoredFiles (
391+ filePaths : string [ ] ,
392+ ignores : string [ ] ,
393+ ) {
394+ const ignored = micromatch ( filePaths , ignores , { ...microMatchOptions , basename : false } ) ;
395+ return difference ( filePaths , ignored ) ;
396+ }
0 commit comments