1
1
const log = require ( "@ui5/logger" ) . getLogger ( "resources:adapters:AbstractAdapter" ) ;
2
2
const minimatch = require ( "minimatch" ) ;
3
3
const AbstractReaderWriter = require ( "../AbstractReaderWriter" ) ;
4
+ const Resource = require ( "../Resource" ) ;
4
5
5
6
/**
6
7
* Abstract Resource Adapter
@@ -45,6 +46,25 @@ class AbstractAdapter extends AbstractReaderWriter {
45
46
return [ ] ;
46
47
}
47
48
patterns = Array . prototype . concat . apply ( [ ] , patterns ) ;
49
+ if ( ! options . nodir ) {
50
+ for ( let i = patterns . length - 1 ; i >= 0 ; i -- ) {
51
+ const idx = this . _virBaseDir . indexOf ( patterns [ i ] ) ;
52
+ if ( patterns [ i ] && idx !== - 1 && idx < this . _virBaseDir . length ) {
53
+ const subPath = patterns [ i ] ;
54
+ return Promise . resolve ( [
55
+ new Resource ( {
56
+ project : this . project ,
57
+ statInfo : { // TODO: make closer to fs stat info
58
+ isDirectory : function ( ) {
59
+ return true ;
60
+ }
61
+ } ,
62
+ path : subPath
63
+ } )
64
+ ] ) ;
65
+ }
66
+ }
67
+ }
48
68
return this . _runGlob ( patterns , options , trace ) ;
49
69
} ) ;
50
70
}
@@ -58,6 +78,7 @@ class AbstractAdapter extends AbstractReaderWriter {
58
78
*/
59
79
_normalizePattern ( virPattern ) {
60
80
return Promise . resolve ( ) . then ( ( ) => {
81
+ const that = this ;
61
82
const mm = new minimatch . Minimatch ( virPattern ) ;
62
83
63
84
const basePathParts = this . _virBaseDir . split ( "/" ) ;
@@ -67,41 +88,50 @@ class AbstractAdapter extends AbstractReaderWriter {
67
88
for ( i = 0 ; i < basePathParts . length ; i ++ ) {
68
89
const globPart = subset [ i ] ;
69
90
if ( globPart === undefined ) {
70
- log . verbose ( "Ran out of glob parts to match. This should not happen." ) ;
71
- return - 42 ;
91
+ log . verbose ( "Ran out of glob parts to match (this should not happen):" ) ;
92
+ if ( that . _project ) { // project is optional
93
+ log . verbose ( `Project: ${ that . _project . metadata . name } ` ) ;
94
+ }
95
+ log . verbose ( `Virtual base path: ${ that . _virBaseDir } ` ) ;
96
+ log . verbose ( `Pattern to match: ${ virPattern } ` ) ;
97
+ log . verbose ( `Current subset (tried index ${ i } ):` ) ;
98
+ log . verbose ( subset ) ;
99
+ return { idx : i , virtualMatch : true } ;
72
100
}
73
101
const basePathPart = basePathParts [ i ] ;
74
102
if ( typeof globPart === "string" ) {
75
103
if ( globPart !== basePathPart ) {
76
- return - 42 ;
104
+ return null ;
77
105
} else {
78
106
continue ;
79
107
}
80
108
} else if ( globPart === minimatch . GLOBSTAR ) {
81
109
return i ;
82
110
} else { // Regex
83
111
if ( ! globPart . test ( basePathPart ) ) {
84
- return - 42 ;
112
+ return null ;
85
113
} else {
86
114
continue ;
87
115
}
88
116
}
89
117
}
90
118
if ( subset . length === basePathParts . length ) {
91
- return - 1 ;
119
+ return { rootMatch : true } ;
92
120
}
93
- return i ;
121
+ return { idx : i } ;
94
122
}
95
123
96
124
const resultGlobs = [ ] ;
97
125
for ( let i = 0 ; i < mm . set . length ; i ++ ) {
98
- const matchIdx = matchSubset ( mm . set [ i ] ) ;
99
- let resultPattern ;
100
- if ( matchIdx !== - 42 ) {
101
- if ( matchIdx === - 1 ) { // matched one up
126
+ const match = matchSubset ( mm . set [ i ] ) ;
127
+ if ( match ) {
128
+ let resultPattern ;
129
+ if ( match . virtualMatch ) {
130
+ resultPattern = basePathParts . slice ( 0 , match . idx ) . join ( "/" ) ;
131
+ } else if ( match . rootMatch ) { // matched one up
102
132
resultPattern = "" ; // root "/"
103
133
} else { // matched at some part of the glob
104
- resultPattern = mm . globParts [ i ] . slice ( matchIdx ) . join ( "/" ) ;
134
+ resultPattern = mm . globParts [ i ] . slice ( match . idx ) . join ( "/" ) ;
105
135
if ( resultPattern . startsWith ( "/" ) ) {
106
136
resultPattern = resultPattern . substr ( 1 ) ;
107
137
}
0 commit comments