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