uxp-linter-test-app
Version:
UXP LINTER is useful for linting your code with ESLint rules and guidelines.
80 lines (67 loc) • 3.23 kB
JavaScript
;
require("v8-compile-cache");
const path = require('path'),
fs = require('fs');
const utils = require('./utils');
const sonarPropFileName = 'sonar-project.properties';
const sonarPropFilePath = path.join(__dirname, sonarPropFileName);
async function initializeSonarProperties() {
await utils.addConfigFile(sonarPropFilePath, path.join(__dirname, `./../../../sonar-project.properties`), true);
await utils.updateLinterConfig({'sonar-properties-initialized': true});
console.log('\x1b[32m%s\x1b[0m', `\n\nSonar project properties file has been initialized at ./${sonarPropFileName}. Please configure the desired properties values as per your server configuration.
\n\nPlease make sure sonar.externalIssuesReportPaths correctly points to Sonar report created through JS Linter, to see all the issues on your Sonar dashboard.`);
return true;
}
async function uninitializeSonarProperties() {
await utils.removeConfigFile(sonarPropFileName);
console.log('\x1b[32m%s\x1b[0m', `\nSonar project properties removed successfully.`);
return true;
}
async function askUserToInitializeSonar(rl) {
return new Promise(function (resolve, reject) {
rl.question(`\nDo you want to import JS Linter report on SonarQube dashboard? Y/N (Y) :`, async function (value) {
value = value.toUpperCase();
if (value.length === 0 || value === 'Y') {
await initializeSonarProperties();
} else {
await utils.updateLinterConfig({'sonar-properties-initialized': false});
}
resolve();
})
})
}
const createSonarConfigJson = (reportFilePath) => {
let uxpEslintRules = fs.readFileSync(path.join(__dirname, '../../eslint-config-impetus-basetest/rules/master-ruleset-eslint.json'));
uxpEslintRules = JSON.parse(uxpEslintRules)
let issues = [];
let sonarConfig = { uxpEslintRules }
let jsonReport = fs.readFileSync(reportFilePath);
jsonReport = JSON.parse(jsonReport)
jsonReport.forEach((file) => {
let t = file.messages.map(el => {
let error = {}
error.engineId = "UXP-LINTER";
error.ruleId = el.ruleId;
error.severity = sonarConfig[el.ruleId] ? sonarConfig["severity"] : "MAJOR";
error.type = sonarConfig[el.ruleId] ? sonarConfig["type"] : "CODE_SMELL";
error.effortMinutes = sonarConfig[el.effortMinutes] ? sonarConfig[el.effortMinutes] : 5;
error.primaryLocation = {
"message": el.message,
"filePath": file.filePath,
"textRange": {
"startLine": el.line,
"endLine": el.endLine,
"startColumn": el.column - 1,
"endColumn": el.endColumn - 1
}
}
return error;
})
issues = issues.concat(t)
})
let sonarIssues = { "issues": [...issues] }
sonarIssues = JSON.stringify(sonarIssues);
fs.writeFileSync(reportFilePath, sonarIssues);
}
module.exports = { initializeSonarProperties, uninitializeSonarProperties, createSonarConfigJson, askUserToInitializeSonar };