@@ -12,62 +12,64 @@ const IGNORE_PATH_PREFIX: &str = "_";
12
12
/// It uses glob patterns to detect the workers and
13
13
/// provide utilities to work with public folders and
14
14
/// other related resources.
15
- pub struct Files {
15
+ pub struct Files < ' t > {
16
16
/// Root path
17
17
root : PathBuf ,
18
18
/// Defines pattern for files considered as workers
19
- include_pattern : String ,
19
+ include_pattern : Glob < ' t > ,
20
20
/// Defines patterns to exclude when traversing for workers
21
- ignore_patterns : Vec < String > ,
21
+ ignore_patterns : Vec < Glob < ' t > > ,
22
22
}
23
23
24
- impl Files {
24
+ impl < ' t > Files < ' t > {
25
+ const PUBLIC_ASSETS_FOLDER : & str = "public" ;
25
26
const DEFAULT_EXTENSIONS : [ & str ; 2 ] = [ "js" , "wasm" ] ;
26
27
27
28
/// Initializes a new files instance. It will detect
28
29
/// relevant resources for WWS like the public folder.
29
30
pub fn new ( root : & Path , file_extensions : Vec < String > , ignore_patterns : Vec < String > ) -> Self {
30
31
Self {
31
32
root : root. to_path_buf ( ) ,
32
- include_pattern : Self :: construct_include_pattern ( file_extensions) ,
33
- ignore_patterns : Self :: construct_ignore_patterns ( ignore_patterns) ,
33
+ include_pattern : Self :: build_include_pattern ( file_extensions) ,
34
+ ignore_patterns : Self :: build_ignore_patterns ( ignore_patterns) ,
34
35
}
35
36
}
36
37
37
38
/// Walk through all the different files associated to this
38
39
/// project using a Glob pattern
39
40
pub fn walk ( & self ) -> Vec < WalkEntry > {
40
- let include_pattern = Glob :: from_str ( self . include_pattern . as_str ( ) ) . expect (
41
- "Failed to parse include pattern when processing files in the current directory" ,
42
- ) ;
43
-
44
- return include_pattern
41
+ return self
42
+ . include_pattern
45
43
. walk ( & self . root )
46
- . not ( self . ignore_patterns . iter ( ) . map ( |s| s. as_str ( ) ) )
47
- . expect (
48
- "Failed to parse ignore patterns when processing files in the current directory" ,
49
- )
44
+ . not ( self . ignore_patterns . clone ( ) )
45
+ . expect ( "Failed to walk the tree when processing files in the current directory" )
50
46
. map ( |e| e. unwrap ( ) )
51
47
. collect ( ) ;
52
48
}
53
49
54
- fn construct_include_pattern ( file_extensions : Vec < String > ) -> String {
50
+ fn build_include_pattern ( file_extensions : Vec < String > ) -> Glob < ' t > {
55
51
let mut file_extensions = file_extensions;
56
52
for default_extension in Self :: DEFAULT_EXTENSIONS {
57
53
file_extensions. push ( default_extension. to_string ( ) ) ;
58
54
}
59
55
60
- format ! ( "**/*.{{{}}}" , file_extensions. join( "," ) )
56
+ let include_pattern = format ! ( "**/*.{{{}}}" , file_extensions. join( "," ) ) ;
57
+ Glob :: from_str ( include_pattern. as_str ( ) ) . expect ( "Failed to parse include pattern!" )
61
58
}
62
59
63
- fn construct_ignore_patterns ( ignore_patterns : Vec < String > ) -> Vec < String > {
64
- let mut result = vec ! [
65
- "**/public /**" . to_string ( ) ,
60
+ fn build_ignore_patterns ( ignore_patterns : Vec < String > ) -> Vec < Glob < ' t > > {
61
+ let default_ignore_patterns = vec ! [
62
+ format! ( "**/{} /**" , Self :: PUBLIC_ASSETS_FOLDER ) ,
66
63
format!( "**/{}/**" , STORE_FOLDER ) ,
67
64
format!( "**/{}*/**" , IGNORE_PATH_PREFIX ) ,
68
65
] ;
66
+
67
+ let mut result = default_ignore_patterns;
69
68
result. extend ( ignore_patterns) ;
70
69
result
70
+ . iter ( )
71
+ . map ( |s| Glob :: from_str ( s. as_str ( ) ) . expect ( "Failed to parse ignore pattern" ) )
72
+ . collect ( )
71
73
}
72
74
}
73
75
@@ -177,7 +179,7 @@ mod tests {
177
179
}
178
180
179
181
#[ test]
180
- fn walk_ignore2 ( ) {
182
+ fn walk_ignore_multiple_patterns ( ) {
181
183
let files = Files :: new (
182
184
Path :: new ( "tests/data/files" ) ,
183
185
vec ! [ "ext" . to_string( ) , "none" . to_string( ) ] ,
0 commit comments