ShtukbaseStandard
包文件→ .shtukbase/packages/hooks/run.ts

run.ts

62 行, 2.8 KB.

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

  1. // Runs the hooks of one project for a Git or Claude Code event. The dispatcher selects the project.
  2. import { spawnSync } from 'node:child_process';
  3. import { existsSync } from 'node:fs';
  4. import { resolve } from 'node:path';
  5. import {
  6. BLOCK, command, fileOwners, inside, installedPackages, list, normHead, projectRoot, readPayload,
  7. } from './lib/hooklib.ts';
  8. const [requested = '', ...eventArgs] = process.argv.slice(2);
  9. const event = requested.replace(/^agent:/, '');
  10. const git = event === 'pre-commit' || event === 'commit-msg';
  11. if (event === 'commit-msg' && eventArgs[0] && process.env.SHTUK_ROOT)
  12. eventArgs[0] = resolve(process.env.SHTUK_ROOT, eventArgs[0]);
  13. const { raw, p } = git ? { raw: '', p: null } : readPayload();
  14. if (!event || (!git && !p)) process.exit(0);
  15. const files = process.env.SHTUK_FILES?.split('\n').filter(Boolean);
  16. const owners = files ? fileOwners() : {};
  17. const dispatched = git || requested.startsWith('agent:') || process.env.SHTUK_FILES !== undefined;
  18. const root = dispatched ? process.cwd() : projectRoot(p);
  19. const tool = typeof p?.tool_name === 'string' ? p.tool_name : '';
  20. const blocks: string[] = [];
  21. const said: string[] = [];
  22. const broken: string[] = [];
  23. for (const pkg of installedPackages(root))
  24. for (const h of list(pkg.head.hooks)) {
  25. if (!h || h.event !== event) continue;
  26. const tools = list(h.tools).map(String);
  27. if (tools.length && !tools.includes(tool)) continue;
  28. const env = { ...process.env, CLAUDE_PROJECT_DIR: root };
  29. if (files?.length) {
  30. const { except } = normHead(root, h.norm);
  31. if (Array.isArray(except) && except.length) {
  32. const kept = files.filter((f) => !Object.hasOwn(owners, f) || !except.includes(owners[f]));
  33. if (!kept.length) continue;
  34. env.SHTUK_FILES = kept.join('\n');
  35. }
  36. }
  37. const name = `${pkg.id}/${h.name ?? h.run}`;
  38. const file = inside(pkg.dir, h.run);
  39. if (!file || !existsSync(file)) {
  40. broken.push(`${name}: no file ${h.run}`);
  41. continue;
  42. }
  43. const [exe, args] = command(file);
  44. const r = spawnSync(exe, [...args, ...eventArgs], {
  45. cwd: root, input: raw, encoding: 'utf8', timeout: (Number(h.timeout) || 60) * 1000,
  46. env,
  47. });
  48. if (r.status === BLOCK) blocks.push(`[${name}] ${(r.stderr || r.stdout || 'blocks').trim()}`);
  49. else if (r.status === 0) {
  50. if (r.stdout?.trim()) said.push(r.stdout.trim());
  51. } else broken.push(`${name}: ${r.error ? r.error.message : `code ${r.status}`} ${(r.stderr ?? '').trim().split('\n')[0] ?? ''}`.trim());
  52. }
  53. if (said.length) process.stdout.write(said.join('\n\n') + '\n');
  54. if (broken.length) process.stderr.write(broken.map((b) => `hook is broken, skipped: ${b}`).join('\n') + '\n');
  55. if (blocks.length) {
  56. process.stderr.write(blocks.join('\n\n') + '\n');
  57. process.exit(BLOCK);
  58. }
  59. process.exit(0);