What is sonar-scanner?
The sonar-scanner npm package is a tool for running SonarQube analysis on your projects. It helps in identifying bugs, vulnerabilities, and code smells in your codebase by integrating with SonarQube, a popular static code analysis tool.
What are sonar-scanner's main functionalities?
Basic Analysis
This feature allows you to perform a basic analysis of your project by specifying the server URL, project key, source directory, and authentication token.
const scanner = require('sonar-scanner');
scanner({
serverUrl: 'http://localhost:9000',
options: {
'sonar.projectKey': 'my-project',
'sonar.sources': './src',
'sonar.login': 'your-sonar-token'
}
}, () => process.exit());
Custom Configuration
This feature allows you to customize the analysis configuration by specifying additional options such as file exclusions and programming language.
const scanner = require('sonar-scanner');
scanner({
serverUrl: 'http://localhost:9000',
options: {
'sonar.projectKey': 'my-project',
'sonar.sources': './src',
'sonar.exclusions': '**/*.test.js',
'sonar.language': 'js',
'sonar.login': 'your-sonar-token'
}
}, () => process.exit());
Multi-module Project Analysis
This feature allows you to analyze multi-module projects by specifying the modules and their respective base directories.
const scanner = require('sonar-scanner');
scanner({
serverUrl: 'http://localhost:9000',
options: {
'sonar.projectKey': 'my-multi-module-project',
'sonar.modules': 'module1,module2',
'module1.sonar.projectBaseDir': './module1',
'module2.sonar.projectBaseDir': './module2',
'sonar.login': 'your-sonar-token'
}
}, () => process.exit());
Other packages similar to sonar-scanner
eslint
ESLint is a popular linting tool for JavaScript and TypeScript. It helps in identifying and fixing problems in your codebase. Unlike sonar-scanner, which integrates with SonarQube for a broader range of static code analysis, ESLint focuses specifically on linting and code style enforcement.
jshint
JSHint is another linting tool for JavaScript. It is similar to ESLint but with a different set of rules and configurations. While sonar-scanner provides a more comprehensive analysis by integrating with SonarQube, JSHint focuses solely on JavaScript code quality.
stylelint
Stylelint is a linter for CSS and other style sheet languages. It helps in enforcing consistent conventions and avoiding errors in stylesheets. Unlike sonar-scanner, which is used for general static code analysis, Stylelint is specialized for style sheet languages.
node-sonar-scanner
Wrap SonarQube Scanner as a node module.
Installation
You can install node-sonar-scanner as a development dependency and add it as a script property in your package.json.
npm i sonar-scanner --save-dev
{
"scripts": {
"sonar-scanner": "node_modules/sonar-scanner/bin/sonar-scanner"
}
}
npm run sonar-scanner