From 976aad9ee70c9592024387a8d7d909f3953e114a Mon Sep 17 00:00:00 2001 From: sunlei Date: Fri, 5 Jun 2026 08:52:54 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96Husky=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .husky/pre-commit | 2 +- internal/commit/husky-fast-check.mjs | 86 ++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 internal/commit/husky-fast-check.mjs diff --git a/.husky/pre-commit b/.husky/pre-commit index 8ff2704..28156f3 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -pnpm run verify:commit +node internal/commit/husky-fast-check.mjs diff --git a/internal/commit/husky-fast-check.mjs b/internal/commit/husky-fast-check.mjs new file mode 100644 index 0000000..206a837 --- /dev/null +++ b/internal/commit/husky-fast-check.mjs @@ -0,0 +1,86 @@ +import { spawnSync } from 'node:child_process'; +import { existsSync } from 'node:fs'; +import { extname } from 'node:path'; + +const CHECK_EXTENSIONS = new Set([ + '.cjs', + '.js', + '.jsx', + '.mjs', + '.ts', + '.tsx', + '.vue', +]); +const CHECK_ROOTS = ['apps/web-antdv-next/', 'internal/']; + +function run(command, args) { + const result = spawnSync(command, args, { + shell: process.platform === 'win32' && command === 'pnpm', + stdio: 'inherit', + }); + + if (result.error) { + console.error(result.error.message); + process.exit(1); + } + + if (result.status !== 0) { + process.exit(result.status ?? 1); + } +} + +function output(command, args) { + const result = spawnSync(command, args, { + encoding: 'utf8', + shell: false, + }); + + if (result.error) { + console.error(result.error.message); + process.exit(1); + } + + if (result.status !== 0) { + process.exit(result.status ?? 1); + } + + return result.stdout; +} + +function getPnpmCommand() { + return 'pnpm'; +} + +function getStagedFiles() { + return output('git', [ + 'diff', + '--cached', + '--name-only', + '--diff-filter=ACMR', + '-z', + ]) + .split('\0') + .map((file) => file.replaceAll('\\', '/')) + .filter(Boolean); +} + +const files = getStagedFiles().filter((file) => { + if (!existsSync(file)) return false; + if (!CHECK_ROOTS.some((root) => file.startsWith(root))) return false; + + return CHECK_EXTENSIONS.has(extname(file)); +}); + +if (files.length === 0) { + console.info('[husky] no staged files need eslint.'); + process.exit(0); +} + +run(getPnpmCommand(), [ + 'exec', + 'eslint', + '--cache', + '--cache-location', + 'node_modules/.cache/husky-eslint/', + ...files, +]); diff --git a/package.json b/package.json index 235db7f..fc6fb3c 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "preinstall": "npx only-allow pnpm", "preview": "pnpm -F @vben/web-antdv-next run preview", "reinstall": "pnpm run clean && rimraf pnpm-lock.yaml && pnpm install", + "verify:staged": "node internal/commit/husky-fast-check.mjs", "verify:commit": "pnpm run lint:commit && pnpm -F @vben/web-antdv-next run typecheck", "test:e2e": "turbo run test:e2e" },