diff --git a/code_quality.js b/code_quality.js index b14c36e77a..0387648783 100644 --- a/code_quality.js +++ b/code_quality.js @@ -3,6 +3,7 @@ const prettier = require('prettier'); const prettierConfig = require('./prettier_config.json'); const codeQualityConfig = require('./package.json').codeQuality || {}; const path = require('path'); +const { exec } = require('child_process'); const fs = require('fs'); const { ESLint } = require('eslint'); @@ -17,54 +18,54 @@ const ansiup = new AnsiUp(); * @returns {[]}: list of files (absolut path) */ const getAllFiles = () => { - const options = getOptions(); - if (!options.sourcePaths) { - return getAllFilesRecoursive(__dirname); - } - const sourcePaths = getSourcePaths(options.sourcePaths); - let results = []; - sourcePaths.forEach(sourcePath => { - const files = getAllFilesRecoursive(sourcePath); - results = results.concat(files); - }); - return results; + const options = getOptions(); + if (!options.sourcePaths) { + return getAllFilesRecoursive(__dirname); + } + const sourcePaths = getSourcePaths(options.sourcePaths); + let results = []; + sourcePaths.forEach(sourcePath => { + const files = getAllFilesRecoursive(sourcePath); + results = results.concat(files); + }); + return results; }; const getSourcePaths = sourcePaths => { - return sourcePaths.split(',').map(sourcePath => path.resolve(__dirname, ...sourcePath.split('/'))); + return sourcePaths.split(',').map(sourcePath => path.resolve(__dirname, ...sourcePath.split('/'))); }; const getAllFilesRecoursive = dir => { - let results = []; - const list = fs.readdirSync(dir); - list.forEach(function (file) { - file = dir + '/' + file; - const stat = fs.statSync(file); - if (fileToExclude(file, stat)) { - return []; - } - if (stat && stat.isDirectory()) { - /* Recurse into a subdirectory */ - results = results.concat(getAllFilesRecoursive(file)); - } else { - /* Is a file */ - results.push(file); - } - }); - return results; + let results = []; + const list = fs.readdirSync(dir); + list.forEach(function(file) { + file = dir + '/' + file; + const stat = fs.statSync(file); + if (fileToExclude(file, stat)) { + return []; + } + if (stat && stat.isDirectory()) { + /* Recurse into a subdirectory */ + results = results.concat(getAllFilesRecoursive(file)); + } else { + /* Is a file */ + results.push(file); + } + }); + return results; }; const fileToExclude = (file, stat) => { - if (stat && stat.isDirectory()) { - return ( - file.endsWith('node_modules') || - file.indexOf('.git') !== -1 || - file.indexOf('/dist/') !== -1 || - file.indexOf('\\dist\\') !== -1 - ); - } - const fileName = path.basename(file); - return /(.git|.min.css|.min.js)$/.test(fileName); + if (stat && stat.isDirectory()) { + return ( + file.endsWith('node_modules') || + file.indexOf('.git') !== -1 || + file.indexOf('/dist/') !== -1 || + file.indexOf('\\dist\\') !== -1 + ); + } + const fileName = path.basename(file); + return /(.git|.min.css|.min.js)$/.test(fileName); }; /** @@ -72,8 +73,8 @@ const fileToExclude = (file, stat) => { * @returns {Promise<*>}: promise wiht list of files. */ const getChangedFiles = async () => { - const committedGitFiles = await gitChangedFiles(); - return committedGitFiles.unCommittedFiles.filter(file => fs.existsSync(file) && !file.endsWith('package-lock.json')); + const committedGitFiles = await gitChangedFiles(); + return committedGitFiles.unCommittedFiles.filter(file => fs.existsSync(file) && !file.endsWith('package-lock.json')); }; /** @@ -82,22 +83,22 @@ const getChangedFiles = async () => { * @returns {key: 'File extension:, value: Array of file absolute paths} */ const groupFilesByExtension = files => { - return files.reduce((map, file) => { - try { - if (file.startsWith('./') || file.indexOf('.') === -1) { - return map; - } - - const extension = file.substring(file.lastIndexOf('.') + 1).toLowerCase(); - const array = map[extension] || []; - array.push(file); - map[extension] = array; - return map; - } catch (e) { - console.error('error --> ', e); - return {}; + return files.reduce((map, file) => { + try { + if (file.startsWith('./') || file.indexOf('.') === -1) { + return map; } - }, {}); + + const extension = file.substring(file.lastIndexOf('.') + 1).toLowerCase(); + const array = map[extension] || []; + array.push(file); + map[extension] = array; + return map; + } catch (e) { + console.error('error --> ', e); + return {}; + } + }, {}); }; /** @@ -106,17 +107,17 @@ const groupFilesByExtension = files => { * @param config: configuration that will be used to prettier the file. */ const prettifyFile = (file, config) => { - try { - const text = fs.readFileSync(file).toString(); - const pretty = prettier.format(text, config); - if (text === pretty) { - return; - } - console.log('Running prettier the file ' + file); - fs.writeFileSync(file, pretty); - } catch (error) { - console.log('Error in prettier the file ' + file + ': \n' + error); - } + try { + const text = fs.readFileSync(file).toString(); + const pretty = prettier.format(text, config); + if (text === pretty) { + return; + } + console.log('Running prettier on the file: ' + file); + fs.writeFileSync(file, pretty); + } catch (error) { + console.log('Error in running prettier the file ' + file + ': \n' + error); + } }; /** @@ -124,22 +125,20 @@ const prettifyFile = (file, config) => { * @param filesByExtension: we pass a Map json object where the key is the extension, value is an Array with absolute file paths */ const prettifyFiles = filesByExtension => { - if (!codeQualityConfig.usePrettier) { - return; // no need to use pretty; - } - Object.keys(filesByExtension).forEach(extension => { - const files = filesByExtension[extension]; - const config = prettierConfig[extension]; - if (!config) { - console.log( - "We don't have any configuration for extension " + - extension + - ', please add it in file prettier_config.json' - ); - return; - } - files.forEach(file => prettifyFile(file, config)); - }); + if (!codeQualityConfig.usePrettier) { + return; // no need to use pretty; + } + Object.keys(filesByExtension).forEach(extension => { + const files = filesByExtension[extension]; + const config = prettierConfig[extension]; + if (!config) { + console.log( + "We don't have any configuration for extension " + extension + ', please add it in file prettier_config.json' + ); + return; + } + files.forEach(file => prettifyFile(file, config)); + }); }; /** @@ -148,29 +147,29 @@ const prettifyFiles = filesByExtension => { * @returns {Promise<{}|{report: string, error: boolean}>} */ const eslintFiles = async files => { - if (!codeQualityConfig.useEslint) { - return {}; - } - - const formatter = await eslint.loadFormatter('stylish'); - let error = false; - let report = ''; - for (const file of files) { - try { - const result = await eslint.lintFiles(file); - error = error || result.some(res => res.errorCount > 0); - await ESLint.outputFixes(result); - const resultText = formatter.format(result); - if (!!resultText && resultText.trim().length > 0) { - report += resultText; - } - } catch (exception) { - error = true; - report += 'Error in eslint file ' + file + ':\n' + exception; + if (!codeQualityConfig.useEslint) { + return {}; + } + + const formatter = await eslint.loadFormatter('stylish'); + let error = false; + let report = ''; + for (const file of files) { + try { + const result = await eslint.lintFiles(file); + error = error || result.some(res => res.errorCount > 0); + await ESLint.outputFixes(result); + const resultText = formatter.format(result); + if (!!resultText && resultText.trim().length > 0) { + report += resultText; } - } + } catch (exception) { + error = true; + report += 'Error in eslint file ' + file + ':\n' + exception; + } + } - return { error, report }; + return { error, report }; }; /** @@ -178,17 +177,17 @@ const eslintFiles = async files => { * You can also call this function using: npm run code-quality-prettier */ const preCommitPrettier = async filesByExtension => { - if (!filesByExtension) { - const files = await getChangedFiles(); - if (!files) { - console.log("Couldn't find any file that hand been changed"); - return; - } - console.log('File to be analyzed before commit:\n' + files.join('\n')); - filesByExtension = groupFilesByExtension(files); - } + if (!filesByExtension) { + const files = await getChangedFiles(); + if (!files) { + console.log("Couldn't find any file that hand been changed"); + return; + } + console.log('File to be analyzed before commit:\n' + files.join('\n')); + filesByExtension = groupFilesByExtension(files); + } - prettifyFiles(filesByExtension); + prettifyFiles(filesByExtension); }; /** @@ -196,26 +195,26 @@ const preCommitPrettier = async filesByExtension => { * You can also call this function using: npm run code-quality-eslint */ const preCommitEslint = async filesByExtension => { - if (!filesByExtension) { - const files = await getChangedFiles(); - if (!files) { - console.log("Couldn't find any file that hand been changed"); - return; - } - console.log('File to be analyzed before commit:\n' + files.join('\n')); - filesByExtension = groupFilesByExtension(files); - } - - const esLintResult = await eslintFilesByExtension(filesByExtension); - if (esLintResult.error) { - console.log('Please fix these errors before to commit:' + esLintResult.report); - console.log('Eslint executed in ' + esLintResult.numberFiles + ' files '); - if (codeQualityConfig.eslintStopCommit) { - process.exit(1); - } - } else { - console.log('Eslint executed in ' + esLintResult.numberFiles + ' files '); - } + if (!filesByExtension) { + const files = await getChangedFiles(); + if (!files) { + console.log("Couldn't find any file that hand been changed"); + return; + } + console.log('File to be analyzed before commit:\n' + files.join('\n')); + filesByExtension = groupFilesByExtension(files); + } + + const esLintResult = await eslintFilesByExtension(filesByExtension); + if (esLintResult.error) { + console.log('Please fix these errors before to commit:' + esLintResult.report); + console.log('Eslint executed in ' + esLintResult.numberFiles + ' files '); + if (codeQualityConfig.eslintStopCommit) { + process.exit(1); + } + } else { + console.log('Eslint executed in ' + esLintResult.numberFiles + ' files '); + } }; /** @@ -223,24 +222,34 @@ const preCommitEslint = async filesByExtension => { * You can also call this function using: npm run code-quality */ const preCommit = async () => { - const files = await getChangedFiles(); - if (!files) { - console.log("Couldn't find any file that hand been changed"); - return; - } - console.log('File to be analyzed before commit:\n' + files.join('\n')); - const filesByExtension = groupFilesByExtension(files); - if (codeQualityConfig.usePrettier) { - await preCommitPrettier(filesByExtension); - } else { - console.log('Prettier is disabled. Skipping...'); - } - - if (codeQualityConfig.usePrettier) { - await preCommitEslint(filesByExtension); - } else { - console.log('ESlint is disabled. Skipping...'); - } + const files = await getChangedFiles(); + if (!files) { + console.log("Couldn't find any file that hand been changed"); + return; + } + console.log('File to be analyzed before commit:\n' + files.join('\n')); + const filesByExtension = groupFilesByExtension(files); + if (codeQualityConfig.usePrettier) { + console.log('\x1b[33m%s\x1b[0m', 'Running Prettier in pre-commit'); + await preCommitPrettier(filesByExtension); + } else { + console.log('Prettier is disabled. Skipping...'); + } + + if (codeQualityConfig.useEslint) { + console.log('\x1b[33m%s\x1b[0m', 'Running ESlint in pre-commit'); + await preCommitEslint(filesByExtension); + } else { + console.log('ESlint is disabled. Skipping...'); + } + + console.log('Adding any possible changes to the commit'); + exec('git add', (err, stdout, stderr) => { + // handle err, stdout & stderr + if (err) { + console.log('Error with `git add`', error.message); + } + }); }; /** @@ -249,21 +258,21 @@ const preCommit = async () => { * @returns {Promise<{report: string, numberFiles: number, error: boolean}>} */ const eslintFilesByExtension = async filesByExtension => { - let error = false; - let report = ''; - let numberFiles = 0; - const extensions = Object.keys(filesByExtension); - for (const extension of extensions.filter(extension => extension === 'ts' || extension === 'js')) { - const esLintFiles = filesByExtension[extension]; - numberFiles += esLintFiles.length; - const eslintResult = await eslintFiles(esLintFiles); - error = error || eslintResult.error; - if (!!eslintResult.report && eslintResult.report.trim().length > 0) { - report += eslintResult.report; - } - } + let error = false; + let report = ''; + let numberFiles = 0; + const extensions = Object.keys(filesByExtension); + for (const extension of extensions.filter(extension => extension === 'ts' || extension === 'js')) { + const esLintFiles = filesByExtension[extension]; + numberFiles += esLintFiles.length; + const eslintResult = await eslintFiles(esLintFiles); + error = error || eslintResult.error; + if (!!eslintResult.report && eslintResult.report.trim().length > 0) { + report += eslintResult.report; + } + } - return { error, report, numberFiles }; + return { error, report, numberFiles }; }; /** @@ -271,11 +280,11 @@ const eslintFilesByExtension = async filesByExtension => { * You can also call this function using: npm run full-code-quality */ const full = async () => { - const files = getAllFiles(); - const filesByExtension = groupFilesByExtension(files); - await fullPrettier(filesByExtension); - console.log("Running EsLint on all files. Please wait...") - await fullEslint(filesByExtension); + const files = getAllFiles(); + const filesByExtension = groupFilesByExtension(files); + await fullPrettier(filesByExtension); + console.log('Running EsLint on all files. Please wait...'); + await fullEslint(filesByExtension); }; /** @@ -283,11 +292,11 @@ const full = async () => { * You can also call this function using: npm run full-code-quality */ const fullPrettier = async filesByExtension => { - if (!filesByExtension) { - const files = getAllFiles(); - filesByExtension = groupFilesByExtension(files); - } - prettifyFiles(filesByExtension); + if (!filesByExtension) { + const files = getAllFiles(); + filesByExtension = groupFilesByExtension(files); + } + prettifyFiles(filesByExtension); }; /** @@ -295,19 +304,21 @@ const fullPrettier = async filesByExtension => { * You can also call this function using: npm run full-code-quality */ const fullEslint = async filesByExtension => { - if (!filesByExtension) { - const files = getAllFiles(); - filesByExtension = groupFilesByExtension(files); - } - - const esLintResult = await eslintFilesByExtension(filesByExtension); - const options = getOptions(); - const reportFile = options.report || 'full_eslint_report.html'; - if (esLintResult.error) { - fs.writeFileSync(reportFile, ansiup.ansi_to_html(esLintResult.report).replace(/(?:\r\n|\r|\n)/g, '
')); - console.log('Wrote eslint report to file ' + path.resolve(reportFile)); - } - console.log("Eslint executed in ' + esLintResult.numberFiles + ' files. Results written in 'full_eslint_report.html' "); + if (!filesByExtension) { + const files = getAllFiles(); + filesByExtension = groupFilesByExtension(files); + } + + const esLintResult = await eslintFilesByExtension(filesByExtension); + const options = getOptions(); + const reportFile = options.report || 'full_eslint_report.html'; + if (esLintResult.error) { + fs.writeFileSync(reportFile, ansiup.ansi_to_html(esLintResult.report).replace(/(?:\r\n|\r|\n)/g, '
')); + console.log('Wrote eslint report to file ' + path.resolve(reportFile)); + } + console.log( + "Eslint executed in ' + esLintResult.numberFiles + ' files. Results written in 'full_eslint_report.html' " + ); }; /** @@ -315,49 +326,49 @@ const fullEslint = async filesByExtension => { * @returns {{}} an json Map object */ const getOptions = () => { - const options = {}; + const options = {}; - for (let i = 0; i < process.argv.length; i++) { - const arg = process.argv[i]; - if (!arg.includes('=')) { - continue; - } + for (let i = 0; i < process.argv.length; i++) { + const arg = process.argv[i]; + if (!arg.includes('=')) { + continue; + } - const key = process.argv[i].substring(0, process.argv[i].indexOf('=')); - const value = process.argv[i].substring(process.argv[i].indexOf('=') + 1); - options[key] = value; - } - return options; + const key = process.argv[i].substring(0, process.argv[i].indexOf('=')); + const value = process.argv[i].substring(process.argv[i].indexOf('=') + 1); + options[key] = value; + } + return options; }; /** * This is the method that will be executed with node code_quality.js */ (async () => { - const options = getOptions(); - if (options.mode === 'pre_commit') { - return await preCommit(); - } - if (options.mode === 'pre_commit_prettier') { - return await preCommitPrettier(); - } - if (options.mode === 'pre_commit_eslint') { - return await preCommitEslint(); - } - if (options.mode === 'full') { - return await full(); - } - if (options.mode === 'full_prettier') { - return await fullPrettier(); - } - if (options.mode === 'full_eslint') { - return await fullEslint(); - } - - console.error( - 'You need to pass application parameter -- mode=pre_commit|pre_commit-prettier|pre_commit-eslint|full|full-prettier|full-eslint' - ); + const options = getOptions(); + if (options.mode === 'pre_commit') { + return await preCommit(); + } + if (options.mode === 'pre_commit_prettier') { + return await preCommitPrettier(); + } + if (options.mode === 'pre_commit_eslint') { + return await preCommitEslint(); + } + if (options.mode === 'full') { + return await full(); + } + if (options.mode === 'full_prettier') { + return await fullPrettier(); + } + if (options.mode === 'full_eslint') { + return await fullEslint(); + } + + console.error( + 'You need to pass application parameter -- mode=pre_commit|pre_commit-prettier|pre_commit-eslint|full|full-prettier|full-eslint' + ); })().catch(err => { - console.log(err); - process.exit(1); + console.log(err); + process.exit(1); }); diff --git a/core/src/Alerts.html b/core/src/Alerts.html index 0ba5ddbfc8..4e3f110ae5 100644 --- a/core/src/Alerts.html +++ b/core/src/Alerts.html @@ -23,32 +23,25 @@ diff --git a/core/src/App.html b/core/src/App.html index 255dbdef89..a9b496ae02 100644 --- a/core/src/App.html +++ b/core/src/App.html @@ -1,10 +1,7 @@
{#if alerts && alerts.length} - + {/if} {#if mfModal.displayed}