-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall-hooks.js
More file actions
59 lines (46 loc) · 1.71 KB
/
install-hooks.js
File metadata and controls
59 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
/**
* Husky 钩子安装脚本
* 根据操作系统选择合适的钩子文件
*/
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
// 在ES模块中获取__dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 检测操作系统
const isWindows = process.platform === 'win32';
const huskyDir = path.join(__dirname, '.husky');
try {
// 创建 _目录(如果不存在)
const huskyUnderscoreDir = path.join(huskyDir, '_');
if (!fs.existsSync(huskyUnderscoreDir)) {
fs.mkdirSync(huskyUnderscoreDir, { recursive: true });
}
// 创建 husky.sh
const huskyShPath = path.join(huskyUnderscoreDir, 'husky.sh');
if (!fs.existsSync(huskyShPath)) {
// 从原始仓库复制 husky.sh(如果需要)
execSync('npx husky init');
}
// 为Windows配置适当的钩子
if (isWindows) {
console.log('检测到 Windows 系统,配置兼容的钩子...');
// 复制 Windows 专用钩子 (批处理文件)
fs.copyFileSync(path.join(huskyDir, 'win-pre-commit'), path.join(huskyDir, 'pre-commit'));
fs.copyFileSync(path.join(huskyDir, 'win-commit-msg'), path.join(huskyDir, 'commit-msg'));
console.log('Windows 兼容钩子安装完成!');
} else {
console.log('配置标准 Unix 钩子...');
// 确保钩子有执行权限
execSync(`chmod +x ${path.join(huskyDir, 'pre-commit')}`);
execSync(`chmod +x ${path.join(huskyDir, 'commit-msg')}`);
console.log('Unix 钩子安装完成!');
}
console.log('Git 钩子设置成功!');
} catch (error) {
console.error('安装钩子时出错:', error.message);
process.exit(1);
}