11import { execFileSync , spawnSync , SpawnSyncReturns } from 'child_process' ;
22import { resolve } from 'path' ;
33
4- import * as shellQuote from 'shell-quote' ;
5-
64import { fileExists , dirExists , recursivelyRenameJsonFiles } from './filesystem' ;
75
86/**
@@ -14,39 +12,6 @@ export interface CdsCompilationResult {
1412 outputPath ?: string ;
1513}
1614
17- /**
18- * Determine the `cds` command to use based on the environment.
19- * @returns A string representing the CLI command to run to invoke the
20- * CDS compiler.
21- */
22- export function determineCdsCommand ( ) : string {
23- let cdsCommand = 'cds' ;
24- // TODO : create a mapping of project sub-directories to the correct
25- // cds command to use, which will also determine the version of the cds
26- // compiler that will be used for compiling `.cds` files to `.cds.json`
27- // files for that sub-directory / project.
28- try {
29- execFileSync ( 'cds' , [ '--version' ] , { stdio : 'ignore' } ) ;
30- } catch ( error ) {
31- // Check if the error is specifically about the command not being found
32- const errorMsg = String ( error ) ;
33- if ( errorMsg . includes ( 'command not found' ) ) {
34- // If 'cds' command is not available, use npx to run it
35- console . log ( 'CDS command not found, falling back to npx...' ) ;
36- } else if ( errorMsg . includes ( 'ENOENT' ) || errorMsg . includes ( 'not recognized' ) ) {
37- // If the error is related to the command not being recognized, use npx
38- console . log ( 'CDS command not recognized, falling back to npx...' ) ;
39- } else {
40- // For other errors, log them but still fall back to npx
41- console . warn (
42- `WARN: determining CDS command failed with error: ${ errorMsg } . Falling back to npx...` ,
43- ) ;
44- }
45- cdsCommand = 'npx -y --package @sap/cds-dk cds' ;
46- }
47- return cdsCommand ;
48- }
49-
5015/**
5116 * Compile a CDS file to JSON
5217 * @param cdsFilePath Path to the CDS file
@@ -116,42 +81,35 @@ export function compileCdsToJson(
11681 return { success : false , message : String ( error ) } ;
11782 }
11883}
119-
12084/**
121- * Add a diagnostic error to the CodeQL database for a failed CDS compilation
122- * @param cdsFilePath Path to the CDS file that failed to compile
123- * @param errorMessage The error message from the compilation
124- * @param codeqlExePath Path to the CodeQL executable
125- * @returns True if the diagnostic was added, false otherwise
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.
12688 */
127- export function addCompilationDiagnostic (
128- cdsFilePath : string ,
129- errorMessage : string ,
130- codeqlExePath : string ,
131- ) : boolean {
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.
13295 try {
133- // Use shell-quote to safely escape the error message
134- const escapedErrorMessage = shellQuote . quote ( [ errorMessage ] ) ;
135-
136- execFileSync ( codeqlExePath , [
137- 'database' ,
138- 'add-diagnostic' ,
139- '--extractor-name=cds' ,
140- '--ready-for-status-page' ,
141- '--source-id=cds/compilation-failure' ,
142- '--source-name=Failure to compile one or more SAP CAP CDS files' ,
143- '--severity=error' ,
144- `--markdown-message=${ escapedErrorMessage . slice ( 1 , - 1 ) } ` , // Remove the added quotes from shell-quote
145- `--file-path=${ resolve ( cdsFilePath ) } ` ,
146- '--' ,
147- `${ process . env . CODEQL_EXTRACTOR_CDS_WIP_DATABASE ?? '' } ` ,
148- ] ) ;
149- console . log ( `Added error diagnostic for source file: ${ cdsFilePath } ` ) ;
150- return true ;
151- } catch ( err ) {
152- console . error (
153- `ERROR: Failed to add error diagnostic for source file=${ cdsFilePath } : ${ String ( err ) } ` ,
154- ) ;
155- return false ;
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' ;
156113 }
114+ return cdsCommand ;
157115}
0 commit comments