ShtukbaseStandard
файл пакета→ .shtukbase/packages/std-conventional-commits/actions/hook.ts

actions/hook.ts

38 строк, 2.0 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // `install-hook` / `uninstall-hook`: puts our commit-msg hook into the repository's hooks folder or takes it out.
  2. // Runs only on the owner's command; it never overwrites a hook that is not ours.
  3. import { chmodSync, existsSync, readFileSync, unlinkSync, writeFileSync, mkdirSync } from 'node:fs';
  4. import { isAbsolute, join } from 'node:path';
  5. import { git, ROOT } from '../checks/lib/std.ts';
  6. const MARK = '# shtukbase: std-conventional-commits commit-msg hook';
  7. const mode = process.argv[2];
  8. const dir = git(['rev-parse', '--git-path', 'hooks'])?.trim();
  9. if (!dir) {
  10. console.log('not a git repository: nowhere to put the hook');
  11. process.exit(1);
  12. }
  13. const hooks = isAbsolute(dir) ? dir : join(ROOT, dir);
  14. const target = join(hooks, 'commit-msg');
  15. const ours = existsSync(target) && readFileSync(target, 'utf8').includes(MARK);
  16. if (mode === 'install') {
  17. if (existsSync(target) && !ours) {
  18. 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`);
  19. process.exit(1);
  20. }
  21. const script = join(ROOT, '.shtukbase/packages/std-conventional-commits/hooks/commit-msg.ts');
  22. mkdirSync(hooks, { recursive: true });
  23. 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`);
  24. chmodSync(target, 0o755);
  25. console.log(`${ours ? 'updated' : 'installed'} hook ${target}`);
  26. } else if (mode === 'uninstall') {
  27. if (!ours) {
  28. console.log(existsSync(target) ? `${target}: the hook is not ours, not touching it` : 'there is no hook of ours');
  29. process.exit(0);
  30. }
  31. unlinkSync(target);
  32. console.log(`hook removed ${target}`);
  33. } else {
  34. console.log('usage: hook.ts install | uninstall');
  35. process.exit(2);
  36. }