|
| 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