Skip to content

Commit 971f4a2

Browse files
committed
feat: add type-checker method
1 parent 1af1937 commit 971f4a2

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

src/built-in-plugins/command-test/plugin/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
22
import { pri } from '../../../node';
33
import { testsPath } from '../../../utils/structor-config';
4+
import { typeChecker } from './type-checker';
45

56
pri.project.whiteFileRules.add(file => {
67
return path.format(file).startsWith(path.join(pri.projectRootPath, testsPath.dir));
@@ -16,11 +17,12 @@ pri.commands.registerCommand({
1617
name: ['test'],
1718
description: 'Run tests.',
1819
action: async () => {
19-
await pri.project.lint({
20-
lintAll: false,
21-
needFix: true,
22-
showBreakError: true,
23-
});
20+
// await pri.project.lint({
21+
// lintAll: false,
22+
// needFix: true,
23+
// showBreakError: true,
24+
// });
25+
typeChecker();
2426
await pri.project.ensureProjectFiles();
2527
await pri.project.checkProjectFiles();
2628

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { execSync } from 'child_process';
2+
import * as path from 'path';
3+
import * as fs from 'fs';
4+
import * as process from 'process';
5+
import { logFatal, logInfo, logSuccess } from '../../../utils/log';
6+
7+
const shScript = `
8+
#!/bin/bash
9+
10+
# author: 奇阳
11+
# 类型校验
12+
13+
# 检查代码中是否包含 @DEBUG 标识符
14+
if [ "$(git diff --cached --numstat --diff-filter=ACM | wc -l)" -gt 0 ]
15+
then
16+
FILES=$(grep -in '@DEBUG' --include *.ts --include *.tsx --include *.js --include *.scss --include *.css $(git diff --cached --name-only --diff-filter=ACM) /dev/null)
17+
if [ -n "$FILES" ]
18+
then
19+
echo '\033[33m待提交代码中存在 @DEBUG 标识符,提交终止'
20+
echo $FILES
21+
exit 1
22+
fi
23+
fi
24+
25+
# 对整个项目进行完整的类型检查
26+
TS_CHANGED=$(git diff --cached --numstat --diff-filter=ACM | grep -F '.ts' | wc -l)
27+
if [ "$TS_CHANGED" -gt 0 ]
28+
then
29+
echo '正在检查 TypeScript 类型,请稍候'
30+
tsc -p . --noEmit || exit 1
31+
fi
32+
`;
33+
34+
export function typeChecker() {
35+
const stashFileCnt = +execSync('git diff --cached --numstat --diff-filter=ACM | wc -l').toString('utf8');
36+
37+
if (stashFileCnt > 0) {
38+
let debugFiles = '';
39+
40+
try {
41+
debugFiles = execSync(
42+
`grep -in '@DEBUG' --include *.ts --include *.tsx --include *.js --include *.scss --include *.css $(git diff --cached --name-only --diff-filter=ACM) /dev/null`,
43+
{
44+
shell: 'bash',
45+
stdio: [0, 1],
46+
},
47+
).toString('utf8');
48+
// eslint-disable-next-line no-empty
49+
} catch (e) {}
50+
51+
if (debugFiles) {
52+
logFatal('待提交代码中存在 @DEBUG 标识符,提交终止');
53+
process.exit(1);
54+
}
55+
}
56+
57+
const tsChangedFilesCnt = +execSync(`git diff --cached --numstat --diff-filter=ACM | grep -F '.ts' | wc -l`).toString(
58+
'utf8',
59+
);
60+
if (tsChangedFilesCnt) {
61+
logInfo('正在检查 Typescript 类型,请稍后');
62+
try {
63+
execSync(`tsc -p . --noEmit || exit 1`, {
64+
stdio: [0, 1, 2],
65+
});
66+
} catch (e) {
67+
logFatal('类型检查出错!');
68+
process.exit(1);
69+
}
70+
logSuccess('所有代码类型检查通过!');
71+
}
72+
}

0 commit comments

Comments
 (0)