Skip to content

Commit

Permalink
feat: 生成规则前进行 eslint 检查
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 5, 2020
1 parent 1cd8436 commit a769458
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/command/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<void> {
if (!ctx.argv.skipLint) {
checkAndFix(ctx.cwd);
}

const config = loadConfig(ctx.cwd, ctx.argv.config, {
// istanbul ignore next
...(ctx.argv.output ? {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/error-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
59 changes: 59 additions & 0 deletions lib/utils/linter.ts
Original file line number Diff line number Diff line change
@@ -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;
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit a769458

Please sign in to comment.