@@ -3,12 +3,12 @@ import fs from 'fs'
33import path from 'path'
44
55// Ensures only one CDS model compilation is ever in-flight.
6- // The moment setModel is called, cds.model is set to a promise.
7- export default async function setModel ( projectPath ) {
6+ // The moment getModel is called, cds.model is set to a promise.
7+ export default async function getModel ( projectPath ) {
88 if ( cds . model ) {
99 // If cds.model is a promise, await it; if it's resolved, return it
1010 if ( typeof cds . model . then === 'function' ) await cds . model
11- return
11+ return cds . model
1212 }
1313 // Assign a promise immediately to cds.model to prevent duplicate compilations
1414 cds . model = ( async ( ) => {
@@ -18,6 +18,7 @@ export default async function setModel(projectPath) {
1818 } ) ( )
1919
2020 await cds . model
21+ return cds . model
2122}
2223
2324// Loads and compiles the CDS model, returns the compiled model or throws on error
@@ -112,18 +113,20 @@ let changeWatcher = null
112113async function cdsFilesChanged ( projectPath ) {
113114 // Recursively find all .cds files under root, ignoring node_modules
114115 async function findCdsFiles ( dir ) {
115- let results = [ ]
116116 const entries = await fs . promises . readdir ( dir , { withFileTypes : true } )
117- for ( const entry of entries ) {
117+ const promises = entries . map ( async entry => {
118118 const fullPath = path . join ( dir , entry . name )
119119 if ( entry . isDirectory ( ) ) {
120- if ( entry . name === 'node_modules' ) continue
121- results = results . concat ( await findCdsFiles ( fullPath ) )
120+ if ( entry . name === 'node_modules' ) return [ ]
121+ return await findCdsFiles ( fullPath )
122122 } else if ( entry . isFile ( ) && entry . name . endsWith ( '.cds' ) ) {
123- results . push ( fullPath )
123+ return [ fullPath ]
124+ } else {
125+ return [ ]
124126 }
125- }
126- return results
127+ } )
128+ const results = await Promise . all ( promises )
129+ return results . flat ( )
127130 }
128131
129132 if ( projectPath . endsWith ( '/' ) ) projectPath = projectPath . slice ( 0 , - 1 )
0 commit comments