11import { execFileSync , spawnSync , SpawnSyncReturns } from 'child_process' ;
22import { 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- }
0 commit comments