@sonar/scan
Version:
SonarQube/SonarCloud Scanner for the JavaScript world
96 lines (95 loc) • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.scan = scan;
/*
* sonar-scanner-npm
* Copyright (C) 2022-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
const constants_1 = require("./constants");
const java_1 = require("./java");
const logging_1 = require("./logging");
const process_1 = require("./process");
const properties_1 = require("./properties");
const request_1 = require("./request");
const scanner_cli_1 = require("./scanner-cli");
const scanner_engine_1 = require("./scanner-engine");
const types_1 = require("./types");
async function scan(scanOptions, cliArgs) {
try {
await runScan(scanOptions, cliArgs);
}
catch (error) {
(0, logging_1.log)(logging_1.LogLevel.ERROR, `An error occurred: ${error}`);
throw error;
}
}
async function runScan(scanOptions, cliArgs) {
const startTimestampMs = Date.now();
const properties = (0, properties_1.getProperties)(scanOptions, startTimestampMs, cliArgs);
if (properties[types_1.ScannerProperty.SonarVerbose] === 'true') {
(0, logging_1.setLogLevel)(logging_1.LogLevel.DEBUG);
(0, logging_1.log)(logging_1.LogLevel.DEBUG, 'Setting the log level to DEBUG due to verbose mode');
}
if (properties[types_1.ScannerProperty.SonarLogLevel]) {
(0, logging_1.setLogLevel)(properties[types_1.ScannerProperty.SonarLogLevel]);
(0, logging_1.log)(logging_1.LogLevel.DEBUG, `Overriding the log level to ${properties[types_1.ScannerProperty.SonarLogLevel]}`);
}
(0, logging_1.log)(logging_1.LogLevel.DEBUG, 'Properties:', properties);
(0, logging_1.log)(logging_1.LogLevel.INFO, 'Platform:', properties[types_1.ScannerProperty.SonarScannerOs], properties[types_1.ScannerProperty.SonarScannerArch]);
await (0, request_1.initializeAxios)(properties);
(0, logging_1.log)(logging_1.LogLevel.INFO, `Server URL: ${properties[types_1.ScannerProperty.SonarHostUrl]}`);
(0, logging_1.log)(logging_1.LogLevel.INFO, `Version: 4.3.2`);
(0, logging_1.log)(logging_1.LogLevel.DEBUG, 'Check if Server supports JRE provisioning');
const supportsJREProvisioning = await (0, java_1.serverSupportsJREProvisioning)(properties);
(0, logging_1.log)(logging_1.LogLevel.INFO, `JRE provisioning ${supportsJREProvisioning ? 'is' : 'is NOT'} supported`);
if (!supportsJREProvisioning) {
(0, logging_1.log)(logging_1.LogLevel.INFO, 'Falling back on using sonar-scanner-cli');
if (scanOptions.localScannerCli) {
(0, logging_1.log)(logging_1.LogLevel.INFO, 'Local scanner is requested, will not download sonar-scanner-cli');
const scannerPath = await (0, process_1.locateExecutableFromPath)(constants_1.SCANNER_CLI_DEFAULT_BIN_NAME);
if (!scannerPath) {
throw new Error('SonarScanner CLI not found in PATH');
}
await (0, scanner_cli_1.runScannerCli)(scanOptions, properties, scannerPath);
}
else {
const binPath = await (0, scanner_cli_1.downloadScannerCli)(properties);
await (0, scanner_cli_1.runScannerCli)(scanOptions, properties, binPath);
}
return;
}
// Detect what Java to use (in path, specified from properties or provisioned)
let javaPath;
if (properties[types_1.ScannerProperty.SonarScannerJavaExePath]) {
javaPath = properties[types_1.ScannerProperty.SonarScannerJavaExePath];
}
else if (properties[types_1.ScannerProperty.SonarScannerSkipJreProvisioning] === 'true') {
const absoluteJavaPath = await (0, process_1.locateExecutableFromPath)('java');
if (!absoluteJavaPath) {
throw new Error('Java not found in PATH');
}
javaPath = absoluteJavaPath;
}
else {
javaPath = await (0, java_1.fetchJRE)(properties);
}
// Fetch the Scanner Engine
const latestScannerEngine = await (0, scanner_engine_1.fetchScannerEngine)(properties);
// Run the Scanner Engine
await (0, scanner_engine_1.runScannerEngine)(javaPath, latestScannerEngine, scanOptions, properties);
}