uxp-linter-test-app
Version:
UXP LINTER is useful for linting your code with ESLint rules and guidelines.
76 lines (65 loc) • 2.48 kB
JavaScript
;
require("v8-compile-cache");
var constants = require('./constants');
// var sonarConfig = require('./sonar-config');
var utils = require('./utils');
//Allowed report formats are mentioned below:
//html, checkstyle, compact, jslint-xml, json-with-metadata, json, junit, stylish, tap, unix, visualstudio, sonar
const { exec } = require('child_process');
const { allowedFormats, outputDirectory } = constants;
let reportFilePath = '';
async function generateReport() {
var nodeArgs = require('minimist')(process.argv.slice(2));
let reportFormat = !!nodeArgs._[0] ? nodeArgs._[0].toLowerCase() : 'html';
let lintingType = !!nodeArgs.ext && nodeArgs.ext === 'html' ? 'html-lint' : 'lint';
const execCommand = await createReportCommand(reportFormat, lintingType);
if (execCommand) {
executeReportCommand(execCommand, reportFormat);
} else {
console.error('\x1b[31m%s\x1b[0m', '\nError: Please select a valid report command!');
}
}
function executeReportCommand(execCommand, reportFormat) {
exec(execCommand, (error, data, getter) => {
if (error && getter) {
console.error('\x1b[31m%s\x1b[0m', `\nError: ${getter} ${reportFilePath}`);
throw error;
}
if (reportFormat === 'sonar') {
sonarConfig.createSonarConfigJson(reportFilePath);
}
console.log('\x1b[32m%s\x1b[0m', '\nReport generated successfully. Please check -> ', reportFilePath);
});
}
async function createReportCommand(reportFormat, lintingType) {
console.log(`\nYou have selected ${reportFormat} report format.`);
let commonCommand;
let fileNamePrefix;
if (lintingType === 'lint') {
commonCommand = await utils.getLintCommand('js', 'Report');
fileNamePrefix = 'uxp-lint-report';
} else if (lintingType === 'html-lint') {
commonCommand = await utils.getLintCommand('html', 'Report');
fileNamePrefix = 'uxp-html-lint-report';
} else {
return;
}
if (commonCommand === '') {
return;
}
let execCommand;
allowedFormats.forEach(format => {
if (format.type === reportFormat) {
reportFilePath = `${outputDirectory}/${fileNamePrefix}-${reportFormat}.${format.ext}`;
if (reportFormat === "sonar") {
execCommand = `${commonCommand} --format json > ${reportFilePath}`;
return;
}
execCommand = `${commonCommand} --format ${reportFormat} > ${reportFilePath}`;
return;
}
});
return execCommand;
}
module.exports = generateReport();