uxp-linter-test-app
Version:
UXP LINTER is useful for linting your code with ESLint rules and guidelines.
262 lines (240 loc) • 11.5 kB
JavaScript
;
require("v8-compile-cache");
var readline = require("readline"),
fs = require('fs'),
path = require('path'),
uxpLinter = require('./uxp-linter-config'),
htmlLinter = require('./html-linter-config'),
cssLinter = require('./css-linter-config'),
prettier = require('./prettier-config'),
sonarConfig = require('./sonar-config'),
utils = require('./utils'),
constants = require('./constants'),
ScriptData = require('./script-data'),
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const { allLinterOptions, jsLintCommand, jsReportCommand, htmlLintCommand, htmlReportCommand,
cssLintCommand, cssReportCommand, prettierLintCommand, defaultLinterPath } = constants
// For asking user for required package
async function askQuestion() {
if (!fs.existsSync(path.join(__dirname, './../../../.uxplinterrc.json'))) {
await utils.initializeLinterConfig();
}
let filterOptions = [];
if (!await uxpLinter.isJsLinterInstalled()) {
filterOptions.push(allLinterOptions[0]);
} else {
filterOptions.push(allLinterOptions[4]);
filterOptions.push(allLinterOptions[5]);
filterOptions.push(allLinterOptions[9]);
filterOptions.push(allLinterOptions[10]);
// if (!fs.existsSync('./sonar-project.properties')) {
// filterOptions.push(allLinterOptions[11]);
// }
// else {
// filterOptions.push(allLinterOptions[12]);
// }
}
// For HTML Linter
if (!await htmlLinter.isHtmlLinterInstalled()) {
filterOptions.push(allLinterOptions[1]);
} else {
filterOptions.push(allLinterOptions[6]);
}
// For CSS Linter
if (!await cssLinter.isCssLinterInstalled()) {
filterOptions.push(allLinterOptions[2]);
} else {
filterOptions.push(allLinterOptions[7]);
}
// For Prettier
if (!await prettier.isPrettierInstalled()) {
filterOptions.push(allLinterOptions[3]);
} else {
filterOptions.push(allLinterOptions[8]);
}
if (filterOptions.indexOf(allLinterOptions[4]) < 0 && (filterOptions.indexOf(allLinterOptions[6]) > 0
|| filterOptions.indexOf(allLinterOptions[7]) > 0 || filterOptions.indexOf(allLinterOptions[8]) > 0)) {
filterOptions.push(allLinterOptions[4]);
}
filterOptions.sort(function (opt1, opt2) {
return opt1.option - opt2.option;
});
let initMessage = `\nHow would you like to use UXP Linter?\n`;
for (let i = 0; i < filterOptions.length; i++) {
initMessage += '\n' + (i + 1) + '. ' + filterOptions[i].text;
}
initMessage += `\n\nYou can enter multiple options separated by comma(,) : `;
let installCodes = [0, 1, 2, 3];
let uninstallCodes = [4, 5, 6, 7, 8];
rl.question(initMessage, async function (initInput) {
let selectOptions = initInput.split(',').map(function (opt) {
let parseOption = parseInt(opt.trim(), 10);
//Validation for invalid option selection
if (!(parseOption > 0 && parseOption <= filterOptions.length)) {
console.log('\x1b[31m%s\x1b[0m', '\nPlease provide valid options.');
process.exit();
}
return parseOption;
});
let options = [];
for (let i = 0; i < selectOptions.length; i++) {
let filterOpt = filterOptions[(selectOptions[i] - 1)];
options.push(filterOpt.option);
}
options.sort(function (a, b) {
return a - b;
});
let hasInstallCodes = installCodes.some(ic => options.includes(ic));
let hasUninstallCodes = uninstallCodes.some(uc => options.includes(uc));
if (hasInstallCodes && hasUninstallCodes) {
console.log('\x1b[32m%s\x1b[0m', '\nYou shouldn\'t provide install and uninstall both options together.');
process.exit();
}
let updateConfig = {};
for (let i = 0; i < options.length; i++) {
let opt = options[i];
switch (opt) {
case 0:
await utils.askPathIfNotExist(rl);
await uxpLinter.configureLinter(rl);
// if (!fs.existsSync('./sonar-project.properties')) {
// await sonarConfig.askUserToInitializeSonar(rl);
// }
updateConfig = {'js-linter': true,
'jsLintCommand': jsLintCommand,
'jsReportCommand': jsReportCommand }
await utils.updateLinterConfig(updateConfig);
await manageUxpLinterScriptCmds();
const rulesProfile = utils.readLinterConfig('linterProfile')
console.log('\x1b[32m%s\x1b[0m', `\n${rulesProfile} rules profile has been successfully configured for JS Linter.`);
break;
case 1:
await utils.askPathIfNotExist(rl);
console.log('\x1b[32m%s\x1b[0m', '\nStarting: HTML Linter installation...');
await htmlLinter.configureHtmlLinter('init');
updateConfig = {'html-linter': true,
'htmlLintCommand': htmlLintCommand,
'htmlReportCommand': htmlReportCommand }
await utils.updateLinterConfig(updateConfig);
await manageUxpLinterScriptCmds();
break;
case 2:
await utils.askPathIfNotExist(rl);
console.log('\x1b[32m%s\x1b[0m', '\nStarting: CSS Linter installation...');
await cssLinter.configureCssLinter('init');
updateConfig = {'css-linter': true,
'cssLintCommand': cssLintCommand,
'cssReportCommand': cssReportCommand }
await utils.updateLinterConfig(updateConfig);
await manageUxpLinterScriptCmds();
break;
case 3:
await utils.askPathIfNotExist(rl);
console.log('\x1b[32m%s\x1b[0m', '\nStarting: Prettier installation...');
await prettier.configurePrettier('init');
updateConfig = {'prettier': true,
'prettierLintCommand': prettierLintCommand }
await utils.updateLinterConfig(updateConfig);
await manageUxpLinterScriptCmds();
break;
case 4:
console.log('\x1b[33m%s\x1b[0m', '\nUninstalling packages. Please wait...');
await htmlLinter.configureHtmlLinter('remove');
await cssLinter.configureCssLinter('remove');
await prettier.configurePrettier('remove');
await uxpLinter.configureLinter(rl, 'remove', 'all');
await manageUxpLinterScriptCmds('removeAll');
break;
case 5:
console.log('\x1b[32m%s\x1b[0m', '\nStarting: JS Linter un-installation...');
await uxpLinter.configureLinter(rl, 'remove', 'js');
await utils.updateLinterConfig({'js-linter': false});
await utils.deleteLinterConfig(['jsLintCommand', 'jsReportCommand']);
await manageUxpLinterScriptCmds();
break;
case 6:
console.log('\x1b[32m%s\x1b[0m', '\nStarting: HTML Linter un-installation...');
await htmlLinter.configureHtmlLinter('remove');
await utils.updateLinterConfig({'html-linter': false});
await utils.deleteLinterConfig(['htmlLintCommand', 'htmlReportCommand']);
await manageUxpLinterScriptCmds();
break;
case 7:
console.log('\x1b[32m%s\x1b[0m', '\nStarting: CSS Linter un-installation...');
await cssLinter.configureCssLinter('remove');
await utils.updateLinterConfig({'css-linter': false});
await utils.deleteLinterConfig(['cssLintCommand', 'cssReportCommand']);
await manageUxpLinterScriptCmds();
break;
case 8:
console.log('\x1b[32m%s\x1b[0m', '\nStarting: Prettier un-installation...');
await prettier.configurePrettier('remove');
await utils.updateLinterConfig({'prettier': false});
await utils.deleteLinterConfig(['prettierLintCommand']);
await manageUxpLinterScriptCmds();
break;
case 9:
await uxpLinter.selectLinterRule(rl, i, options);
break;
case 10:
const currentPath = await utils.getLinterPath();
await utils.askPathToLint(rl, currentPath || defaultLinterPath);
break;
// case 11:
// await sonarConfig.initializeSonarProperties();
// break;
// case 12:
// await sonarConfig.uninitializeSonarProperties();
// break;
default:
console.log('\x1b[32m%s\x1b[0m', '\nInvalid option received - ' + opt + '');
}
}
process.exit();
});
}
// For adding/removing scripts
async function manageUxpLinterScriptCmds(manageParam) {
return new Promise(async function (resolve, reject) {
const linterMainCmd = 'uxp-linter-cmd';
let addScriptFlag = false;
let uxpLinterRunCmd = `${linterMainCmd} run`,
uxpLinterFixCmd = `${linterMainCmd} fix`,
uxpLinterReportCmd = `${linterMainCmd} report`,
uxpLinterUpdateCmd = `${linterMainCmd} update`;
if (!manageParam) {
let isJsLinter = await uxpLinter.isJsLinterInstalled();
let isHtmlLinter = await htmlLinter.isHtmlLinterInstalled();
let isCssLinter = await cssLinter.isCssLinterInstalled();
let isPrettier = await prettier.isPrettierInstalled();
if (isJsLinter || isHtmlLinter || isCssLinter || isPrettier) {
addScriptFlag = true;
}
}
if (addScriptFlag) {
let scripts = [];
let sd = '';
sd = new ScriptData('uxp-linter-run', uxpLinterRunCmd);
scripts.push(sd);
sd = new ScriptData('uxp-linter-fix', uxpLinterFixCmd);
scripts.push(sd);
sd = new ScriptData('uxp-linter-report', uxpLinterReportCmd);
scripts.push(sd);
sd = new ScriptData('uxp-linter-update', uxpLinterUpdateCmd);
scripts.push(sd);
await utils.addScriptPackageJSON(scripts);
} else {
const removeScripts = ['uxp-linter-run', 'uxp-linter-fix', 'uxp-linter-report', 'uxp-linter-update', 'prettier-run'];
if (manageParam && manageParam === 'removeAll') {
removeScripts.push('uxp-linter-init');
}
await utils.removeScriptPackageJSON(removeScripts);
}
resolve(true);
});
}
module.exports = askQuestion();