ShtukbaseStandard
包文件→ .shtukbase/packages/std-conventional-commits/hooks/commit-msg.ts

hooks/commit-msg.ts

25 行, 1.1 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // The commit-msg git hook: checks the message being committed against CC-R01..CC-R03. A norm in `enforce` mode
  2. // stops the commit; the others only print. Installed only by `shtuk run std-conventional-commits install-hook`.
  3. import { readFileSync } from 'node:fs';
  4. import { clean, config, ignored, lint, PKG } from '../checks/cclib.ts';
  5. import { norm } from '../checks/lib/std.ts';
  6. const file = process.argv[2];
  7. if (!file) {
  8. console.error('commit-msg: no message file');
  9. process.exit(0);
  10. }
  11. const message = clean(readFileSync(file, 'utf8'));
  12. const cfg = config();
  13. let stop = false;
  14. if (!ignored(message, cfg)) {
  15. for (const f of lint(message, cfg)) {
  16. const n = norm(PKG, f.rule, f.rule === 'CC-R03' ? 'observe' : 'enforce');
  17. if (!n.active) continue;
  18. const hard = n.mode === 'enforce';
  19. stop ||= hard;
  20. console.error(`${hard ? 'commit-msg' : 'commit-msg (warning)'}: ${f.rule}: ${f.text}`);
  21. }
  22. }
  23. if (stop) console.error('The message does not follow Conventional Commits 1.0.0: "type(scope)!: description". Fix it and commit again.');
  24. process.exit(stop ? 1 : 0);