1
1
const { execFileSync, spawnSync } = require('child_process');
2
- const { existsSync, readFileSync, statSync } = require('fs');
2
+ const { existsSync, readFileSync, statSync, writeFileSync } = require('fs');
3
3
const { arch, platform } = require('os');
4
- const { dirname, join, resolve } = require('path');
4
+ const { basename, dirname, join, resolve } = require('path');
5
5
const { quote } = require('shell-quote');
6
6
7
7
// Terminate early if this script is not invoked with the required arguments.
@@ -23,7 +23,7 @@ const osPlatform = platform();
23
23
const osPlatformArch = arch();
24
24
console.log(`Detected OS platform=${osPlatform} : arch=${osPlatformArch}`);
25
25
const codeqlExe = osPlatform === 'win32' ? 'codeql.exe' : 'codeql';
26
- const codeqlExePath = join(quote([process.env.CODEQL_DIST]), codeqlExe);
26
+ const codeqlExePath = resolve( join(quote([process.env.CODEQL_DIST]), codeqlExe) );
27
27
28
28
if (!existsSync(sourceRoot)) {
29
29
console.warn(`'${codeqlExe} database index-files --language cds' terminated early due to internal error: could not find project root directory '${sourceRoot}'.`);
@@ -61,9 +61,9 @@ if (!CODEQL_EXTRACTOR_JAVASCRIPT_ROOT) {
61
61
}
62
62
63
63
const autobuildScriptName = osPlatform === 'win32' ? 'autobuild.cmd' : 'autobuild.sh';
64
- const autobuildScriptPath = join(
64
+ const autobuildScriptPath = resolve( join(
65
65
CODEQL_EXTRACTOR_JAVASCRIPT_ROOT, 'tools', autobuildScriptName
66
- );
66
+ )) ;
67
67
68
68
/**
69
69
* Terminate early if:
@@ -104,7 +104,7 @@ let cdsCommand = 'cds';
104
104
try {
105
105
execFileSync('cds', ['--version'], { stdio: 'ignore' });
106
106
} catch {
107
- console.log('Pre-installing cds compiler');
107
+ console.log('Pre-installing cds compiler ... ');
108
108
109
109
// Use a JS `Set` to avoid duplicate processing of the same directory.
110
110
const packageJsonDirs = new Set();
@@ -156,18 +156,18 @@ try {
156
156
// Sanity check that we found at least one package.json directory from which the CDS
157
157
// compiler dependencies may be installed.
158
158
if (packageJsonDirs.size === 0) {
159
- console.warn('WARN: failed to detect any package.json directories for cds compiler installation.');
159
+ console.warn('WARN: failed to detect any package.json directories for cds compiler installation.');
160
160
exit(0);
161
161
}
162
162
163
163
packageJsonDirs.forEach((dir) => {
164
- console.log(`Installing '@sap/cds-dk' into ${dir} to enable CDS compilation.`);
164
+ console.log(`Installing '@sap/cds-dk' into ${dir} to enable CDS compilation .. .`);
165
165
execFileSync(
166
166
'npm',
167
- ['install', '--quiet', '--no-audit', '--no-fund', '@sap/cds-dk'],
167
+ ['install', '--quiet', '--no-audit', '--no-fund', '--no-save', ' @sap/cds-dk'],
168
168
{ cwd: dir, stdio: 'inherit' }
169
169
);
170
- console.log(`Installing node packages into ${dir} to enable CDS compilation.`);
170
+ console.log(`Installing node packages into ${dir} to enable CDS compilation .. .`);
171
171
execFileSync(
172
172
'npm',
173
173
['install', '--quiet', '--no-audit', '--no-fund'],
@@ -183,7 +183,7 @@ try {
183
183
cdsCommand = 'npx -y --package @sap/cds-dk cds';
184
184
}
185
185
186
- console.log('Processing CDS files to JSON');
186
+ console.log('Processing CDS files to JSON ... ');
187
187
188
188
/**
189
189
* Run the cds compile command on each file in the response files list, outputting the
@@ -192,33 +192,44 @@ console.log('Processing CDS files to JSON');
192
192
responseFiles.forEach(rawCdsFilePath => {
193
193
const cdsFilePath = quote([rawCdsFilePath]);
194
194
const cdsJsonFilePath = `${cdsFilePath}.json`;
195
- console.log(`Processing CDS file ${cdsFilePath} to: ${cdsJsonFilePath}`);
195
+ console.log(`Processing CDS file ${cdsFilePath} to ${cdsJsonFilePath} ... `);
196
196
const result = spawnSync(
197
197
cdsCommand,
198
- ['compile', cdsFilePath, '-2', 'json', '-o', cdsJsonFilePath, '--locations'],
199
- { shell: true }
198
+ [
199
+ 'compile', cdsFilePath,
200
+ '-2', 'json',
201
+ '--locations',
202
+ '--log-level', 'warn'
203
+ ],
204
+ { cwd: dirname(cdsFilePath), shell: true, stdio: 'pipe' }
200
205
);
201
- if (result.error || result.status !== 0) {
202
- const stderrTruncated = result.stderr.toString().split('\n').filter(line => line.startsWith('[ERROR]')).slice(-4).join('\n');
203
- const errorMessage = `Could not compile the file ${cdsFilePath}.\nReported error(s):\n\`\`\`\n${stderrTruncated}\n\`\`\``;
206
+ if (result.error || result.status !== 0 || !result.stdout) {
207
+ const errorMessage = `Could not compile the file ${cdsFilePath}.\nReported error(s):\n\`\`\`\n${result.stderr.toString()}\n\`\`\``;
204
208
console.log(errorMessage);
205
- execFileSync(
206
- codeqlExePath,
207
- [
208
- 'database',
209
- 'add-diagnostic',
210
- '--extractor-name=cds',
211
- '--ready-for-status-page',
212
- '--source-id=cds/compilation-failure',
213
- '--source-name="Failure to compile one or more SAP CAP CDS files"',
214
- '--severity=error',
215
- `--markdown-message="${errorMessage}"`,
216
- `--file-path="${cdsFilePath}"`,
217
- '--',
218
- `${process.env.CODEQL_EXTRACTOR_CDS_WIP_DATABASE}`
219
- ],
220
- );
209
+ try {
210
+ execFileSync(
211
+ codeqlExePath,
212
+ [
213
+ 'database',
214
+ 'add-diagnostic',
215
+ '--extractor-name=cds',
216
+ '--ready-for-status-page',
217
+ '--source-id=cds/compilation-failure',
218
+ '--source-name="Failure to compile one or more SAP CAP CDS files"',
219
+ '--severity=error',
220
+ `--markdown-message="${errorMessage}"`,
221
+ `--file-path="${cdsFilePath}"`,
222
+ '--',
223
+ `${process.env.CODEQL_EXTRACTOR_CDS_WIP_DATABASE}`
224
+ ],
225
+ );
226
+ console.log(`Added error diagnostic for source file: ${cdsFilePath}`);
227
+ } catch (err) {
228
+ console.error(`Failed to add error diagnostic for source file=${cdsFilePath} : ${err}`);
229
+ }
221
230
}
231
+ // Write the compiled JSON result to cdsJsonFilePath.
232
+ writeFileSync(cdsJsonFilePath, result.stdout);
222
233
});
223
234
224
235
let excludeFilters = '';
0 commit comments