diff --git a/lib/commands/lint.js b/lib/commands/lint.js index 5e31f30..b9a3e97 100644 --- a/lib/commands/lint.js +++ b/lib/commands/lint.js @@ -4,21 +4,24 @@ const { contextMiddleware } = importLazy('../cli/context-middleware'); const fs = importLazy('fs'); const process = importLazy('process'); const _ = importLazy('lodash'); +const chalk = importLazy('chalk'); + const { ESLint } = require('eslint'); function createESLintInstance(overrideConfig) { return new ESLint({ overrideConfig }); } -const lineFormat = (line, column) => `${_.padStart(line, 4)}:${_.padEnd(column, 3)}`; -const messageFormat = (message) => _.padEnd(message, 90); -const severityFormat = (severity) => _.padEnd(severity === 1 ? 'warning' : 'error', 10); +const lineFormat = (line, column) => chalk.green(`${_.padStart(line, 4)}:${_.padEnd(column, 3)}`); +const messageFormat = (message, maxLen) => _.padEnd(message, maxLen); +const severityFormat = (severity) => severity === 1 ? chalk.green(_.padEnd('warning', 10)) : chalk.red(_.padEnd('error', 10)); const messagesFormat = (list) => { - return list.map(m => `${lineFormat(m.line, m.column)}${severityFormat(m.severity)}${messageFormat(m.message)}${m.ruleId}`).join('\n'); + const maxLen = Math.max(...list.map(m => m.message.length)) + 2; + return list.map(m => `${lineFormat(m.line, m.column)}${severityFormat(m.severity)}${messageFormat(m.message, maxLen)}${m.ruleId}`).join('\n'); }; const resultFormatter = (message) => ( - `${message.filePath}\n${messagesFormat(message.messages)}\n` + `${chalk.underline(message.filePath)}\n${messagesFormat(message.messages)}\n` ); const lintCommand = async () => { @@ -42,13 +45,26 @@ const lintCommand = async () => { 0, ); + const fwarnings = results.reduce( + (acc, result) => acc + result.fixableWarningCount, + 0, + ); + const ferrors = results.reduce( + (acc, result) => acc + result.fixableErrorCount, + 0, + ); + for (const m of results.filter(i => i.warningCount > 0 || i.errorCount > 0)) { console.log(resultFormatter(m)); } const dingCount = errors + warnings; + const theme = errors ? chalk.bold.red : chalk.bold.green; if (errors || warnings) { - console.warn(`✖ ${dingCount} problem${dingCount === 1 ? '' : 's'} (${errors} errors, ${warnings} warnings)`); + console.warn(theme(`✖ ${dingCount} problem${dingCount === 1 ? '' : 's'} (${errors} errors, ${warnings} warnings)`)); + } + if (ferrors || fwarnings) { + console.warn(theme(` ${ferrors} error${ferrors === 1 ? '' : 's'} and ${fwarnings} warning${fwarnings === 1 ? '' : 's'} potentially fixable with the \`--fix\` option.`)); } if (errors) {