46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { ROOT } = require('./deploy-lib.cjs');
|
|
|
|
function main() {
|
|
const hooksDir = path.join(ROOT, '.githooks');
|
|
const gitHooksDir = path.join(ROOT, '.git', 'hooks');
|
|
|
|
if (!fs.existsSync(hooksDir)) {
|
|
fs.mkdirSync(hooksDir, { recursive: true });
|
|
}
|
|
|
|
// 创建 pre-commit hook 脚本
|
|
const preCommitPath = path.join(hooksDir, 'pre-commit');
|
|
const preCommitContent = [
|
|
'#!/bin/sh',
|
|
'node script/git-safety-check.cjs',
|
|
'',
|
|
].join('\n');
|
|
fs.writeFileSync(preCommitPath, preCommitContent, { mode: 0o755 });
|
|
|
|
// 配置 git 使用 .githooks 目录
|
|
const { execSync } = require('node:child_process');
|
|
try {
|
|
execSync('git config core.hooksPath .githooks', { cwd: ROOT, encoding: 'utf8' });
|
|
console.log('Git hooks 已配置: core.hooksPath = .githooks');
|
|
} catch (error) {
|
|
console.warn('配置 git core.hooksPath 失败,请手动执行: git config core.hooksPath .githooks');
|
|
}
|
|
|
|
// 确保 .gitignore 中有 versioning 相关规则
|
|
const gitignorePath = path.join(ROOT, '.gitignore');
|
|
if (fs.existsSync(gitignorePath)) {
|
|
const content = fs.readFileSync(gitignorePath, 'utf8');
|
|
if (!content.includes('versioning/')) {
|
|
console.warn('建议在 .gitignore 中添加 versioning/ 规则以保护本地版本记录');
|
|
}
|
|
}
|
|
|
|
console.log('Git hooks 设置完成。');
|
|
}
|
|
|
|
main();
|