Skip to content

Commit

Permalink
feat: add type check to command lint
Browse files Browse the repository at this point in the history
  • Loading branch information
linfeng committed Oct 15, 2019
1 parent 7b26151 commit 920088e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
5 changes: 1 addition & 4 deletions src/built-in-plugins/command-lint/plugin/run-lint.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as path from 'path';
import * as colors from 'colors';
import { spinner, logFatal, logSuccess, logInfo } from '../../../utils/log';
import { pri, srcPath } from '../../../node';
import { lint } from '../../../utils/lint';

export const CommandLint = async () => {
await lint({
lintAll: true,
showBreakError: true,
needFix: true,
typeCheck: true,
});
};
2 changes: 1 addition & 1 deletion src/built-in-plugins/command-test/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import { pri } from '../../../node';
import { testsPath } from '../../../utils/structor-config';
import { typeChecker } from './type-checker';
import { typeChecker } from '../../../utils/type-checker';

pri.project.whiteFileRules.add(file => {
return path.format(file).startsWith(path.join(pri.projectRootPath, testsPath.dir));
Expand Down
8 changes: 8 additions & 0 deletions src/utils/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import * as fs from 'fs-extra';
import { spinner } from './log';
import { globalState } from './global-state';
import { srcPath, packagesPath } from './structor-config';
import { typeChecker } from './type-checker';

export const eslintParam = "'./?(src|packages|docs|tests)/**/*.?(ts|tsx)'";

interface Options {
lintAll?: boolean;
needFix?: boolean;
showBreakError?: boolean;
typeCheck?: boolean;
}

class DefaultOptions {
Expand All @@ -23,6 +25,8 @@ class DefaultOptions {
needFix = true;

showBreakError = true;

typeCheck = false;
}

export async function lint(options?: Partial<DefaultOptions>) {
Expand Down Expand Up @@ -141,4 +145,8 @@ export async function lint(options?: Partial<DefaultOptions>) {
execSync(`git add ${fixedFiles.map(file => file.filePath).join(' ')}`);
process.exit(1);
}

if (mergedOptions.typeCheck) {
typeChecker();
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
import { execSync } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import * as process from 'process';
import { logFatal, logInfo, logSuccess } from '../../../utils/log';

const shScript = `
#!/bin/bash
# author: 奇阳
# 类型校验
# 检查代码中是否包含 @DEBUG 标识符
if [ "$(git diff --cached --numstat --diff-filter=ACM | wc -l)" -gt 0 ]
then
FILES=$(grep -in '@DEBUG' --include *.ts --include *.tsx --include *.js --include *.scss --include *.css $(git diff --cached --name-only --diff-filter=ACM) /dev/null)
if [ -n "$FILES" ]
then
echo '\033[33m待提交代码中存在 @DEBUG 标识符,提交终止'
echo $FILES
exit 1
fi
fi
# 对整个项目进行完整的类型检查
TS_CHANGED=$(git diff --cached --numstat --diff-filter=ACM | grep -F '.ts' | wc -l)
if [ "$TS_CHANGED" -gt 0 ]
then
echo '正在检查 TypeScript 类型,请稍候'
tsc -p . || exit 1
fi
`;
import { pri } from '../node';
import { logFatal, logInfo, logSuccess } from './log';

export function typeChecker() {
const stashFileCnt = +execSync('git diff --cached --numstat --diff-filter=ACM | wc -l').toString('utf8');
Expand Down Expand Up @@ -60,7 +32,7 @@ export function typeChecker() {
if (tsChangedFilesCnt) {
logInfo('正在检查 Typescript 类型,请稍后');
try {
execSync(`tsc -p . || exit 1`, {
execSync(`${pri.projectRootPath}/node_modules/.bin/tsc -p . || exit 1`, {
stdio: [0, 1, 2],
});
} catch (e) {
Expand Down

0 comments on commit 920088e

Please sign in to comment.