From ab877a23cd322fea41506ddb4180b269186deb99 Mon Sep 17 00:00:00 2001 From: JK Date: Sat, 19 May 2018 06:05:55 +0800 Subject: [PATCH] feat(eslint): add --max-warnings and --max-errors for cli-plugin-eslint (#1289) close #1268 --- packages/@vue/cli-plugin-eslint/index.js | 4 +++- packages/@vue/cli-plugin-eslint/lint.js | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/@vue/cli-plugin-eslint/index.js b/packages/@vue/cli-plugin-eslint/index.js index 6eaf496877..9428601aff 100644 --- a/packages/@vue/cli-plugin-eslint/index.js +++ b/packages/@vue/cli-plugin-eslint/index.js @@ -24,7 +24,9 @@ module.exports = (api, { lintOnSave }) => { usage: 'vue-cli-service lint [options] [...files]', options: { '--format [formatter]': 'specify formatter (default: codeframe)', - '--no-fix': 'do not fix errors' + '--no-fix': 'do not fix errors', + '--max-errors [limit]': 'specify number of errors to make build failed (default: 0)', + '--max-warnings [limit]': 'specify number of warnings to make build failed (default: Infinity)' }, details: 'For more options, see https://eslint.org/docs/user-guide/command-line-interface#options' }, args => { diff --git a/packages/@vue/cli-plugin-eslint/lint.js b/packages/@vue/cli-plugin-eslint/lint.js index 51183de74a..e8e5f8e5d6 100644 --- a/packages/@vue/cli-plugin-eslint/lint.js +++ b/packages/@vue/cli-plugin-eslint/lint.js @@ -7,10 +7,11 @@ module.exports = function lint (args = {}, api) { const { log, done } = require('@vue/cli-shared-utils') const files = args._ && args._.length ? args._ : ['src', 'tests', '*.js'] + const argsConfig = normalizeConfig(args) const config = Object.assign({}, options, { fix: true, cwd - }, normalizeConfig(args)) + }, argsConfig) const engine = new CLIEngine(config) const report = engine.executeOnFiles(files) const formatter = engine.getFormatter(args.format || 'codeframe') @@ -18,8 +19,13 @@ module.exports = function lint (args = {}, api) { if (config.fix) { CLIEngine.outputFixes(report) } + + const maxErrors = argsConfig.maxErrors || 0 + const maxWarnings = typeof argsConfig.maxWarnings === 'number' ? argsConfig.maxWarnings : Infinity + const isErrorsExceeded = report.errorCount > maxErrors + const isWarningsExceeded = report.warningCount > maxWarnings - if (!report.errorCount) { + if (!isErrorsExceeded && !isWarningsExceeded) { if (!args.silent) { const hasFixed = report.results.some(f => f.output) if (hasFixed) { @@ -32,7 +38,7 @@ module.exports = function lint (args = {}, api) { }) log() } - if (report.warningCount) { + if (report.warningCount || report.errorCount) { console.log(formatter(report.results)) } else { done(hasFixed ? `All lint errors auto-fixed.` : `No lint errors found!`) @@ -40,6 +46,12 @@ module.exports = function lint (args = {}, api) { } } else { console.log(formatter(report.results)) + if (isErrorsExceed && typeof argsConfig.maxErrors === 'number') { + log(`Eslint found too many errors (maximum: ${argsConfig.maxErrors}).`) + } + if (isWarningsExceed) { + log(`Eslint found too many warnings (maximum: ${argsConfig.maxWarnings}).`) + } process.exit(1) } }