From a769458225905b228ef8903da593ded2c42ab49d Mon Sep 17 00:00:00 2001 From: Roy Li Date: Tue, 5 May 2020 16:17:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=9F=E6=88=90=E8=A7=84=E5=88=99?= =?UTF-8?q?=E5=89=8D=E8=BF=9B=E8=A1=8C=20eslint=20=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/command/generate.ts | 11 ++++++++ lib/utils/error-helper.ts | 2 +- lib/utils/linter.ts | 59 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 lib/utils/linter.ts diff --git a/lib/command/generate.ts b/lib/command/generate.ts index 4de17b0fd..4ea0a6a02 100644 --- a/lib/command/generate.ts +++ b/lib/command/generate.ts @@ -4,6 +4,7 @@ import path from 'path'; import { loadConfig } from '../utils/config'; import generate from '../generate'; import { errorHandler } from '../utils/error-helper'; +import { checkAndFix } from '../utils/linter'; class GenerateCommand extends Command { private options: object; @@ -24,11 +25,21 @@ class GenerateCommand extends Command { 'skip-fail': { type: 'boolean', default: false, + description: '跳过生成失败的 Artifact', + }, + 'skip-lint': { + type: 'boolean', + default: false, + description: '跳过代码检查', }, }; } public async run(ctx): Promise { + if (!ctx.argv.skipLint) { + checkAndFix(ctx.cwd); + } + const config = loadConfig(ctx.cwd, ctx.argv.config, { // istanbul ignore next ...(ctx.argv.output ? { diff --git a/lib/utils/error-helper.ts b/lib/utils/error-helper.ts index 95c670780..d871a4eb2 100644 --- a/lib/utils/error-helper.ts +++ b/lib/utils/error-helper.ts @@ -14,7 +14,7 @@ export const errorHandler = function(this: Command, err: Error): void { console.error(chalk.red(err.stack)); console.error(); doctorInfo.forEach(item => { - console.error(' ', item); + console.error(item); }); console.error(); diff --git a/lib/utils/linter.ts b/lib/utils/linter.ts new file mode 100644 index 000000000..5f4ce000b --- /dev/null +++ b/lib/utils/linter.ts @@ -0,0 +1,59 @@ +// istanbul ignore file + +import { CLIEngine } from 'eslint'; + +const linterConfig = { + baseConfig: { + extends: ['eslint:recommended'], + parserOptions: { + ecmaVersion: 2020 + }, + env: { + es6: true, + node: true, + commonjs: true, + }, + rules: { + 'array-bracket-spacing': 0, + 'comma-dangle': 0, + 'dot-notation': 0, + 'valid-jsdoc': 0, + 'no-unused-vars': 0, + 'eqeqeq': 'error', + }, + }, +}; + +export const createCli = (cliConfig?: object) => { + return new CLIEngine({ + ...linterConfig, + ...cliConfig, + }); +}; + +export const checkAndFix = (cwd: string): boolean => { + const cli = createCli({ fix: true, cwd, }); + const report = cli.executeOnFiles(['.']); + const formatter = cli.getFormatter(); + const { errorCount, fixableErrorCount } = report; + + console.log(formatter(report.results)); + + if (errorCount - fixableErrorCount > 0) { + throw new Error('ESLint 测试不通过'); + } + + return true; +}; + +export const check = (cwd: string): boolean => { + const cli = createCli({ cwd, }); + const report = cli.executeOnFiles(['.']); + const formatter = cli.getFormatter(); + const { errorCount } = report; + + console.log(formatter(report.results)); + + return errorCount === 0; +}; + diff --git a/package.json b/package.json index d08e4583d..db1d6a6fe 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "codecov": "^3.5.0", "coffee": "^5.3.0", "conventional-changelog-cli": "^2.0.31", - "eslint": "^6.6.0", "husky": "^4.2.5", "ini": "^1.3.5", "jsdom": "^16.2.2", @@ -100,6 +99,7 @@ "emoji-regex": "^8.0.0", "env2": "^2.2.2", "execa": "^4.0.0", + "eslint": "^6.6.0", "filesize": "^6.1.0", "fs-extra": "^9.0.0", "get-port": "^5.1.0",