1
- const { execFileSync, execSync , spawnSync } = require ( 'child_process' ) ;
1
+ const { execFileSync, spawnSync } = require ( 'child_process' ) ;
2
2
const { existsSync, readFileSync, statSync } = require ( 'fs' ) ;
3
3
const { arch, platform } = require ( 'os' ) ;
4
4
const { dirname, join, resolve } = require ( 'path' ) ;
@@ -13,6 +13,12 @@ const osPlatformArch = arch();
13
13
console . log ( `Detected OS platform=${ osPlatform } : arch=${ osPlatformArch } ` ) ;
14
14
const codeqlExe = osPlatform === 'win32' ? 'codeql.exe' : 'codeql' ;
15
15
const codeqlExePath = join ( quote ( [ process . env . CODEQL_DIST ] ) , codeqlExe ) ;
16
+ const projectRootDir = resolve ( `${ dirname ( __filename ) } /../../..` ) ;
17
+
18
+ if ( ! existsSync ( projectRootDir ) ) {
19
+ console . warn ( `'${ codeqlExe } database index-files --language cds' terminated early due to internal error: could not find project root directory '${ projectRootDir } '.` ) ;
20
+ process . exit ( 0 ) ;
21
+ }
16
22
17
23
let CODEQL_EXTRACTOR_JAVASCRIPT_ROOT = process . env . CODEQL_EXTRACTOR_JAVASCRIPT_ROOT
18
24
? quote ( [ process . env . CODEQL_EXTRACTOR_JAVASCRIPT_ROOT ] )
@@ -122,11 +128,7 @@ try {
122
128
typeof packageJsonData . dependencies === 'object'
123
129
) {
124
130
const dependencyNames = Object . keys ( packageJsonData . dependencies ) ;
125
- if (
126
- dependencyNames . includes ( '@sap/cds' )
127
- &&
128
- dependencyNames . includes ( '@sap/cds-dk' )
129
- ) {
131
+ if ( dependencyNames . includes ( '@sap/cds' ) ) {
130
132
packageJsonDirs . add ( dir ) ;
131
133
break ;
132
134
}
@@ -138,14 +140,20 @@ try {
138
140
}
139
141
} ) ;
140
142
141
- // TODO : revise this check as the equality is probably not guaranteed.
142
- if ( responseFiles . length !== packageJsonDirs . size ) {
143
- console . warn (
144
- ` WARN: mismatch between number of response files ( ${ responseFiles . length } ) and package.json directories ( ${ packageJsonDirs . length } )`
145
- ) ;
143
+ // Sanity check that we found at least one package.json directory from which the CDS
144
+ // compiler dependencies may be installed.
145
+ if ( packageJsonDirs . size === 0 ) {
146
+ console . warn ( ' WARN: failed to detect any package.json directories for cds compiler installation.' ) ;
147
+ exit ( 0 ) ;
146
148
}
147
149
148
150
packageJsonDirs . forEach ( ( dir ) => {
151
+ console . log ( `Installing '@sap/cds-dk' into ${ dir } to enable CDS compilation.` ) ;
152
+ execFileSync (
153
+ 'npm' ,
154
+ [ 'install' , '--quiet' , '--no-audit' , '--no-fund' , '@sap/cds-dk' ] ,
155
+ { cwd : dir , stdio : 'inherit' }
156
+ ) ;
149
157
console . log ( `Installing node packages into ${ dir } to enable CDS compilation.` ) ;
150
158
execFileSync (
151
159
'npm' ,
@@ -244,19 +252,26 @@ process.env.LGTM_INDEX_FILETYPES = '.cds:JSON';
244
252
// refer to .js or .ts files.
245
253
delete process . env . LGTM_INDEX_INCLUDE ;
246
254
247
- console . log ( 'Extracting the .cds.json files' ) ;
248
-
249
- console . log ( `Running 'javascript' extractor autobuild script: ${ autobuildScriptPath } ` ) ;
255
+ console . log (
256
+ `Extracting the .cds.json files by running the 'javascript' extractor autobuild script:
257
+ ${ autobuildScriptPath } `
258
+ ) ;
250
259
/**
251
260
* Invoke the javascript autobuilder to index the .cds.json files only.
252
261
*
253
262
* Environment variables must be passed from this script's process to the
254
263
* process that invokes the autobuild script, otherwise the CDS autobuild.sh
255
264
* script will not be invoked by the autobuild script built into the
256
265
* 'javascript' extractor.
266
+ *
267
+ * IMPORTANT: The JavaScript extractor autobuild script must be invoked with
268
+ * the current working directory set to the project root directory because it
269
+ * assumes it is running from there. Without the `cwd` property set to the
270
+ * project root directory, the autobuild script will not detect the .cds.json
271
+ * files as being in the project and will not index them.
257
272
*/
258
273
spawnSync (
259
274
autobuildScriptPath ,
260
275
[ ] ,
261
- { env : process . env , shell : true , stdio : 'inherit' }
276
+ { cwd : projectRootDir , env : process . env , shell : true , stdio : 'inherit' }
262
277
) ;
0 commit comments