UNPKG

git-yike-logger-hook

Version:

A TypeScript Git hook plugin for automatically generating commit logs with TODO/WIP comment scanning

4 lines (3 loc) 2.37 kB
#!/usr/bin/env node "use strict";var t=require("fs"),o=require("path");function i(t){if(t&&t.__esModule)return t;var o=Object.create(null);return t&&Object.keys(t).forEach(function(i){if("default"!==i){var e=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(o,i,e.get?e:{enumerable:!0,get:function(){return t[i]}})}}),o.default=t,Object.freeze(o)}var e=i(t),n=i(o);class s{constructor(){this.projectRoot=process.cwd(),this.gitHooksDir=n.join(this.projectRoot,".git","hooks")}async install(){try{if(!this.isGitRepository())throw new Error("当前目录不是 Git 仓库,请先运行 git init");this.ensureHooksDirectory(),this.createPreCommitHook(),this.createPostCommitHook()}catch(t){process.exit(1)}}async uninstall(){try{const t=n.join(this.gitHooksDir,"pre-commit"),o=n.join(this.gitHooksDir,"post-commit");e.existsSync(t)&&e.unlinkSync(t),e.existsSync(o)&&e.unlinkSync(o)}catch(t){process.exit(1)}}isGitRepository(){return e.existsSync(n.join(this.projectRoot,".git"))}ensureHooksDirectory(){e.existsSync(this.gitHooksDir)||e.mkdirSync(this.gitHooksDir,{recursive:!0})}createPreCommitHook(){const t=n.join(this.gitHooksDir,"pre-commit");if(e.writeFileSync(t,'#!/bin/sh\n# Git Logger Hook - Pre-commit\n# 在提交前生成日志\n\n# 获取项目根目录\nPROJECT_ROOT="$(git rev-parse --show-toplevel)"\ncd "$PROJECT_ROOT"\n\n# 检查是否存在编译后的文件\nif [ -f "dist/hook.js" ]; then\n node dist/hook.js\nelse\n echo "⚠️ 请先运行 npm run build 编译项目"\n exit 1\nfi\n'),"win32"===process.platform){const o=t+".cmd",i='@echo off\ncd /d "%PROJECT_ROOT%"\nnode dist/hook.js\n';e.writeFileSync(o,i)}else e.chmodSync(t,"755")}createPostCommitHook(){const t=n.join(this.gitHooksDir,"post-commit");e.writeFileSync(t,'#!/bin/sh\n# Git Logger Hook - Post-commit\n# 在提交后执行额外操作(可选)\n\n# 获取项目根目录\nPROJECT_ROOT="$(git rev-parse --show-toplevel)"\ncd "$PROJECT_ROOT"\n\n# 这里可以添加提交后的额外操作\n# 例如:发送通知、更新文档等\n\necho "📊 提交日志已记录到 .git-logs 目录"\n'),"win32"!==process.platform&&e.chmodSync(t,"755")}}require.main===module&&async function(){const t=new s;switch(process.argv[2]){case"install":await t.install();break;case"uninstall":await t.uninstall()}}().catch(t=>{process.exit(1)}); //# sourceMappingURL=install.js.map