包文件→ .shtukbase/packages/std-conventional-commits/actions/hook.ts
actions/hook.ts
38 行, 2.0 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // `install-hook` / `uninstall-hook`: puts our commit-msg hook into the repository's hooks folder or takes it out.
- // Runs only on the owner's command; it never overwrites a hook that is not ours.
- import { chmodSync, existsSync, readFileSync, unlinkSync, writeFileSync, mkdirSync } from 'node:fs';
- import { isAbsolute, join } from 'node:path';
- import { git, ROOT } from '../checks/lib/std.ts';
- const MARK = '# shtukbase: std-conventional-commits commit-msg hook';
- const mode = process.argv[2];
- const dir = git(['rev-parse', '--git-path', 'hooks'])?.trim();
- if (!dir) {
- console.log('not a git repository: nowhere to put the hook');
- process.exit(1);
- }
- const hooks = isAbsolute(dir) ? dir : join(ROOT, dir);
- const target = join(hooks, 'commit-msg');
- const ours = existsSync(target) && readFileSync(target, 'utf8').includes(MARK);
- if (mode === 'install') {
- if (existsSync(target) && !ours) {
- console.log(`${target}: there is already a foreign commit-msg hook; not touching it. Remove it yourself or call ours from it:\n bun "${join(ROOT, '.shtukbase/packages/std-conventional-commits/hooks/commit-msg.ts')}" "$1" || exit 1`);
- process.exit(1);
- }
- const script = join(ROOT, '.shtukbase/packages/std-conventional-commits/hooks/commit-msg.ts');
- mkdirSync(hooks, { recursive: true });
- writeFileSync(target, `#!/bin/sh\n${MARK}\n# Removed by: shtuk run std-conventional-commits uninstall-hook\ncommand -v bun >/dev/null 2>&1 || { echo "commit-msg: bun not found, message not checked" >&2; exit 0; }\ncase "$1" in /*) f="$1";; *) f="$PWD/$1";; esac\ncd "${ROOT}" && exec bun "${script}" "$f"\n`);
- chmodSync(target, 0o755);
- console.log(`${ours ? 'updated' : 'installed'} hook ${target}`);
- } else if (mode === 'uninstall') {
- if (!ours) {
- console.log(existsSync(target) ? `${target}: the hook is not ours, not touching it` : 'there is no hook of ours');
- process.exit(0);
- }
- unlinkSync(target);
- console.log(`hook removed ${target}`);
- } else {
- console.log('usage: hook.ts install | uninstall');
- process.exit(2);
- }