Skip to content

Commit 9950b88

Browse files
authored
refactor: parallelize CDS file discovery and rename setModel to getModel (#23)
1 parent 043e580 commit 9950b88

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

lib/setModel.js renamed to lib/getModel.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import fs from 'fs'
33
import 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
112113
async 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)

lib/tools.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cds from '@sap/cds'
22
import { z } from 'zod'
3-
import setModel from './setModel.js'
3+
import getModel from './getModel.js'
44
import fuzzyTopN from './fuzzyTopN.js'
55

66
const tools = {
@@ -16,16 +16,16 @@ const tools = {
1616
namesOnly: z.boolean().default(false).describe('If true, only return definition names (for overview)')
1717
},
1818
handler: async ({ projectPath, name, kind, topN, namesOnly }) => {
19-
await setModel(projectPath)
19+
const model = await getModel(projectPath)
2020
const defNames = kind
21-
? Object.entries(cds.model.definitions)
21+
? Object.entries(model.definitions)
2222
// eslint-disable-next-line no-unused-vars
2323
.filter(([_k, v]) => v.kind === kind)
2424
.map(([k]) => k)
25-
: Object.keys(cds.model.definitions)
25+
: Object.keys(model.definitions)
2626
const scores = name ? fuzzyTopN(name, defNames, topN) : fuzzyTopN('', defNames, topN)
2727
if (namesOnly) return scores.map(s => s.item)
28-
return scores.map(s => cds.model.definitions[s.item])
28+
return scores.map(s => model.definitions[s.item])
2929
}
3030
}
3131
}

0 commit comments

Comments
 (0)