1
1
import { execFileSync , spawnSync , SpawnSyncReturns } from 'child_process' ;
2
2
import { resolve } from 'path' ;
3
3
4
- import { fileExists , dirExists , recursivelyRenameJsonFiles } from './filesystem' ;
4
+ import { fileExists , dirExists , recursivelyRenameJsonFiles } from '.. /filesystem' ;
5
5
6
6
/**
7
7
* Result of a CDS compilation
@@ -12,6 +12,26 @@ export interface CdsCompilationResult {
12
12
outputPath ?: string ;
13
13
}
14
14
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
+
15
35
/**
16
36
* Compile a CDS file to JSON
17
37
* @param cdsFilePath Path to the CDS file
@@ -81,35 +101,3 @@ export function compileCdsToJson(
81
101
return { success : false , message : String ( error ) } ;
82
102
}
83
103
}
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