|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { execSync } = require('child_process'); |
| 6 | + |
| 7 | +const UI_DIR = path.resolve(__dirname, '..'); // UI directory |
| 8 | +const ROOT_DIR = path.resolve(UI_DIR, '..'); // Project root directory |
| 9 | +const GIT_DIR = getGitDir(ROOT_DIR); // Git root directory |
| 10 | +const HUSKY_DIR = path.join(GIT_DIR, '.husky'); |
| 11 | + |
| 12 | +// 查找 Git 目录 |
| 13 | +function getGitDir(startDir) { |
| 14 | + let currentDir = startDir; |
| 15 | + |
| 16 | + while (currentDir !== path.parse(currentDir).root) { |
| 17 | + const gitDir = path.join(currentDir, '.git'); |
| 18 | + if (fs.existsSync(gitDir)) { |
| 19 | + return currentDir; |
| 20 | + } |
| 21 | + currentDir = path.dirname(currentDir); |
| 22 | + } |
| 23 | + |
| 24 | + throw new Error('Could not find Git directory'); |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +if (!fs.existsSync(HUSKY_DIR)) { |
| 29 | + console.log(`Creating husky directory: ${HUSKY_DIR}`); |
| 30 | + fs.mkdirSync(HUSKY_DIR, { recursive: true }); |
| 31 | +} |
| 32 | + |
| 33 | +if (!fs.existsSync(path.join(HUSKY_DIR, '_'))) { |
| 34 | + console.log(`Creating husky _ directory: ${path.join(HUSKY_DIR, '_')}`); |
| 35 | + fs.mkdirSync(path.join(HUSKY_DIR, '_'), { recursive: true }); |
| 36 | +} |
| 37 | + |
| 38 | +// init husky |
| 39 | +try { |
| 40 | + console.log('Initializing husky...'); |
| 41 | + execSync('npx husky install', { cwd: GIT_DIR, stdio: 'inherit' }); |
| 42 | +} catch (error) { |
| 43 | + console.error(`❌ Failed to initialize husky: ${error.message}`); |
| 44 | + process.exit(1); |
| 45 | +} |
| 46 | + |
| 47 | +// create lint-staged config file |
| 48 | +const lintStagedConfig = { |
| 49 | + "src/**/*.{ts,tsx}": [ |
| 50 | + "eslint --fix", |
| 51 | + "prettier --write" |
| 52 | + ], |
| 53 | + "src/**/*.{scss,md}": [ |
| 54 | + "prettier --write" |
| 55 | + ] |
| 56 | +}; |
| 57 | + |
| 58 | +console.log(`Creating lint-staged config: ${path.join(UI_DIR, '.lintstagedrc.json')}`); |
| 59 | +fs.writeFileSync( |
| 60 | + path.join(UI_DIR, '.lintstagedrc.json'), |
| 61 | + JSON.stringify(lintStagedConfig, null, 2) |
| 62 | +); |
| 63 | + |
| 64 | +// create pre-commit hook |
| 65 | +const preCommitContent = `#!/bin/sh |
| 66 | +. "$(dirname "$0")/_/husky.sh" |
| 67 | +
|
| 68 | +echo "🔍 Start running the code check..." |
| 69 | +
|
| 70 | +# Getting the Git Root Directory |
| 71 | +GIT_ROOT=$(git rev-parse --show-toplevel) |
| 72 | +
|
| 73 | +# Get a list of staging files |
| 74 | +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR) |
| 75 | +
|
| 76 | +# Check for files in the ui/ directory |
| 77 | +UI_FILES=$(echo "$STAGED_FILES" | grep '^ui/' || echo "") |
| 78 | +
|
| 79 | +if [ -n "$UI_FILES" ]; then |
| 80 | + echo "🔎 Discover ui file changes, run code checks..." |
| 81 | +
|
| 82 | + # Switch to the ui directory |
| 83 | + cd "$GIT_ROOT/ui" || { |
| 84 | + echo "❌ Unable to access the UI catalog" |
| 85 | + exit 1 |
| 86 | + } |
| 87 | +
|
| 88 | + # 运行 lint-staged |
| 89 | + echo "✨ Running ESLint and Prettier Formatting..." |
| 90 | + npx lint-staged --verbose |
| 91 | +
|
| 92 | + LINT_STAGED_RESULT=$? |
| 93 | + if [ $LINT_STAGED_RESULT -ne 0 ]; then |
| 94 | + echo "❌ Code check failed, please fix the above problem" |
| 95 | + exit $LINT_STAGED_RESULT |
| 96 | + fi |
| 97 | +
|
| 98 | + echo "✅ Code check passes!" |
| 99 | +else |
| 100 | + echo "ℹ️ No front-end file changes found, skip code checking" |
| 101 | +fi |
| 102 | +
|
| 103 | +echo "🎉 Pre-submission check completed" |
| 104 | +`; |
| 105 | + |
| 106 | +console.log(`Creating pre-commit hook: ${path.join(HUSKY_DIR, 'pre-commit')}`); |
| 107 | +fs.writeFileSync(path.join(HUSKY_DIR, 'pre-commit'), preCommitContent); |
| 108 | +execSync(`chmod +x ${path.join(HUSKY_DIR, 'pre-commit')}`); |
| 109 | + |
| 110 | +// create husky.sh |
| 111 | +const huskyShContent = `#!/bin/sh |
| 112 | +if [ -z "$husky_skip_init" ]; then |
| 113 | + debug () { |
| 114 | + if [ "$HUSKY_DEBUG" = "1" ]; then |
| 115 | + echo "husky (debug) - $1" |
| 116 | + fi |
| 117 | + } |
| 118 | +
|
| 119 | + readonly hook_name="$(basename "$0")" |
| 120 | + debug "starting $hook_name..." |
| 121 | +
|
| 122 | + if [ "$HUSKY" = "0" ]; then |
| 123 | + debug "HUSKY=0, skip hook" |
| 124 | + exit 0 |
| 125 | + fi |
| 126 | +
|
| 127 | + if [ -f ~/.huskyrc ]; then |
| 128 | + debug "sourcing ~/.huskyrc" |
| 129 | + . ~/.huskyrc |
| 130 | + fi |
| 131 | +
|
| 132 | + export readonly husky_skip_init=1 |
| 133 | + sh -e "$0" "$@" |
| 134 | + exitCode="$?" |
| 135 | +
|
| 136 | + if [ $exitCode != 0 ]; then |
| 137 | + echo "husky - $hook_name hook exited with code $exitCode (error)" |
| 138 | + fi |
| 139 | +
|
| 140 | + exit $exitCode |
| 141 | +fi |
| 142 | +`; |
| 143 | + |
| 144 | +console.log(`Creating husky.sh: ${path.join(HUSKY_DIR, '_', 'husky.sh')}`); |
| 145 | +fs.writeFileSync( |
| 146 | + path.join(HUSKY_DIR, '_', 'husky.sh'), |
| 147 | + huskyShContent |
| 148 | +); |
| 149 | +execSync(`chmod +x ${path.join(HUSKY_DIR, '_', 'husky.sh')}`); |
| 150 | + |
| 151 | +console.log('Lint setup complete! Husky and lint-staged have been configured.'); |
| 152 | +console.log(`Git root directory: ${GIT_DIR}`); |
| 153 | +console.log(`Husky directory: ${HUSKY_DIR}`); |
0 commit comments