sonarqube-scanner makes it very easy to trigger SonarQube
/ SonarCloud analyses on a JavaScript code base, without needing
to install any specific tool or (Java) runtime.
This module is analyzed on SonarCloud.
This package is available on npm as: sonarqube-scanner
To add code analysis to your build files, simply add the package to your project dev dependencies:
npm install -D sonarqube-scannerTo install the scanner globally and be able to run analyses on the command line:
npm install -g sonarqube-scannerPrerequisite: you've installed the package as a dev dependency.
The following example shows how to run an analysis on a JavaScript project, and pushing the results to a SonarQube instance:
const sonarqubeScanner = require('sonarqube-scanner');
sonarqubeScanner(
{
serverUrl : 'https://sonarqube.mycompany.com',
token : "019d1e2e04eefdcd0caee1468f39a45e69d33d3f",
options: {
'sonar.projectName': 'My App',
'sonar.projectDescription': 'Description for "My App" project...',
'sonar.sources': 'dist',
'sonar.tests': 'specs'
}
},
() => process.exit()
)Syntax: sonarqube-scanner ( parameters, [callback] )
Arguments
parametersMapserverUrlString (optional) The URL of the SonarQube server. Defaults to http://localhost:9000tokenString (optional) The token used to connect to the SonarQube server. Empty by default.optionsMap (optional) Used to pass extra parameters for the SonarQube analysis. See the official documentation for more details.
callbackFunction (optional) Callback (the execution of the analysis is asynchronous).
Prerequisite: you've installed the package globally.
If you want to run an analysis without having to configure anything in the first place, simply run the sonar-scanner command. The following
example assumes that you have installed SonarQube locally:
cd my-project
sonar-scanner
Specifying properties/settings
- If there's a
package.jsonfile in the folder, it will be read to feed the analysis with basic information (like project name or version) - If there's a
sonar-project.propertiesfile in the folder, it will behave like the original SonarQube Scanner - Additional analysis parameters can be passed on the command line using the standard
-Dsonar.xxx=yyysyntax-
Example:
sonar-scanner -Dsonar.host.url=https://myserver.com -Dsonar.login=019d1e2e04e
-
I constantly get "Impossible to download and extract binary [...] In such situation, the best solution is to install the standard SonarQube Scanner", what can I do?
You can install manually the standard SonarQube Scanner, which requires to have a Java Runtime Environment available too (Java 8+). Once this is done, you can replace the 2nd line of the example by:
var sonarqubeScanner = require('sonarqube-scanner').customScanner;You are probably relying on Alpine for your Docker image, and Alpine does not include glibc by default. It needs to be installed manually.
Thanks to Philipp Eschenbach for troubleshooting this on issue #59.
By default, the scanner binaries are downloaded from https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/.
To use a custom mirror, set $SONAR_SCANNER_MIRROR. Or download precise version with $SONAR_SCANNER_VERSION
Example:
export SONAR_SCANNER_MIRROR=https://npm.taobao.org/mirrors/sonar-scanner/
export SONAR_SCANNER_VERSION=3.2.0.1227or alternatively set variable in .npmrc
sonar_scanner_mirror=https://npm.taobao.org/mirrors/sonar-scanner/
sonar_scanner_version=3.2.0.1227
By default, the scanner binaries are cached into $HOME/.sonar/native-sonar-scanner folder.
To use a custom cache fodler instead of $HOME, set $SONAR_BINARY_CACHE.
Example:
export SONAR_BINARY_CACHE=/Users/myaccount/cacheor alternatively set variable in .npmrc
sonar_binary_cache=/Users/myaccount/cache
sonarqube-scanner is licensed under the LGPL v3 License.