|
1 | 1 | #!/usr/bin/env node |
2 | | -const { readStdin } = require('./adapter'); |
| 2 | +const { readStdin, hookEnabled } = require('./adapter'); |
| 3 | + |
| 4 | +function splitShellSegments(command) { |
| 5 | + const segments = []; |
| 6 | + let current = ''; |
| 7 | + let quote = null; |
| 8 | + |
| 9 | + for (let i = 0; i < command.length; i++) { |
| 10 | + const ch = command[i]; |
| 11 | + if (quote) { |
| 12 | + if (ch === quote) quote = null; |
| 13 | + current += ch; |
| 14 | + continue; |
| 15 | + } |
| 16 | + |
| 17 | + if (ch === '"' || ch === "'") { |
| 18 | + quote = ch; |
| 19 | + current += ch; |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + const next = command[i + 1] || ''; |
| 24 | + if (ch === ';' || (ch === '&' && next === '&') || (ch === '|' && next === '|') || (ch === '&' && next !== '&')) { |
| 25 | + if (current.trim()) segments.push(current.trim()); |
| 26 | + current = ''; |
| 27 | + if ((ch === '&' && next === '&') || (ch === '|' && next === '|')) i++; |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + current += ch; |
| 32 | + } |
| 33 | + |
| 34 | + if (current.trim()) segments.push(current.trim()); |
| 35 | + return segments; |
| 36 | +} |
| 37 | + |
3 | 38 | readStdin().then(raw => { |
4 | 39 | try { |
5 | | - const input = JSON.parse(raw); |
6 | | - const cmd = input.command || ''; |
7 | | - |
8 | | - // 1. Block dev server outside tmux |
9 | | - if (process.platform !== 'win32' && /(npm run dev\b|pnpm( run)? dev\b|yarn dev\b|bun run dev\b)/.test(cmd)) { |
10 | | - console.error('[ECC] BLOCKED: Dev server must run in tmux for log access'); |
11 | | - console.error('[ECC] Use: tmux new-session -d -s dev "npm run dev"'); |
12 | | - process.exit(2); |
| 40 | + const input = JSON.parse(raw || '{}'); |
| 41 | + const cmd = String(input.command || input.args?.command || ''); |
| 42 | + |
| 43 | + if (hookEnabled('pre:bash:dev-server-block', ['standard', 'strict']) && process.platform !== 'win32') { |
| 44 | + const segments = splitShellSegments(cmd); |
| 45 | + const tmuxLauncher = /^\s*tmux\s+(new|new-session|new-window|split-window)\b/; |
| 46 | + const devPattern = /\b(npm\s+run\s+dev|pnpm(?:\s+run)?\s+dev|yarn\s+dev|bun\s+run\s+dev)\b/; |
| 47 | + const hasBlockedDev = segments.some(segment => devPattern.test(segment) && !tmuxLauncher.test(segment)); |
| 48 | + if (hasBlockedDev) { |
| 49 | + console.error('[ECC] BLOCKED: Dev server must run in tmux for log access'); |
| 50 | + console.error('[ECC] Use: tmux new-session -d -s dev "npm run dev"'); |
| 51 | + process.exit(2); |
| 52 | + } |
13 | 53 | } |
14 | 54 |
|
15 | | - // 2. Tmux reminder for long-running commands |
16 | | - if (process.platform !== 'win32' && !process.env.TMUX && |
17 | | - /(npm (install|test)|pnpm (install|test)|yarn (install|test)?|bun (install|test)|cargo build|make\b|docker\b|pytest|vitest|playwright)/.test(cmd)) { |
| 55 | + if ( |
| 56 | + hookEnabled('pre:bash:tmux-reminder', ['strict']) && |
| 57 | + process.platform !== 'win32' && |
| 58 | + !process.env.TMUX && |
| 59 | + /(npm (install|test)|pnpm (install|test)|yarn (install|test)?|bun (install|test)|cargo build|make\b|docker\b|pytest|vitest|playwright)/.test(cmd) |
| 60 | + ) { |
18 | 61 | console.error('[ECC] Consider running in tmux for session persistence'); |
19 | 62 | } |
20 | 63 |
|
21 | | - // 3. Git push review reminder |
22 | | - if (/git push/.test(cmd)) { |
| 64 | + if (hookEnabled('pre:bash:git-push-reminder', ['strict']) && /\bgit\s+push\b/.test(cmd)) { |
23 | 65 | console.error('[ECC] Review changes before push: git diff origin/main...HEAD'); |
24 | 66 | } |
25 | | - } catch {} |
| 67 | + } catch { |
| 68 | + // noop |
| 69 | + } |
| 70 | + |
26 | 71 | process.stdout.write(raw); |
27 | 72 | }).catch(() => process.exit(0)); |
0 commit comments