@@ -27,6 +27,24 @@ class Memory extends AbstractAdapter {
2727 this . _virDirs = Object . create ( null ) ; // map full of directories
2828 }
2929
30+ /**
31+ * Matches and returns resources from a given map (either _virFiles or _virDirs).
32+ *
33+ * @private
34+ * @param {string[] } patterns
35+ * @param {object } resourceMap
36+ * @returns {Promise<module:@ui5/fs.Resource[]> }
37+ */
38+ async _matchPatterns ( patterns , resourceMap ) {
39+ const resourcePaths = Object . keys ( resourceMap ) ;
40+ const matchedPaths = micromatch ( resourcePaths , patterns , {
41+ dot : true
42+ } ) ;
43+ return Promise . all ( matchedPaths . map ( ( virPath ) => {
44+ return resourceMap [ virPath ] && resourceMap [ virPath ] . clone ( ) ;
45+ } ) ) ;
46+ }
47+
3048 /**
3149 * Locate resources by glob.
3250 *
@@ -55,22 +73,11 @@ class Memory extends AbstractAdapter {
5573 ] ;
5674 }
5775
58- const filePaths = Object . keys ( this . _virFiles ) ;
59- const matchedFilePaths = micromatch ( filePaths , patterns , {
60- dot : true
61- } ) ;
62- let matchedResources = matchedFilePaths . map ( ( virPath ) => {
63- return this . _virFiles [ virPath ] ;
64- } ) ;
76+ let matchedResources = await this . _matchPatterns ( patterns , this . _virFiles ) ;
6577
6678 if ( ! options . nodir ) {
67- const dirPaths = Object . keys ( this . _virDirs ) ;
68- const matchedDirs = micromatch ( dirPaths , patterns , {
69- dot : true
70- } ) ;
71- matchedResources = matchedResources . concat ( matchedDirs . map ( ( virPath ) => {
72- return this . _virDirs [ virPath ] ;
73- } ) ) ;
79+ const matchedDirs = await this . _matchPatterns ( patterns , this . _virDirs ) ;
80+ matchedResources = matchedResources . concat ( matchedDirs ) ;
7481 }
7582
7683 return matchedResources ;
@@ -85,28 +92,25 @@ class Memory extends AbstractAdapter {
8592 * @param {@ui5/fs/tracing.Trace } trace Trace instance
8693 * @returns {Promise<@ui5/fs/Resource> } Promise resolving to a single resource
8794 */
88- _byPath ( virPath , options , trace ) {
95+ async _byPath ( virPath , options , trace ) {
8996 if ( this . isPathExcluded ( virPath ) ) {
90- return Promise . resolve ( null ) ;
97+ return null ;
98+ }
99+ if ( ! virPath . startsWith ( this . _virBasePath ) && virPath !== this . _virBaseDir ) {
100+ // Neither starts with basePath, nor equals baseDirectory
101+ return null ;
91102 }
92- return new Promise ( ( resolve , reject ) => {
93- if ( ! virPath . startsWith ( this . _virBasePath ) && virPath !== this . _virBaseDir ) {
94- // Neither starts with basePath, nor equals baseDirectory
95- resolve ( null ) ;
96- return ;
97- }
98103
99- const relPath = virPath . substr ( this . _virBasePath . length ) ;
100- trace . pathCall ( ) ;
104+ const relPath = virPath . substr ( this . _virBasePath . length ) ;
105+ trace . pathCall ( ) ;
101106
102- const resource = this . _virFiles [ relPath ] ;
107+ const resource = this . _virFiles [ relPath ] ;
103108
104- if ( ! resource || ( options . nodir && resource . getStatInfo ( ) . isDirectory ( ) ) ) {
105- resolve ( null ) ;
106- } else {
107- resolve ( resource ) ;
108- }
109- } ) ;
109+ if ( ! resource || ( options . nodir && resource . getStatInfo ( ) . isDirectory ( ) ) ) {
110+ return null ;
111+ } else {
112+ return await resource . clone ( ) ;
113+ }
110114 }
111115
112116 /**
@@ -119,42 +123,39 @@ class Memory extends AbstractAdapter {
119123 async _write ( resource ) {
120124 resource = await this . _migrateResource ( resource ) ;
121125 super . _write ( resource ) ;
122- return new Promise ( ( resolve , reject ) => {
123- const relPath = resource . getPath ( ) . substr ( this . _virBasePath . length ) ;
124- log . silly ( "Writing to virtual path %s" , resource . getPath ( ) ) ;
125- this . _virFiles [ relPath ] = resource ;
126-
127- // Add virtual directories for all path segments of the written resource
128- // TODO: Add tests for all this
129- const pathSegments = relPath . split ( "/" ) ;
130- pathSegments . pop ( ) ; // Remove last segment representing the resource itself
126+ const relPath = resource . getPath ( ) . substr ( this . _virBasePath . length ) ;
127+ log . silly ( "Writing to virtual path %s" , resource . getPath ( ) ) ;
128+ this . _virFiles [ relPath ] = await resource . clone ( ) ;
131129
132- pathSegments . forEach ( ( segment , i ) => {
133- if ( i >= 1 ) {
134- segment = pathSegments [ i - 1 ] + "/" + segment ;
135- }
136- pathSegments [ i ] = segment ;
137- } ) ;
130+ // Add virtual directories for all path segments of the written resource
131+ // TODO: Add tests for all this
132+ const pathSegments = relPath . split ( "/" ) ;
133+ pathSegments . pop ( ) ; // Remove last segment representing the resource itself
138134
139- for ( let i = pathSegments . length - 1 ; i >= 0 ; i -- ) {
140- const segment = pathSegments [ i ] ;
141- if ( ! this . _virDirs [ segment ] ) {
142- this . _virDirs [ segment ] = this . _createResource ( {
143- project : this . _project ,
144- source : {
145- adapter : "Memory"
146- } ,
147- statInfo : { // TODO: make closer to fs stat info
148- isDirectory : function ( ) {
149- return true ;
150- }
151- } ,
152- path : this . _virBasePath + segment
153- } ) ;
154- }
135+ pathSegments . forEach ( ( segment , i ) => {
136+ if ( i >= 1 ) {
137+ segment = pathSegments [ i - 1 ] + "/" + segment ;
155138 }
156- resolve ( ) ;
139+ pathSegments [ i ] = segment ;
157140 } ) ;
141+
142+ for ( let i = pathSegments . length - 1 ; i >= 0 ; i -- ) {
143+ const segment = pathSegments [ i ] ;
144+ if ( ! this . _virDirs [ segment ] ) {
145+ this . _virDirs [ segment ] = this . _createResource ( {
146+ project : this . _project ,
147+ source : {
148+ adapter : "Memory"
149+ } ,
150+ statInfo : { // TODO: make closer to fs stat info
151+ isDirectory : function ( ) {
152+ return true ;
153+ }
154+ } ,
155+ path : this . _virBasePath + segment
156+ } ) ;
157+ }
158+ }
158159 }
159160}
160161
0 commit comments