Skip to content

Commit 5bafe3d

Browse files
committed
Refactor CDS extractor for dedicated "cds" package
This commit: - Implements an initial version of a project-aware CDS parser. - Creates a dedicated "cds" package at "extractors/cds/tools/src/cds". - Converts existing unit tests to use the new path for functions related to parsing and/or compiling .cds files.
1 parent 9c50a3b commit 5bafe3d

File tree

9 files changed

+1954
-37
lines changed

9 files changed

+1954
-37
lines changed

extractors/cds/tools/index-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { compileCdsToJson, determineCdsCommand } from './src/cdsCompiler';
1+
import { compileCdsToJson, determineCdsCommand } from './src/cds';
22
import { runJavaScriptExtractor } from './src/codeql';
33
import { addCompilationDiagnostic } from './src/diagnostics';
44
import { configureLgtmIndexFilters, setupAndValidateEnvironment } from './src/environment';

extractors/cds/tools/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"child_process": "^1.0.2",
2020
"fs": "^0.0.1-security",
21+
"glob": "^11.0.2",
2122
"os": "^0.1.2",
2223
"path": "^0.12.7",
2324
"shell-quote": "^1.8.2"
@@ -26,7 +27,9 @@
2627
"@eslint/compat": "^1.2.8",
2728
"@eslint/eslintrc": "^3.3.1",
2829
"@eslint/js": "^9.25.1",
30+
"@types/glob": "^8.1.0",
2931
"@types/jest": "^29.5.14",
32+
"@types/mock-fs": "^4.13.4",
3033
"@types/node": "^22.15.3",
3134
"@types/shell-quote": "^1.7.5",
3235
"@typescript-eslint/eslint-plugin": "^8.31.1",
@@ -39,6 +42,7 @@
3942
"eslint-plugin-prettier": "^5.2.6",
4043
"globals": "^16.0.0",
4144
"jest": "^29.7.0",
45+
"mock-fs": "^5.5.0",
4246
"prettier": "^3.5.3",
4347
"ts-jest": "^29.3.2",
4448
"typescript": "^5.8.3"

extractors/cds/tools/src/cdsCompiler.ts renamed to extractors/cds/tools/src/cds/compiler.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execFileSync, spawnSync, SpawnSyncReturns } from 'child_process';
22
import { resolve } from 'path';
33

4-
import { fileExists, dirExists, recursivelyRenameJsonFiles } from './filesystem';
4+
import { fileExists, dirExists, recursivelyRenameJsonFiles } from '../filesystem';
55

66
/**
77
* Result of a CDS compilation
@@ -12,6 +12,26 @@ export interface CdsCompilationResult {
1212
outputPath?: string;
1313
}
1414

15+
/**
16+
* Determine the `cds` command to use based on the environment.
17+
* @returns A string representing the CLI command to run to invoke the
18+
* CDS compiler.
19+
*/
20+
export function determineCdsCommand(): string {
21+
let cdsCommand = 'cds';
22+
// TODO : create a mapping of project sub-directories to the correct
23+
// cds command to use, which will also determine the version of the cds
24+
// compiler that will be used for compiling `.cds` files to `.cds.json`
25+
// files for that sub-directory / project.
26+
try {
27+
execFileSync('cds', ['--version'], { stdio: 'ignore' });
28+
} catch {
29+
// If 'cds' command is not available, use npx to run it
30+
cdsCommand = 'npx -y --package @sap/cds-dk cds';
31+
}
32+
return cdsCommand;
33+
}
34+
1535
/**
1636
* Compile a CDS file to JSON
1737
* @param cdsFilePath Path to the CDS file
@@ -81,35 +101,3 @@ export function compileCdsToJson(
81101
return { success: false, message: String(error) };
82102
}
83103
}
84-
/**
85-
* Determine the `cds` command to use based on the environment.
86-
* @returns A string representing the CLI command to run to invoke the
87-
* CDS compiler.
88-
*/
89-
export function determineCdsCommand(): string {
90-
let cdsCommand = 'cds';
91-
// TODO : create a mapping of project sub-directories to the correct
92-
// cds command to use, which will also determine the version of the cds
93-
// compiler that will be used for compiling `.cds` files to `.cds.json`
94-
// files for that sub-directory / project.
95-
try {
96-
execFileSync('cds', ['--version'], { stdio: 'ignore' });
97-
} catch (error) {
98-
// Check if the error is specifically about the command not being found
99-
const errorMsg = String(error);
100-
if (errorMsg.includes('command not found')) {
101-
// If 'cds' command is not available, use npx to run it
102-
console.log('CDS command not found, falling back to npx...');
103-
} else if (errorMsg.includes('ENOENT') || errorMsg.includes('not recognized')) {
104-
// If the error is related to the command not being recognized, use npx
105-
console.log('CDS command not recognized, falling back to npx...');
106-
} else {
107-
// For other errors, log them but still fall back to npx
108-
console.warn(
109-
`WARN: determining CDS command failed with error: ${errorMsg}. Falling back to npx...`,
110-
);
111-
}
112-
cdsCommand = 'npx -y --package @sap/cds-dk cds';
113-
}
114-
return cdsCommand;
115-
}

extractors/cds/tools/src/cds/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './compiler';
2+
export * from './parser';
3+
export * from './parserTypes';

0 commit comments

Comments
 (0)