@webpieces/ai-hook-rules 0.3.159 → 0.3.160

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@
6
6
 
7
7
  const path = require('path');
8
8
  const fs = require('fs');
9
- const compiled = path.join(__dirname, '..', 'src', 'bin', 'postinstall.js');
9
+ const compiled = path.join(__dirname, '..', 'src', 'bin', 'setup.js');
10
10
 
11
11
  if (fs.existsSync(compiled)) {
12
12
  require(compiled).main();
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+ // Plain JS shim — delegates to compiled TypeScript.
3
+ // Must NOT be converted to TypeScript (needs to exist pre-build for pnpm bin symlinks).
4
+ // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
5
+ 'use strict';
6
+
7
+ const path = require('path');
8
+ const fs = require('fs');
9
+ const compiled = path.join(__dirname, '..', 'src', 'bin', 'global-setup.js');
10
+
11
+ if (fs.existsSync(compiled)) {
12
+ require(compiled).main();
13
+ } else {
14
+ console.error(' [ai-hook-rules] Package not built yet. Run the build first, or install from npm.');
15
+ process.exit(1);
16
+ }
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@webpieces/ai-hook-rules",
3
- "version": "0.3.159",
3
+ "version": "0.3.160",
4
4
  "description": "Pluggable write-time validation framework for AI coding agents (@webpieces/ai-hook-rules). Claude Code PreToolUse + openclaw before_tool_call adapters share one rule engine.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
7
- "scripts": {
8
- "postinstall": "node bin/postinstall.js"
9
- },
10
7
  "bin": {
8
+ "wp-ai-hook": "./src/adapters/claude-code-hook.js",
11
9
  "wp-setup-ai-hooks": "./bin/wp-setup-ai-hooks.js",
10
+ "wp-setup-global-ai-hooks": "./bin/wp-setup-global-ai-hooks.js",
12
11
  "wp-git-update": "./src/scripts/git-updateFromMain.js",
13
12
  "wp-git-gather": "./src/scripts/git-gatherInfo.js",
14
13
  "wp-git-merge-complete": "./src/scripts/git-mergeComplete.js"
@@ -35,7 +34,7 @@
35
34
  "directory": "packages/tooling/ai-hook-rules"
36
35
  },
37
36
  "dependencies": {
38
- "@webpieces/rules-config": "0.3.159"
37
+ "@webpieces/rules-config": "0.3.160"
39
38
  },
40
39
  "publishConfig": {
41
40
  "access": "public"
@@ -26,8 +26,7 @@ function safeParse(raw) {
26
26
  }
27
27
  catch (err) {
28
28
  const error = (0, to_error_1.toError)(err);
29
- void error;
30
- return null;
29
+ throw new types_1.InformAiError(`Malformed hook input from Claude Code stdin: ${error.message}`);
31
30
  }
32
31
  }
33
32
  function normalizeToolKind(toolName) {
@@ -102,8 +101,13 @@ async function main() {
102
101
  }
103
102
  catch (err) {
104
103
  const error = (0, to_error_1.toError)(err);
105
- process.stderr.write(`[ai-hooks] claude-code adapter crashed (failing open): ${error.message}\n`);
106
- process.exit(0);
104
+ if (err instanceof types_1.InformAiError) {
105
+ process.stderr.write(error.message + '\n');
106
+ }
107
+ else {
108
+ process.stderr.write(`[ai-hooks] hook crashed unexpectedly — failing closed: ${error.message}\n`);
109
+ }
110
+ process.exit(2);
107
111
  }
108
112
  }
109
113
  if (require.main === module) {
@@ -1 +1 @@
1
- {"version":3,"file":"claude-code-hook.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/adapters/claude-code-hook.ts"],"names":[],"mappings":";;AA4EA,oBAoCC;AAhHD,2CAA8C;AAC9C,yDAAqD;AACrD,yCAA8E;AAC9E,+CAA2C;AAE3C,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;AAqBnE,SAAS,SAAS;IACd,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QAClC,OAAO,CAAC,KAAK,CAAC,E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C,OAAO,GAAY,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;QAC3B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,0DAA0D,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;QAClG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;AACL,CAAC;AAED,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC1B,IAAI,EAAE,CAAC;AACX,CAAC","sourcesContent":["import { run, runBash } from '../core/runner';\nimport { logRejection } from '../core/rejection-log';\nimport { NormalizedToolInput, NormalizedEdit, ToolKind } from '../core/types';\nimport { toError } from '../core/to-error';\n\nconst HANDLED_FILE_TOOLS = new Set(['Write', 'Edit', 'MultiEdit']);\n\ninterface ClaudeCodePayload {\n tool_name: string;\n tool_input: ClaudeCodeToolInput;\n}\n\ninterface ClaudeCodeToolInput {\n file_path?: string;\n content?: string;\n old_string?: string;\n new_string?: string;\n edits?: ClaudeCodeEditEntry[];\n command?: string;\n}\n\ninterface ClaudeCodeEditEntry {\n old_string?: string;\n new_string?: string;\n}\n\nfunction readStdin(): Promise<string> {\n return new Promise((resolve) => {\n let data = '';\n process.stdin.setEncoding('utf8');\n process.stdin.on('data', (chunk: string) => { data += chunk; });\n process.stdin.on('end', () => resolve(data));\n process.stdin.on('error', () => resolve(''));\n if (process.stdin.isTTY) resolve('');\n });\n}\n\nfunction safeParse(raw: string): ClaudeCodePayload | null {\n if (!raw || raw.trim() === '') return null;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return JSON.parse(raw) as ClaudeCodePayload;\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n}\n\nfunction normalizeToolKind(toolName: string): ToolKind | null {\n if (HANDLED_FILE_TOOLS.has(toolName)) return toolName as ToolKind;\n return null;\n}\n\nfunction normalizeToolInput(toolKind: ToolKind, toolInput: ClaudeCodeToolInput): NormalizedToolInput | null {\n const filePath = toolInput.file_path;\n if (!filePath) return null;\n\n if (toolKind === 'Write') {\n return new NormalizedToolInput(filePath, [\n new NormalizedEdit('', toolInput.content || ''),\n ]);\n }\n if (toolKind === 'Edit') {\n return new NormalizedToolInput(filePath, [\n new NormalizedEdit(toolInput.old_string || '', toolInput.new_string || ''),\n ]);\n }\n if (toolKind === 'MultiEdit') {\n const raw = Array.isArray(toolInput.edits) ? toolInput.edits : [];\n const edits = raw.map((e) => new NormalizedEdit(e.old_string || '', e.new_string || ''));\n return new NormalizedToolInput(filePath, edits);\n }\n return null;\n}\n\nexport async function main(): Promise<void> {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const raw = await readStdin();\n const payload = safeParse(raw);\n if (!payload) { process.exit(0); return; }\n\n const cwd = process.cwd();\n\n if (payload.tool_name === 'Bash') {\n const command = payload.tool_input.command;\n if (!command || command.trim() === '') { process.exit(0); return; }\n const result = runBash(command, cwd);\n if (!result) { process.exit(0); return; }\n process.stderr.write(result.report);\n process.exit(2);\n return;\n }\n\n const toolKind = normalizeToolKind(payload.tool_name);\n if (!toolKind) { process.exit(0); return; }\n\n const input = normalizeToolInput(toolKind, payload.tool_input);\n if (!input) { process.exit(0); return; }\n\n const result = run(toolKind, input, cwd);\n if (!result) { process.exit(0); return; }\n\n logRejection(toolKind, input, result, cwd);\n process.stderr.write(result.report);\n process.exit(2);\n } catch (err: unknown) {\n const error = toError(err);\n process.stderr.write(`[ai-hooks] claude-code adapter crashed (failing open): ${error.message}\\n`);\n process.exit(0);\n }\n}\n\nif (require.main === module) {\n main();\n}\n"]}
1
+ {"version":3,"file":"claude-code-hook.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/adapters/claude-code-hook.ts"],"names":[],"mappings":";;AA2EA,oBAwCC;AAnHD,2CAA8C;AAC9C,yDAAqD;AACrD,yCAA6F;AAC7F,+CAA2C;AAE3C,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAA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C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAAC,OAAO;QAAC,CAAC;QAEzC,IAAA,4BAAY,EAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;QAC3C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;QAC3B,IAAI,GAAG,YAAY,qBAAa,EAAE,CAAC;YAC/B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;QAC/C,CAAC;aAAM,CAAC;YACJ,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,0DAA0D,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;QACtG,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;AACL,CAAC;AAED,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC1B,IAAI,EAAE,CAAC;AACX,CAAC","sourcesContent":["import { run, runBash } from '../core/runner';\nimport { logRejection } from '../core/rejection-log';\nimport { NormalizedToolInput, NormalizedEdit, ToolKind, InformAiError } from '../core/types';\nimport { toError } from '../core/to-error';\n\nconst HANDLED_FILE_TOOLS = new Set(['Write', 'Edit', 'MultiEdit']);\n\ninterface ClaudeCodePayload {\n tool_name: string;\n tool_input: ClaudeCodeToolInput;\n}\n\ninterface ClaudeCodeToolInput {\n file_path?: string;\n content?: string;\n old_string?: string;\n new_string?: string;\n edits?: ClaudeCodeEditEntry[];\n command?: string;\n}\n\ninterface ClaudeCodeEditEntry {\n old_string?: string;\n new_string?: string;\n}\n\nfunction readStdin(): Promise<string> {\n return new Promise((resolve) => {\n let data = '';\n process.stdin.setEncoding('utf8');\n process.stdin.on('data', (chunk: string) => { data += chunk; });\n process.stdin.on('end', () => resolve(data));\n process.stdin.on('error', () => resolve(''));\n if (process.stdin.isTTY) resolve('');\n });\n}\n\nfunction safeParse(raw: string): ClaudeCodePayload | null {\n if (!raw || raw.trim() === '') return null;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return JSON.parse(raw) as ClaudeCodePayload;\n } catch (err: unknown) {\n const error = toError(err);\n throw new InformAiError(`Malformed hook input from Claude Code stdin: ${error.message}`);\n }\n}\n\nfunction normalizeToolKind(toolName: string): ToolKind | null {\n if (HANDLED_FILE_TOOLS.has(toolName)) return toolName as ToolKind;\n return null;\n}\n\nfunction normalizeToolInput(toolKind: ToolKind, toolInput: ClaudeCodeToolInput): NormalizedToolInput | null {\n const filePath = toolInput.file_path;\n if (!filePath) return null;\n\n if (toolKind === 'Write') {\n return new NormalizedToolInput(filePath, [\n new NormalizedEdit('', toolInput.content || ''),\n ]);\n }\n if (toolKind === 'Edit') {\n return new NormalizedToolInput(filePath, [\n new NormalizedEdit(toolInput.old_string || '', toolInput.new_string || ''),\n ]);\n }\n if (toolKind === 'MultiEdit') {\n const raw = Array.isArray(toolInput.edits) ? toolInput.edits : [];\n const edits = raw.map((e) => new NormalizedEdit(e.old_string || '', e.new_string || ''));\n return new NormalizedToolInput(filePath, edits);\n }\n return null;\n}\n\nexport async function main(): Promise<void> {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const raw = await readStdin();\n const payload = safeParse(raw);\n if (!payload) { process.exit(0); return; }\n\n const cwd = process.cwd();\n\n if (payload.tool_name === 'Bash') {\n const command = payload.tool_input.command;\n if (!command || command.trim() === '') { process.exit(0); return; }\n const result = runBash(command, cwd);\n if (!result) { process.exit(0); return; }\n process.stderr.write(result.report);\n process.exit(2);\n return;\n }\n\n const toolKind = normalizeToolKind(payload.tool_name);\n if (!toolKind) { process.exit(0); return; }\n\n const input = normalizeToolInput(toolKind, payload.tool_input);\n if (!input) { process.exit(0); return; }\n\n const result = run(toolKind, input, cwd);\n if (!result) { process.exit(0); return; }\n\n logRejection(toolKind, input, result, cwd);\n process.stderr.write(result.report);\n process.exit(2);\n } catch (err: unknown) {\n const error = toError(err);\n if (err instanceof InformAiError) {\n process.stderr.write(error.message + '\\n');\n } else {\n process.stderr.write(`[ai-hooks] hook crashed unexpectedly — failing closed: ${error.message}\\n`);\n }\n process.exit(2);\n }\n}\n\nif (require.main === module) {\n main();\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs_1 = require("fs");
4
+ const path_1 = require("path");
5
+ const child_process_1 = require("child_process");
6
+ function readSkipHooks(cwd) {
7
+ // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
8
+ try {
9
+ const skipPath = (0, path_1.join)(cwd, '.webpieces', 'skiphooks');
10
+ if (!(0, fs_1.existsSync)(skipPath))
11
+ return null;
12
+ return JSON.parse((0, fs_1.readFileSync)(skipPath, 'utf8'));
13
+ }
14
+ catch (err) {
15
+ // eslint-disable-next-line @webpieces/catch-error-pattern -- intentionally discard; malformed .skiphooks must not crash global hook
16
+ return null;
17
+ }
18
+ }
19
+ function run(rawInput) {
20
+ const cwd = process.cwd();
21
+ let payload = null;
22
+ // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
23
+ try {
24
+ if (rawInput.trim()) {
25
+ payload = JSON.parse(rawInput);
26
+ }
27
+ }
28
+ catch (err) {
29
+ //const error = toError(err); -- malformed stdin is intentionally ignored; must not crash global hook
30
+ }
31
+ // 1. Check .skiphooks — if valid and not expired, allow everything
32
+ const skipHooks = readSkipHooks(cwd);
33
+ if (skipHooks !== null) {
34
+ const nowSeconds = Date.now() / 1000;
35
+ if (skipHooks.expires === null || nowSeconds < skipHooks.expires) {
36
+ process.exit(0);
37
+ }
38
+ }
39
+ // 2. Always allow writing .webpieces/skiphooks (this is the escape hatch)
40
+ const filePath = payload?.tool_input?.file_path ?? '';
41
+ if (payload?.tool_name === 'Write' && filePath.endsWith('.webpieces/skiphooks')) {
42
+ process.exit(0);
43
+ }
44
+ // 3. Delegate to per-repo hook if present
45
+ const localHook = (0, path_1.join)(cwd, 'node_modules', '.bin', 'wp-ai-hook');
46
+ if ((0, fs_1.existsSync)(localHook)) {
47
+ const result = (0, child_process_1.spawnSync)(localHook, [], {
48
+ input: rawInput,
49
+ encoding: 'buffer',
50
+ });
51
+ if (result.stdout?.length)
52
+ process.stdout.write(result.stdout);
53
+ if (result.stderr?.length)
54
+ process.stderr.write(result.stderr);
55
+ process.exit(result.status ?? 0);
56
+ }
57
+ // 4. No local hook found — block and instruct AI
58
+ process.stdout.write('This project is missing the webpieces AI hook (./node_modules/.bin/wp-ai-hook not found).\n' +
59
+ 'Ask the human one of:\n' +
60
+ ' A) Install webpieces: run `npm install @webpieces/ai-hook-rules` then retry\n' +
61
+ ' B) Skip hooks: ask how long (1 hour / 1 day / forever), then write .webpieces/skiphooks:\n' +
62
+ ' {"expires": <unix epoch seconds>, "reason": "..."}\n' +
63
+ ' or for forever: {"expires": null, "reason": "..."}\n' +
64
+ ' Note: writing .webpieces/skiphooks is ALWAYS allowed even while blocked.\n');
65
+ process.exit(2);
66
+ }
67
+ let stdinData = '';
68
+ process.stdin.resume();
69
+ process.stdin.setEncoding('utf8');
70
+ process.stdin.on('data', (chunk) => {
71
+ stdinData += chunk;
72
+ });
73
+ process.stdin.on('end', () => {
74
+ run(stdinData);
75
+ });
76
+ //# sourceMappingURL=global-hook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global-hook.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/adapters/global-hook.ts"],"names":[],"mappings":";;AAAA,2BAA8C;AAC9C,+BAA4B;AAC5B,iDAA0C;AAc1C,SAAS,aAAa,CAAC,GAAW;IAC9B,8DAA8D;IAC9D,IAAI,CAAC;QACD,MAAM,QAAQ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,CAAC,SAAS,CAAC,CAAC;AACnB,CAAC,CAAC,CAAC","sourcesContent":["import { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { spawnSync } from 'child_process';\n\ninterface SkipHooksFile {\n expires: number | null;\n reason?: string;\n}\n\ninterface HookPayload {\n tool_name?: string;\n tool_input?: {\n file_path?: string;\n };\n}\n\nfunction readSkipHooks(cwd: string): SkipHooksFile | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const skipPath = join(cwd, '.webpieces', 'skiphooks');\n if (!existsSync(skipPath)) return null;\n return JSON.parse(readFileSync(skipPath, 'utf8')) as SkipHooksFile;\n } catch (err: unknown) {\n // eslint-disable-next-line @webpieces/catch-error-pattern -- intentionally discard; malformed .skiphooks must not crash global hook\n return null;\n }\n}\n\nfunction run(rawInput: string): void {\n const cwd = process.cwd();\n\n let payload: HookPayload | null = null;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n if (rawInput.trim()) {\n payload = JSON.parse(rawInput) as HookPayload;\n }\n } catch (err: unknown) {\n //const error = toError(err); -- malformed stdin is intentionally ignored; must not crash global hook\n }\n\n // 1. Check .skiphooks — if valid and not expired, allow everything\n const skipHooks = readSkipHooks(cwd);\n if (skipHooks !== null) {\n const nowSeconds = Date.now() / 1000;\n if (skipHooks.expires === null || nowSeconds < skipHooks.expires) {\n process.exit(0);\n }\n }\n\n // 2. Always allow writing .webpieces/skiphooks (this is the escape hatch)\n const filePath = payload?.tool_input?.file_path ?? '';\n if (payload?.tool_name === 'Write' && filePath.endsWith('.webpieces/skiphooks')) {\n process.exit(0);\n }\n\n // 3. Delegate to per-repo hook if present\n const localHook = join(cwd, 'node_modules', '.bin', 'wp-ai-hook');\n if (existsSync(localHook)) {\n const result = spawnSync(localHook, [], {\n input: rawInput,\n encoding: 'buffer',\n });\n if (result.stdout?.length) process.stdout.write(result.stdout);\n if (result.stderr?.length) process.stderr.write(result.stderr);\n process.exit(result.status ?? 0);\n }\n\n // 4. No local hook found — block and instruct AI\n process.stdout.write(\n 'This project is missing the webpieces AI hook (./node_modules/.bin/wp-ai-hook not found).\\n' +\n 'Ask the human one of:\\n' +\n ' A) Install webpieces: run `npm install @webpieces/ai-hook-rules` then retry\\n' +\n ' B) Skip hooks: ask how long (1 hour / 1 day / forever), then write .webpieces/skiphooks:\\n' +\n ' {\"expires\": <unix epoch seconds>, \"reason\": \"...\"}\\n' +\n ' or for forever: {\"expires\": null, \"reason\": \"...\"}\\n' +\n ' Note: writing .webpieces/skiphooks is ALWAYS allowed even while blocked.\\n',\n );\n process.exit(2);\n}\n\nlet stdinData = '';\nprocess.stdin.resume();\nprocess.stdin.setEncoding('utf8');\nprocess.stdin.on('data', (chunk: string) => {\n stdinData += chunk;\n});\nprocess.stdin.on('end', () => {\n run(stdinData);\n});\n"]}
@@ -67,8 +67,10 @@ async function handler(event, _context) {
67
67
  }
68
68
  catch (err) {
69
69
  const error = (0, to_error_1.toError)(err);
70
- console.error(`[ai-hooks] openclaw adapter crashed (failing open): ${error.message}`);
71
- return undefined;
70
+ const msg = err instanceof types_1.InformAiError
71
+ ? error.message
72
+ : `[ai-hooks] openclaw adapter crashed — failing closed: ${error.message}`;
73
+ return new OpenclawHandlerResult('rejected', msg);
72
74
  }
73
75
  }
74
76
  //# sourceMappingURL=openclaw-plugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"openclaw-plugin.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/adapters/openclaw-plugin.ts"],"names":[],"mappings":";;AAiEA,0BAuBC;;AAxFD,mDAA6B;AAC7B,+CAAyB;AAEzB,0DAA0D;AAC1D,2CAAqC;AACrC,yCAA8E;AAC9E,+CAA2C;AAa3C,MAAM,qBAAqB;IAIvB,YAAY,MAA+B,EAAE,MAAe;QACxD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CACJ;AAED,MAAM,QAAQ,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DAAuD,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACtF,OAAO,SAAS,CAAC;IACrB,CAAC;AACL,CAAC","sourcesContent":["import * as path from 'path';\nimport * as fs from 'fs';\n\nimport { CONFIG_FILENAME } from '@webpieces/rules-config';\nimport { run } from '../core/runner';\nimport { NormalizedToolInput, NormalizedEdit, ToolKind } from '../core/types';\nimport { toError } from '../core/to-error';\n\ninterface ToolCallEvent {\n toolName: string;\n // webpieces-disable no-any-unknown -- openclaw SDK passes opaque tool arguments\n arguments: Record<string, unknown>;\n}\n\ninterface HookContext {\n // webpieces-disable no-any-unknown -- openclaw SDK context shape is opaque\n [key: string]: unknown;\n}\n\nclass OpenclawHandlerResult {\n readonly status: 'approved' | 'rejected';\n readonly reason: string | undefined;\n\n constructor(status: 'approved' | 'rejected', reason?: string) {\n this.status = status;\n this.reason = reason;\n }\n}\n\nconst TOOL_MAP: Record<string, ToolKind> = {\n 'write': 'Write',\n 'edit': 'Edit',\n};\n\nfunction mapToolName(openclawName: string): ToolKind | null {\n return TOOL_MAP[openclawName] || null;\n}\n\n// webpieces-disable no-any-unknown -- openclaw SDK passes opaque tool arguments\nfunction mapToolInput(toolName: string, args: Record<string, unknown>): NormalizedToolInput | null {\n const filePath = typeof args['path'] === 'string' ? args['path'] as string : null;\n if (!filePath) return null;\n\n if (toolName === 'write') {\n const content = typeof args['content'] === 'string' ? args['content'] as string : '';\n return new NormalizedToolInput(filePath, [new NormalizedEdit('', content)]);\n }\n if (toolName === 'edit') {\n const oldStr = typeof args['old_string'] === 'string' ? args['old_string'] as string : '';\n const newStr = typeof args['new_string'] === 'string' ? args['new_string'] as string : '';\n return new NormalizedToolInput(filePath, [new NormalizedEdit(oldStr, newStr)]);\n }\n return null;\n}\n\nfunction findWorkspaceRoot(filePath: string): string | null {\n let dir = path.dirname(filePath);\n while (true) {\n if (fs.existsSync(path.join(dir, CONFIG_FILENAME))) return dir;\n const parent = path.dirname(dir);\n if (parent === dir) return null;\n dir = parent;\n }\n}\n\nexport default async function handler(\n event: ToolCallEvent,\n _context: HookContext,\n): Promise<OpenclawHandlerResult | undefined> {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const toolKind = mapToolName(event.toolName);\n if (!toolKind) return undefined;\n\n const input = mapToolInput(event.toolName, event.arguments);\n if (!input) return undefined;\n\n const wsRoot = findWorkspaceRoot(input.filePath);\n if (!wsRoot) return undefined;\n\n const result = run(toolKind, input, wsRoot);\n if (!result) return new OpenclawHandlerResult('approved');\n return new OpenclawHandlerResult('rejected', result.report);\n } catch (err: unknown) {\n const error = toError(err);\n console.error(`[ai-hooks] openclaw adapter crashed (failing open): ${error.message}`);\n return undefined;\n }\n}\n"]}
1
+ {"version":3,"file":"openclaw-plugin.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/adapters/openclaw-plugin.ts"],"names":[],"mappings":";;AAiEA,0BAyBC;;AA1FD,mDAA6B;AAC7B,+CAAyB;AAEzB,0DAA0D;AAC1D,2CAAqC;AACrC,yCAA6F;AAC7F,+CAA2C;AAa3C,MAAM,qBAAqB;IAIvB,YAAY,MAA+B,EAAE,MAAe;QACxD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CACJ;AAED,MAAM,QAAQ,GAA6B;IACvC,OAAO,EAAE,OAAO;IAChB,MAAM,EAAE,MAAM;CACjB,CAAC;AAEF,SAAS,WAAW,CAAC,YAAoB;IACrC,OAAO,QAAQ,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AAC1C,CAAC;AAED,gFAAgF;AAChF,SAAS,YAAY,CAAC,QAAgB,EAAE,IAA6B;IACjE,MAAM,QAAQ,GAAG,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAW,CAAC,CAAC,CAAC,IAAI,CAAC;IAClF,IAAI,CAAC,QAAQ;QAAE,O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c,KAAK,UAAU,OAAO,CACjC,KAAoB,EACpB,QAAqB;IAErB,8DAA8D;IAC9D,IAAI,CAAC;QACD,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC7C,IAAI,CAAC,QAAQ;YAAE,OAAO,SAAS,CAAC;QAEhC,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;QAC5D,IAAI,CAAC,KAAK;YAAE,OAAO,SAAS,CAAC;QAE7B,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM;YAAE,OAAO,SAAS,CAAC;QAE9B,MAAM,MAAM,GAAG,IAAA,YAAG,EAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QAC5C,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,qBAAqB,CAAC,UAAU,CAAC,CAAC;QAC1D,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAChE,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;QAC3B,MAAM,GAAG,GAAG,GAAG,YAAY,qBAAa;YACpC,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,yDAAyD,KAAK,CAAC,OAAO,EAAE,CAAC;QAC/E,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;IACtD,CAAC;AACL,CAAC","sourcesContent":["import * as path from 'path';\nimport * as fs from 'fs';\n\nimport { CONFIG_FILENAME } from '@webpieces/rules-config';\nimport { run } from '../core/runner';\nimport { NormalizedToolInput, NormalizedEdit, ToolKind, InformAiError } from '../core/types';\nimport { toError } from '../core/to-error';\n\ninterface ToolCallEvent {\n toolName: string;\n // webpieces-disable no-any-unknown -- openclaw SDK passes opaque tool arguments\n arguments: Record<string, unknown>;\n}\n\ninterface HookContext {\n // webpieces-disable no-any-unknown -- openclaw SDK context shape is opaque\n [key: string]: unknown;\n}\n\nclass OpenclawHandlerResult {\n readonly status: 'approved' | 'rejected';\n readonly reason: string | undefined;\n\n constructor(status: 'approved' | 'rejected', reason?: string) {\n this.status = status;\n this.reason = reason;\n }\n}\n\nconst TOOL_MAP: Record<string, ToolKind> = {\n 'write': 'Write',\n 'edit': 'Edit',\n};\n\nfunction mapToolName(openclawName: string): ToolKind | null {\n return TOOL_MAP[openclawName] || null;\n}\n\n// webpieces-disable no-any-unknown -- openclaw SDK passes opaque tool arguments\nfunction mapToolInput(toolName: string, args: Record<string, unknown>): NormalizedToolInput | null {\n const filePath = typeof args['path'] === 'string' ? args['path'] as string : null;\n if (!filePath) return null;\n\n if (toolName === 'write') {\n const content = typeof args['content'] === 'string' ? args['content'] as string : '';\n return new NormalizedToolInput(filePath, [new NormalizedEdit('', content)]);\n }\n if (toolName === 'edit') {\n const oldStr = typeof args['old_string'] === 'string' ? args['old_string'] as string : '';\n const newStr = typeof args['new_string'] === 'string' ? args['new_string'] as string : '';\n return new NormalizedToolInput(filePath, [new NormalizedEdit(oldStr, newStr)]);\n }\n return null;\n}\n\nfunction findWorkspaceRoot(filePath: string): string | null {\n let dir = path.dirname(filePath);\n while (true) {\n if (fs.existsSync(path.join(dir, CONFIG_FILENAME))) return dir;\n const parent = path.dirname(dir);\n if (parent === dir) return null;\n dir = parent;\n }\n}\n\nexport default async function handler(\n event: ToolCallEvent,\n _context: HookContext,\n): Promise<OpenclawHandlerResult | undefined> {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const toolKind = mapToolName(event.toolName);\n if (!toolKind) return undefined;\n\n const input = mapToolInput(event.toolName, event.arguments);\n if (!input) return undefined;\n\n const wsRoot = findWorkspaceRoot(input.filePath);\n if (!wsRoot) return undefined;\n\n const result = run(toolKind, input, wsRoot);\n if (!result) return new OpenclawHandlerResult('approved');\n return new OpenclawHandlerResult('rejected', result.report);\n } catch (err: unknown) {\n const error = toError(err);\n const msg = err instanceof InformAiError\n ? error.message\n : `[ai-hooks] openclaw adapter crashed failing closed: ${error.message}`;\n return new OpenclawHandlerResult('rejected', msg);\n }\n}\n"]}
@@ -1,2 +1 @@
1
- #!/usr/bin/env node
2
1
  export declare function main(): void;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.main = main;
4
+ const fs_1 = require("fs");
5
+ const os_1 = require("os");
6
+ const path_1 = require("path");
7
+ function main() {
8
+ const homeDir = (0, os_1.homedir)();
9
+ const webpiecesDir = (0, path_1.join)(homeDir, '.webpieces');
10
+ const globalHookDest = (0, path_1.join)(webpiecesDir, 'global-hook.js');
11
+ const claudeSettingsPath = (0, path_1.join)(homeDir, '.claude', 'settings.json');
12
+ // Find compiled global-hook.js relative to this compiled file (src/bin/ → src/adapters/)
13
+ const shimSource = (0, path_1.join)(__dirname, '..', 'adapters', 'global-hook.js');
14
+ if (!(0, fs_1.existsSync)(shimSource)) {
15
+ console.error(`[wp-setup-global-ai-hooks] Cannot find compiled hook at: ${shimSource}`);
16
+ console.error(' Make sure the package is built first.');
17
+ process.exit(1);
18
+ }
19
+ // Create ~/.webpieces/ if needed
20
+ if (!(0, fs_1.existsSync)(webpiecesDir)) {
21
+ (0, fs_1.mkdirSync)(webpiecesDir, { recursive: true });
22
+ }
23
+ (0, fs_1.copyFileSync)(shimSource, globalHookDest);
24
+ console.log(` Installed global hook → ${globalHookDest}`);
25
+ // Wire into ~/.claude/settings.json using absolute path (~ is not expanded by Claude Code)
26
+ const hookCommand = `node ${globalHookDest}`;
27
+ let settings = {};
28
+ if ((0, fs_1.existsSync)(claudeSettingsPath)) {
29
+ settings = JSON.parse((0, fs_1.readFileSync)(claudeSettingsPath, 'utf8'));
30
+ }
31
+ if (!settings.hooks)
32
+ settings.hooks = {};
33
+ if (!Array.isArray(settings.hooks.PreToolUse))
34
+ settings.hooks.PreToolUse = [];
35
+ const alreadyWired = settings.hooks.PreToolUse.some((e) => e.hooks.some((h) => h.command.includes('global-hook.js')));
36
+ if (alreadyWired) {
37
+ console.log(' ~/.claude/settings.json already has the global hook — skipping.');
38
+ }
39
+ else {
40
+ settings.hooks.PreToolUse.push({
41
+ matcher: 'Write|Edit|MultiEdit|Bash',
42
+ hooks: [{ type: 'command', command: hookCommand }],
43
+ });
44
+ (0, fs_1.mkdirSync)((0, path_1.dirname)(claudeSettingsPath), { recursive: true });
45
+ (0, fs_1.writeFileSync)(claudeSettingsPath, JSON.stringify(settings, null, 4) + '\n');
46
+ console.log(` Wired global hook into ~/.claude/settings.json`);
47
+ }
48
+ console.log('');
49
+ console.log('✅ Global webpieces hook installed.');
50
+ console.log(' IMPORTANT: Remove any per-project hook entries from .claude/settings.json files.');
51
+ console.log(' The global hook delegates to each repo\'s ./node_modules/.bin/wp-ai-hook automatically.');
52
+ console.log('');
53
+ console.log(' If a project lacks webpieces, the hook will ask AI to warn you and offer:');
54
+ console.log(' A) Install webpieces in that project');
55
+ console.log(' B) Write a .skiphooks file to bypass temporarily or forever');
56
+ }
57
+ if (require.main === module) {
58
+ main();
59
+ }
60
+ //# sourceMappingURL=global-setup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global-setup.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/bin/global-setup.ts"],"names":[],"mappings":";;AAiBA,oBA0DC;AA3ED,2BAAsF;AACtF,2BAA6B;AAC7B,+BAAqC;AAerC,SAAgB,IAAI;IAChB,MAAM,OAAO,GAAG,IAAA,YAAO,GAAE,CAAC;IAC1B,MAAM,YAAY,GAAG,IAAA,WAAI,EAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IACjD,MAAM,cAAc,GAAG,IAAA,WAAI,EAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;IAC5D,MAAM,kBAAkB,GAAG,IAAA,WAAI,EA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IAAI,EAAE,CAAC;AACX,CAAC","sourcesContent":["import { existsSync, readFileSync, writeFileSync, copyFileSync, mkdirSync } from 'fs';\nimport { homedir } from 'os';\nimport { join, dirname } from 'path';\n\ninterface HookEntry {\n matcher: string;\n hooks: Array<{ type: string; command: string }>;\n}\n\ninterface ClaudeSettings {\n hooks?: {\n PreToolUse?: HookEntry[];\n };\n // webpieces-disable no-any-unknown -- opaque settings bag; arbitrary keys allowed\n [key: string]: unknown;\n}\n\nexport function main(): void {\n const homeDir = homedir();\n const webpiecesDir = join(homeDir, '.webpieces');\n const globalHookDest = join(webpiecesDir, 'global-hook.js');\n const claudeSettingsPath = join(homeDir, '.claude', 'settings.json');\n\n // Find compiled global-hook.js relative to this compiled file (src/bin/ → src/adapters/)\n const shimSource = join(__dirname, '..', 'adapters', 'global-hook.js');\n\n if (!existsSync(shimSource)) {\n console.error(`[wp-setup-global-ai-hooks] Cannot find compiled hook at: ${shimSource}`);\n console.error(' Make sure the package is built first.');\n process.exit(1);\n }\n\n // Create ~/.webpieces/ if needed\n if (!existsSync(webpiecesDir)) {\n mkdirSync(webpiecesDir, { recursive: true });\n }\n\n copyFileSync(shimSource, globalHookDest);\n console.log(` Installed global hook → ${globalHookDest}`);\n\n // Wire into ~/.claude/settings.json using absolute path (~ is not expanded by Claude Code)\n const hookCommand = `node ${globalHookDest}`;\n\n let settings: ClaudeSettings = {};\n if (existsSync(claudeSettingsPath)) {\n settings = JSON.parse(readFileSync(claudeSettingsPath, 'utf8')) as ClaudeSettings;\n }\n\n if (!settings.hooks) settings.hooks = {};\n if (!Array.isArray(settings.hooks.PreToolUse)) settings.hooks.PreToolUse = [];\n\n const alreadyWired = settings.hooks.PreToolUse.some((e) =>\n e.hooks.some((h) => h.command.includes('global-hook.js')),\n );\n\n if (alreadyWired) {\n console.log(' ~/.claude/settings.json already has the global hook — skipping.');\n } else {\n settings.hooks.PreToolUse.push({\n matcher: 'Write|Edit|MultiEdit|Bash',\n hooks: [{ type: 'command', command: hookCommand }],\n });\n mkdirSync(dirname(claudeSettingsPath), { recursive: true });\n writeFileSync(claudeSettingsPath, JSON.stringify(settings, null, 4) + '\\n');\n console.log(` Wired global hook into ~/.claude/settings.json`);\n }\n\n console.log('');\n console.log('✅ Global webpieces hook installed.');\n console.log(' IMPORTANT: Remove any per-project hook entries from .claude/settings.json files.');\n console.log(' The global hook delegates to each repo\\'s ./node_modules/.bin/wp-ai-hook automatically.');\n console.log('');\n console.log(' If a project lacks webpieces, the hook will ask AI to warn you and offer:');\n console.log(' A) Install webpieces in that project');\n console.log(' B) Write a .skiphooks file to bypass temporarily or forever');\n}\n\nif (require.main === module) {\n main();\n}\n"]}
@@ -0,0 +1 @@
1
+ export declare function main(): void;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.main = main;
4
+ const tslib_1 = require("tslib");
5
+ const fs = tslib_1.__importStar(require("fs"));
6
+ const path = tslib_1.__importStar(require("path"));
7
+ const index_1 = require("../core/rules/index");
8
+ const HOOK_COMMAND = './node_modules/.bin/wp-ai-hook';
9
+ const CONFIG_FILENAME = 'webpieces.config.json';
10
+ function settingsAlreadyHasHook(settingsPath) {
11
+ if (!fs.existsSync(settingsPath))
12
+ return false;
13
+ const content = fs.readFileSync(settingsPath, 'utf8');
14
+ return content.includes('wp-ai-hook');
15
+ }
16
+ function wireSettings(projectRoot) {
17
+ const claudeDir = path.join(projectRoot, '.claude');
18
+ if (!fs.existsSync(claudeDir)) {
19
+ console.log(' [ai-hook-rules] No .claude/ directory found — add the hook manually:');
20
+ console.log(' In .claude/settings.json under hooks.PreToolUse, add:');
21
+ console.log(` { "matcher": "Write|Edit|MultiEdit|Bash", "hooks": [{ "type": "command", "command": "${HOOK_COMMAND}" }] }`);
22
+ return;
23
+ }
24
+ const settingsPath = path.join(claudeDir, 'settings.json');
25
+ if (settingsAlreadyHasHook(settingsPath)) {
26
+ console.log(' [ai-hook-rules] .claude/settings.json already has the hook — skipping.');
27
+ return;
28
+ }
29
+ let settings = {};
30
+ if (fs.existsSync(settingsPath)) {
31
+ settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
32
+ }
33
+ if (!settings.hooks)
34
+ settings.hooks = {};
35
+ if (!Array.isArray(settings.hooks.PreToolUse))
36
+ settings.hooks.PreToolUse = [];
37
+ settings.hooks.PreToolUse.push({
38
+ matcher: 'Write|Edit|MultiEdit|Bash',
39
+ hooks: [{ type: 'command', command: HOOK_COMMAND }],
40
+ });
41
+ fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4) + '\n');
42
+ console.log(` [ai-hook-rules] Wired ${HOOK_COMMAND} into .claude/settings.json`);
43
+ }
44
+ function seedConfig(projectRoot) {
45
+ const configPath = path.join(projectRoot, CONFIG_FILENAME);
46
+ if (fs.existsSync(configPath)) {
47
+ console.log(` [ai-hook-rules] ${CONFIG_FILENAME} already exists — run with --sync to add missing rules.`);
48
+ return;
49
+ }
50
+ const rules = {};
51
+ for (const name of index_1.builtInRuleNames) {
52
+ rules[name] = { mode: 'OFF' };
53
+ }
54
+ const config = { rules, rulesDir: [] };
55
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 4) + '\n');
56
+ console.log(` [ai-hook-rules] Created ${CONFIG_FILENAME} with all rules set to OFF.`);
57
+ console.log(' Review and enable the rules you want by changing "mode" to "ON" or "MODIFIED_CODE" etc.');
58
+ }
59
+ function syncConfig(projectRoot) {
60
+ const configPath = path.join(projectRoot, CONFIG_FILENAME);
61
+ let config = { rules: {}, rulesDir: [] };
62
+ if (fs.existsSync(configPath)) {
63
+ config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
64
+ }
65
+ if (!config.rules)
66
+ config.rules = {};
67
+ const added = [];
68
+ for (const name of index_1.builtInRuleNames) {
69
+ if (!Object.prototype.hasOwnProperty.call(config.rules, name)) {
70
+ config.rules[name] = { mode: 'OFF' };
71
+ added.push(name);
72
+ }
73
+ }
74
+ if (added.length === 0) {
75
+ console.log(` [ai-hook-rules] ${CONFIG_FILENAME} is already up to date — no new rules to add.`);
76
+ return;
77
+ }
78
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 4) + '\n');
79
+ console.log(` [ai-hook-rules] Added ${added.length} new rule(s) to ${CONFIG_FILENAME} (set to OFF):`);
80
+ for (const name of added) {
81
+ console.log(` - ${name}`);
82
+ }
83
+ console.log(' Review each new rule and set "mode" to ON/MODIFIED_CODE/etc. as desired.');
84
+ }
85
+ function main() {
86
+ const args = process.argv.slice(2);
87
+ const isSync = args.includes('--sync');
88
+ const projectRoot = process.cwd();
89
+ if (isSync) {
90
+ syncConfig(projectRoot);
91
+ }
92
+ else {
93
+ seedConfig(projectRoot);
94
+ console.log('');
95
+ console.log(' To install the global Claude Code hook (one-time, per machine):');
96
+ console.log(' ./node_modules/.bin/wp-setup-global-ai-hooks');
97
+ }
98
+ }
99
+ if (require.main === module) {
100
+ main();
101
+ }
102
+ //# sourceMappingURL=setup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/bin/setup.ts"],"names":[],"mappings":";;AAiHA,oBAcC;;AA/HD,+CAAyB;AACzB,mDAA6B;AAE7B,+CAAuD;AAEvD,MAAM,YAAY,GAAG,gCAAgC,CAAC;AACtD,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAoBhD,SAAS,sBAAsB,CAAC,YAAoB;IAChD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC;QAAE,OAAO,KAAK,CAAC;IAC/C,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACtD,OAAO,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;AAC1C,CAAC;AAED,SAAS,YAAY,CAAC,WAAmB;IACrC,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;IACpD,IAAI,CAAC,E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,WAAW,CAAC,CAAC;QACxB,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChB,OAAO,CAAC,GAAG,CAAC,mEAAmE,CAAC,CAAC;QACjF,OAAO,CAAC,GAAG,CAAC,kDAAkD,CAAC,CAAC;IACpE,CAAC;AACL,CAAC;AAED,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC1B,IAAI,EAAE,CAAC;AACX,CAAC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nimport { builtInRuleNames } from '../core/rules/index';\n\nconst HOOK_COMMAND = './node_modules/.bin/wp-ai-hook';\nconst CONFIG_FILENAME = 'webpieces.config.json';\n\ninterface HookEntry {\n matcher: string;\n hooks: Array<{ type: string; command: string }>;\n}\n\ninterface ClaudeSettings {\n hooks?: {\n PreToolUse?: HookEntry[];\n };\n // webpieces-disable no-any-unknown -- opaque settings bag allows arbitrary key access\n [key: string]: unknown;\n}\n\ninterface RulesConfig {\n rules: Record<string, object>;\n rulesDir?: string[];\n}\n\nfunction settingsAlreadyHasHook(settingsPath: string): boolean {\n if (!fs.existsSync(settingsPath)) return false;\n const content = fs.readFileSync(settingsPath, 'utf8');\n return content.includes('wp-ai-hook');\n}\n\nfunction wireSettings(projectRoot: string): void {\n const claudeDir = path.join(projectRoot, '.claude');\n if (!fs.existsSync(claudeDir)) {\n console.log(' [ai-hook-rules] No .claude/ directory found — add the hook manually:');\n console.log(' In .claude/settings.json under hooks.PreToolUse, add:');\n console.log(` { \"matcher\": \"Write|Edit|MultiEdit|Bash\", \"hooks\": [{ \"type\": \"command\", \"command\": \"${HOOK_COMMAND}\" }] }`);\n return;\n }\n\n const settingsPath = path.join(claudeDir, 'settings.json');\n\n if (settingsAlreadyHasHook(settingsPath)) {\n console.log(' [ai-hook-rules] .claude/settings.json already has the hook — skipping.');\n return;\n }\n\n let settings: ClaudeSettings = {};\n if (fs.existsSync(settingsPath)) {\n settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8')) as ClaudeSettings;\n }\n\n if (!settings.hooks) settings.hooks = {};\n if (!Array.isArray(settings.hooks.PreToolUse)) settings.hooks.PreToolUse = [];\n\n settings.hooks.PreToolUse.push({\n matcher: 'Write|Edit|MultiEdit|Bash',\n hooks: [{ type: 'command', command: HOOK_COMMAND }],\n });\n\n fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4) + '\\n');\n console.log(` [ai-hook-rules] Wired ${HOOK_COMMAND} into .claude/settings.json`);\n}\n\nfunction seedConfig(projectRoot: string): void {\n const configPath = path.join(projectRoot, CONFIG_FILENAME);\n if (fs.existsSync(configPath)) {\n console.log(` [ai-hook-rules] ${CONFIG_FILENAME} already exists — run with --sync to add missing rules.`);\n return;\n }\n\n const rules: Record<string, object> = {};\n for (const name of builtInRuleNames) {\n rules[name] = { mode: 'OFF' };\n }\n\n const config: RulesConfig = { rules, rulesDir: [] };\n fs.writeFileSync(configPath, JSON.stringify(config, null, 4) + '\\n');\n console.log(` [ai-hook-rules] Created ${CONFIG_FILENAME} with all rules set to OFF.`);\n console.log(' Review and enable the rules you want by changing \"mode\" to \"ON\" or \"MODIFIED_CODE\" etc.');\n}\n\nfunction syncConfig(projectRoot: string): void {\n const configPath = path.join(projectRoot, CONFIG_FILENAME);\n\n let config: RulesConfig = { rules: {}, rulesDir: [] };\n if (fs.existsSync(configPath)) {\n config = JSON.parse(fs.readFileSync(configPath, 'utf8')) as RulesConfig;\n }\n if (!config.rules) config.rules = {};\n\n const added: string[] = [];\n for (const name of builtInRuleNames) {\n if (!Object.prototype.hasOwnProperty.call(config.rules, name)) {\n config.rules[name] = { mode: 'OFF' };\n added.push(name);\n }\n }\n\n if (added.length === 0) {\n console.log(` [ai-hook-rules] ${CONFIG_FILENAME} is already up to date — no new rules to add.`);\n return;\n }\n\n fs.writeFileSync(configPath, JSON.stringify(config, null, 4) + '\\n');\n console.log(` [ai-hook-rules] Added ${added.length} new rule(s) to ${CONFIG_FILENAME} (set to OFF):`);\n for (const name of added) {\n console.log(` - ${name}`);\n }\n console.log(' Review each new rule and set \"mode\" to ON/MODIFIED_CODE/etc. as desired.');\n}\n\nexport function main(): void {\n const args = process.argv.slice(2);\n const isSync = args.includes('--sync');\n\n const projectRoot = process.cwd();\n\n if (isSync) {\n syncConfig(projectRoot);\n } else {\n seedConfig(projectRoot);\n console.log('');\n console.log(' To install the global Claude Code hook (one-time, per machine):');\n console.log(' ./node_modules/.bin/wp-setup-global-ai-hooks');\n }\n}\n\nif (require.main === module) {\n main();\n}\n"]}
@@ -5,6 +5,7 @@ exports.globMatches = globMatches;
5
5
  const tslib_1 = require("tslib");
6
6
  const fs = tslib_1.__importStar(require("fs"));
7
7
  const path = tslib_1.__importStar(require("path"));
8
+ const types_1 = require("./types");
8
9
  const to_error_1 = require("./to-error");
9
10
  const index_1 = require("./rules/index");
10
11
  const no_any_unknown_1 = tslib_1.__importDefault(require("./rules/no-any-unknown"));
@@ -21,6 +22,7 @@ const branch_creation_guard_1 = tslib_1.__importDefault(require("./rules/branch-
21
22
  const pr_creation_guard_1 = tslib_1.__importDefault(require("./rules/pr-creation-guard"));
22
23
  const pr_merge_cleanup_1 = tslib_1.__importDefault(require("./rules/pr-merge-cleanup"));
23
24
  const no_direct_main_update_1 = tslib_1.__importDefault(require("./rules/no-direct-main-update"));
25
+ const no_js_files_1 = tslib_1.__importDefault(require("./rules/no-js-files"));
24
26
  const REQUIRED_FIELDS = ['name', 'description', 'scope', 'files', 'check'];
25
27
  const VALID_SCOPES = new Set(['edit', 'file', 'bash']);
26
28
  const BUILT_IN_RULE_MAP = {
@@ -38,6 +40,7 @@ const BUILT_IN_RULE_MAP = {
38
40
  'pr-creation-guard': pr_creation_guard_1.default,
39
41
  'pr-merge-cleanup': pr_merge_cleanup_1.default,
40
42
  'no-direct-main-update': no_direct_main_update_1.default,
43
+ 'no-js-files': no_js_files_1.default,
41
44
  };
42
45
  function loadRules(config, workspaceRoot) {
43
46
  const builtIns = loadBuiltInRules();
@@ -73,8 +76,7 @@ function loadCustomRules(rulesDirs, workspaceRoot) {
73
76
  }
74
77
  catch (err) {
75
78
  const error = (0, to_error_1.toError)(err);
76
- process.stderr.write(`[ai-hooks] cannot read rulesDir ${absDir}: ${error.message}\n`);
77
- continue;
79
+ throw new types_1.InformAiError(`Cannot read custom rules directory '${absDir}': ${error.message}`);
78
80
  }
79
81
  for (const entry of entries) {
80
82
  const full = path.join(absDir, entry);
@@ -85,7 +87,7 @@ function loadCustomRules(rulesDirs, workspaceRoot) {
85
87
  }
86
88
  catch (err) {
87
89
  const error = (0, to_error_1.toError)(err);
88
- process.stderr.write(`[ai-hooks] failed to load custom rule ${full}: ${error.message}\n`);
90
+ throw new types_1.InformAiError(`Cannot load custom rule '${full}': ${error.message}`);
89
91
  }
90
92
  }
91
93
  }
@@ -1 +1 @@
1
- {"version":3,"file":"load-rules.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/load-rules.ts"],"names":[],"mappings":";;AAyCA,8BAKC;AA6ED,kCAGC;;AA9HD,+CAAyB;AACzB,mDAA6B;AAG7B,yCAAqC;AACrC,yCAAiD;AACjD,oFAAkD;AAClD,sFAAoD;AACpD,oFAAkD;AAClD,4FAAyD;AACzD,oFAAmD;AACnD,8FAA4D;AAC5D,sGAAoE;AACpE,8FAA4D;AAC5D,kGAAgE;AAChE,8FAA2D;AAC3D,kGAAgE;AAChE,0FAAwD;AACxD,wFAAsD;AACtD,kGAA+D;AAE/D,MAAM,eAAe,GAAsB,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAC9F,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAEvD,MAAM,iBAAiB,GAAyB;IAC5C,gBAAgB,EAAE,wBAAoB;IACtC,iBAAiB,EAAE,yBAAqB;IACxC,gBAAgB,EAAE,wBAAoB;IACtC,oBAAoB,EAAE,4BAA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,CAAC,CAAC;IACX,CAAC;IACD,OAAO,IAAI,MAAM,CAAC,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nimport type { Rule, ResolvedConfig } from './types';\nimport { toError } from './to-error';\nimport { builtInRuleNames } from './rules/index';\nimport noAnyUnknown from './rules/no-any-unknown';\nimport noImplicitAny from './rules/no-implicit-any';\nimport maxFileLines from './rules/max-file-lines';\nimport validateTsInSrc from './rules/validate-ts-in-src';\nimport noDestructure from './rules/no-destructure';\nimport requireReturnType from './rules/require-return-type';\nimport noUnmanagedExceptions from './rules/no-unmanaged-exceptions';\nimport catchErrorPattern from './rules/catch-error-pattern';\nimport noShellSubstitution from './rules/no-shell-substitution';\nimport noSymbolDiTokens from './rules/no-symbol-di-tokens';\nimport branchCreationGuard from './rules/branch-creation-guard';\nimport prCreationGuard from './rules/pr-creation-guard';\nimport prMergeCleanup from './rules/pr-merge-cleanup';\nimport noDirectMainUpdate from './rules/no-direct-main-update';\n\nconst REQUIRED_FIELDS: readonly string[] = ['name', 'description', 'scope', 'files', 'check'];\nconst VALID_SCOPES = new Set(['edit', 'file', 'bash']);\n\nconst BUILT_IN_RULE_MAP: Record<string, Rule> = {\n 'no-any-unknown': noAnyUnknown as Rule,\n 'no-implicit-any': noImplicitAny as Rule,\n 'max-file-lines': maxFileLines as Rule,\n 'validate-ts-in-src': validateTsInSrc as Rule,\n 'no-destructure': noDestructure as Rule,\n 'require-return-type': requireReturnType as Rule,\n 'no-unmanaged-exceptions': noUnmanagedExceptions as Rule,\n 'catch-error-pattern': catchErrorPattern as Rule,\n 'no-shell-substitution': noShellSubstitution as Rule,\n 'no-symbol-di-tokens': noSymbolDiTokens as Rule,\n 'branch-creation-guard': branchCreationGuard as Rule,\n 'pr-creation-guard': prCreationGuard as Rule,\n 'pr-merge-cleanup': prMergeCleanup as Rule,\n 'no-direct-main-update': noDirectMainUpdate as Rule,\n};\n\nexport function loadRules(config: ResolvedConfig, workspaceRoot: string): readonly Rule[] {\n const builtIns = loadBuiltInRules();\n const custom = loadCustomRules(config.rulesDir, workspaceRoot);\n const all: Rule[] = [...builtIns, ...custom];\n return all.filter((rule: Rule) => validateRule(rule));\n}\n\nfunction loadBuiltInRules(): Rule[] {\n const modules: Rule[] = [];\n for (const name of builtInRuleNames) {\n const mod = BUILT_IN_RULE_MAP[name];\n if (mod) {\n modules.push(mod);\n } else {\n process.stderr.write(`[ai-hooks] unknown built-in rule: ${name}\\n`);\n }\n }\n return modules;\n}\n\nfunction loadCustomRules(rulesDirs: readonly string[], workspaceRoot: string): Rule[] {\n const modules: Rule[] = [];\n for (const dir of rulesDirs) {\n const absDir = path.isAbsolute(dir) ? dir : path.join(workspaceRoot, dir);\n if (!fs.existsSync(absDir)) {\n process.stderr.write(`[ai-hooks] rulesDir not found: ${absDir}\\n`);\n continue;\n }\n let entries: string[];\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n entries = fs.readdirSync(absDir).filter((e) => e.endsWith('.js'));\n } catch (err: unknown) {\n const error = toError(err);\n process.stderr.write(`[ai-hooks] cannot read rulesDir ${absDir}: ${error.message}\\n`);\n continue;\n }\n for (const entry of entries) {\n const full = path.join(absDir, entry);\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const mod = require(full);\n modules.push(mod.default || mod);\n } catch (err: unknown) {\n const error = toError(err);\n process.stderr.write(`[ai-hooks] failed to load custom rule ${full}: ${error.message}\\n`);\n }\n }\n }\n return modules;\n}\n\n// webpieces-disable no-any-unknown -- validates untrusted require() output at system boundary\nfunction validateRule(rule: unknown): rule is Rule {\n if (!rule || typeof rule !== 'object') {\n process.stderr.write('[ai-hooks] rule is not an object, skipping\\n');\n return false;\n }\n // webpieces-disable no-any-unknown -- narrowing from unknown at system boundary\n const obj = rule as Record<string, unknown>;\n for (const field of REQUIRED_FIELDS) {\n if (obj[field] === undefined) {\n const name = typeof obj['name'] === 'string' ? obj['name'] : '<unnamed>';\n process.stderr.write(`[ai-hooks] rule \"${name}\" missing required field: ${field}\\n`);\n return false;\n }\n }\n if (!VALID_SCOPES.has(obj['scope'] as string)) {\n process.stderr.write(`[ai-hooks] rule \"${obj['name']}\" has invalid scope: ${String(obj['scope'])}\\n`);\n return false;\n }\n if (!Array.isArray(obj['files'])) {\n process.stderr.write(`[ai-hooks] rule \"${obj['name']}\" files must be an array\\n`);\n return false;\n }\n if (typeof obj['check'] !== 'function') {\n process.stderr.write(`[ai-hooks] rule \"${obj['name']}\" check must be a function\\n`);\n return false;\n }\n return true;\n}\n\nexport function globMatches(pattern: string, filePath: string): boolean {\n const regex = globToRegex(pattern);\n return regex.test(filePath);\n}\n\nfunction globToRegex(pattern: string): RegExp {\n let re = '';\n let i = 0;\n while (i < pattern.length) {\n const ch = pattern[i];\n if (ch === '*') {\n if (pattern[i + 1] === '*') {\n re += '.*';\n i += 2;\n if (pattern[i] === '/') i += 1;\n continue;\n }\n re += '[^/]*';\n i += 1;\n continue;\n }\n if (ch === '?') {\n re += '[^/]';\n i += 1;\n continue;\n }\n if ('.+^$(){}|[]\\\\'.includes(ch)) {\n re += '\\\\' + ch;\n i += 1;\n continue;\n }\n re += ch;\n i += 1;\n }\n return new RegExp('^' + re + '$');\n}\n"]}
1
+ {"version":3,"file":"load-rules.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/load-rules.ts"],"names":[],"mappings":";;AA4CA,8BAKC;AA4ED,kCAGC;;AAhID,+CAAyB;AACzB,mDAA6B;AAG7B,mCAAwC;AACxC,yCAAqC;AACrC,yCAAiD;AACjD,oFAAkD;AAClD,sFAAoD;AACpD,oFAAkD;AAClD,4FAAyD;AACzD,oFAAmD;AACnD,8FAA4D;AAC5D,sGAAoE;AACpE,8FAA4D;AAC5D,kGAAgE;AAChE,8FAA2D;AAC3D,kGAAgE;AAChE,0FAAwD;AACxD,wFAAsD;AACtD,kGAA+D;AAC/D,8EAA4C;AAE5C,MA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;QACD,IAAI,EAAE,KAAK,GAAG,EAAE,CAAC;YACb,EAAE,IAAI,MAAM,CAAC;YACb,CAAC,IAAI,CAAC,CAAC;YACP,SAAS;QACb,CAAC;QACD,IAAI,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;YAC/B,EAAE,IAAI,IAAI,GAAG,EAAE,CAAC;YAChB,CAAC,IAAI,CAAC,CAAC;YACP,SAAS;QACb,CAAC;QACD,EAAE,IAAI,EAAE,CAAC;QACT,CAAC,IAAI,CAAC,CAAC;IACX,CAAC;IACD,OAAO,IAAI,MAAM,CAAC,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nimport type { Rule, ResolvedConfig } from './types';\nimport { InformAiError } from './types';\nimport { toError } from './to-error';\nimport { builtInRuleNames } from './rules/index';\nimport noAnyUnknown from './rules/no-any-unknown';\nimport noImplicitAny from './rules/no-implicit-any';\nimport maxFileLines from './rules/max-file-lines';\nimport validateTsInSrc from './rules/validate-ts-in-src';\nimport noDestructure from './rules/no-destructure';\nimport requireReturnType from './rules/require-return-type';\nimport noUnmanagedExceptions from './rules/no-unmanaged-exceptions';\nimport catchErrorPattern from './rules/catch-error-pattern';\nimport noShellSubstitution from './rules/no-shell-substitution';\nimport noSymbolDiTokens from './rules/no-symbol-di-tokens';\nimport branchCreationGuard from './rules/branch-creation-guard';\nimport prCreationGuard from './rules/pr-creation-guard';\nimport prMergeCleanup from './rules/pr-merge-cleanup';\nimport noDirectMainUpdate from './rules/no-direct-main-update';\nimport noJsFiles from './rules/no-js-files';\n\nconst REQUIRED_FIELDS: readonly string[] = ['name', 'description', 'scope', 'files', 'check'];\nconst VALID_SCOPES = new Set(['edit', 'file', 'bash']);\n\nconst BUILT_IN_RULE_MAP: Record<string, Rule> = {\n 'no-any-unknown': noAnyUnknown as Rule,\n 'no-implicit-any': noImplicitAny as Rule,\n 'max-file-lines': maxFileLines as Rule,\n 'validate-ts-in-src': validateTsInSrc as Rule,\n 'no-destructure': noDestructure as Rule,\n 'require-return-type': requireReturnType as Rule,\n 'no-unmanaged-exceptions': noUnmanagedExceptions as Rule,\n 'catch-error-pattern': catchErrorPattern as Rule,\n 'no-shell-substitution': noShellSubstitution as Rule,\n 'no-symbol-di-tokens': noSymbolDiTokens as Rule,\n 'branch-creation-guard': branchCreationGuard as Rule,\n 'pr-creation-guard': prCreationGuard as Rule,\n 'pr-merge-cleanup': prMergeCleanup as Rule,\n 'no-direct-main-update': noDirectMainUpdate as Rule,\n 'no-js-files': noJsFiles as Rule,\n};\n\nexport function loadRules(config: ResolvedConfig, workspaceRoot: string): readonly Rule[] {\n const builtIns = loadBuiltInRules();\n const custom = loadCustomRules(config.rulesDir, workspaceRoot);\n const all: Rule[] = [...builtIns, ...custom];\n return all.filter((rule: Rule) => validateRule(rule));\n}\n\nfunction loadBuiltInRules(): Rule[] {\n const modules: Rule[] = [];\n for (const name of builtInRuleNames) {\n const mod = BUILT_IN_RULE_MAP[name];\n if (mod) {\n modules.push(mod);\n } else {\n process.stderr.write(`[ai-hooks] unknown built-in rule: ${name}\\n`);\n }\n }\n return modules;\n}\n\nfunction loadCustomRules(rulesDirs: readonly string[], workspaceRoot: string): Rule[] {\n const modules: Rule[] = [];\n for (const dir of rulesDirs) {\n const absDir = path.isAbsolute(dir) ? dir : path.join(workspaceRoot, dir);\n if (!fs.existsSync(absDir)) {\n process.stderr.write(`[ai-hooks] rulesDir not found: ${absDir}\\n`);\n continue;\n }\n let entries: string[];\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n entries = fs.readdirSync(absDir).filter((e) => e.endsWith('.js'));\n } catch (err: unknown) {\n const error = toError(err);\n throw new InformAiError(`Cannot read custom rules directory '${absDir}': ${error.message}`);\n }\n for (const entry of entries) {\n const full = path.join(absDir, entry);\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const mod = require(full);\n modules.push(mod.default || mod);\n } catch (err: unknown) {\n const error = toError(err);\n throw new InformAiError(`Cannot load custom rule '${full}': ${error.message}`);\n }\n }\n }\n return modules;\n}\n\n// webpieces-disable no-any-unknown -- validates untrusted require() output at system boundary\nfunction validateRule(rule: unknown): rule is Rule {\n if (!rule || typeof rule !== 'object') {\n process.stderr.write('[ai-hooks] rule is not an object, skipping\\n');\n return false;\n }\n // webpieces-disable no-any-unknown -- narrowing from unknown at system boundary\n const obj = rule as Record<string, unknown>;\n for (const field of REQUIRED_FIELDS) {\n if (obj[field] === undefined) {\n const name = typeof obj['name'] === 'string' ? obj['name'] : '<unnamed>';\n process.stderr.write(`[ai-hooks] rule \"${name}\" missing required field: ${field}\\n`);\n return false;\n }\n }\n if (!VALID_SCOPES.has(obj['scope'] as string)) {\n process.stderr.write(`[ai-hooks] rule \"${obj['name']}\" has invalid scope: ${String(obj['scope'])}\\n`);\n return false;\n }\n if (!Array.isArray(obj['files'])) {\n process.stderr.write(`[ai-hooks] rule \"${obj['name']}\" files must be an array\\n`);\n return false;\n }\n if (typeof obj['check'] !== 'function') {\n process.stderr.write(`[ai-hooks] rule \"${obj['name']}\" check must be a function\\n`);\n return false;\n }\n return true;\n}\n\nexport function globMatches(pattern: string, filePath: string): boolean {\n const regex = globToRegex(pattern);\n return regex.test(filePath);\n}\n\nfunction globToRegex(pattern: string): RegExp {\n let re = '';\n let i = 0;\n while (i < pattern.length) {\n const ch = pattern[i];\n if (ch === '*') {\n if (pattern[i + 1] === '*') {\n re += '.*';\n i += 2;\n if (pattern[i] === '/') i += 1;\n continue;\n }\n re += '[^/]*';\n i += 1;\n continue;\n }\n if (ch === '?') {\n re += '[^/]';\n i += 1;\n continue;\n }\n if ('.+^$(){}|[]\\\\'.includes(ch)) {\n re += '\\\\' + ch;\n i += 1;\n continue;\n }\n re += ch;\n i += 1;\n }\n return new RegExp('^' + re + '$');\n}\n"]}
@@ -6,6 +6,7 @@ exports.builtInRuleNames = [
6
6
  'no-implicit-any',
7
7
  'max-file-lines',
8
8
  'validate-ts-in-src',
9
+ 'no-js-files',
9
10
  'no-destructure',
10
11
  'require-return-type',
11
12
  'no-unmanaged-exceptions',
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAsB;IAC/C,gBAAgB;IAChB,iBAAiB;IACjB,gBAAgB;IAChB,oBAAoB;IACpB,gBAAgB;IAChB,qBAAqB;IACrB,yBAAyB;IACzB,qBAAqB;IACrB,uBAAuB;IACvB,qBAAqB;IACrB,uBAAuB;IACvB,mBAAmB;IACnB,kBAAkB;IAClB,uBAAuB;CAC1B,CAAC","sourcesContent":["export const builtInRuleNames: readonly string[] = [\n 'no-any-unknown',\n 'no-implicit-any',\n 'max-file-lines',\n 'validate-ts-in-src',\n 'no-destructure',\n 'require-return-type',\n 'no-unmanaged-exceptions',\n 'catch-error-pattern',\n 'no-shell-substitution',\n 'no-symbol-di-tokens',\n 'branch-creation-guard',\n 'pr-creation-guard',\n 'pr-merge-cleanup',\n 'no-direct-main-update',\n];\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAsB;IAC/C,gBAAgB;IAChB,iBAAiB;IACjB,gBAAgB;IAChB,oBAAoB;IACpB,aAAa;IACb,gBAAgB;IAChB,qBAAqB;IACrB,yBAAyB;IACzB,qBAAqB;IACrB,uBAAuB;IACvB,qBAAqB;IACrB,uBAAuB;IACvB,mBAAmB;IACnB,kBAAkB;IAClB,uBAAuB;CAC1B,CAAC","sourcesContent":["export const builtInRuleNames: readonly string[] = [\n 'no-any-unknown',\n 'no-implicit-any',\n 'max-file-lines',\n 'validate-ts-in-src',\n 'no-js-files',\n 'no-destructure',\n 'require-return-type',\n 'no-unmanaged-exceptions',\n 'catch-error-pattern',\n 'no-shell-substitution',\n 'no-symbol-di-tokens',\n 'branch-creation-guard',\n 'pr-creation-guard',\n 'pr-merge-cleanup',\n 'no-direct-main-update',\n];\n"]}
@@ -0,0 +1,3 @@
1
+ import type { FileRule } from '../types';
2
+ declare const noJsFiles: FileRule;
3
+ export default noJsFiles;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const rules_config_1 = require("@webpieces/rules-config");
4
+ const types_1 = require("../types");
5
+ const noJsFiles = {
6
+ name: 'no-js-files',
7
+ description: 'Disallow writing new .js/.jsx files. Use .ts/.tsx instead.',
8
+ scope: 'file',
9
+ files: ['**/*.js', '**/*.jsx'],
10
+ defaultOptions: {
11
+ allowedPaths: [],
12
+ },
13
+ fixHint: [
14
+ 'Write a .ts or .tsx file instead of .js/.jsx.',
15
+ 'If this path must be .js (e.g. a generated or legacy file), add it to no-js-files.allowedPaths in webpieces.config.json',
16
+ ],
17
+ check(ctx) {
18
+ if (ctx.tool !== 'Write')
19
+ return [];
20
+ const allowedPaths = Array.isArray(ctx.options['allowedPaths'])
21
+ ? ctx.options['allowedPaths']
22
+ : [];
23
+ if ((0, rules_config_1.isPathExcluded)(ctx.relativePath, allowedPaths))
24
+ return [];
25
+ return [new types_1.Violation(1, ctx.relativePath, 'Writing .js/.jsx files is not allowed. Use .ts/.tsx instead.')];
26
+ },
27
+ };
28
+ exports.default = noJsFiles;
29
+ //# sourceMappingURL=no-js-files.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-js-files.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/no-js-files.ts"],"names":[],"mappings":";;AAAA,0DAAyD;AAGzD,oCAA0C;AAE1C,MAAM,SAAS,GAAa;IACxB,IAAI,EAAE,aAAa;IACnB,WAAW,EAAE,4DAA4D;IACzE,KAAK,EAAE,MAAM;IACb,KAAK,EAAE,CAAC,SAAS,EAAE,UAAU,CAAC;IAC9B,cAAc,EAAE;QACZ,YAAY,EAAE,EAAE;KACnB;IACD,OAAO,EAAE;QACL,+CAA+C;QAC/C,yHAAyH;KAC5H;IAED,KAAK,CAAC,GAAgB;QAClB,IAAI,GAAG,CAAC,IAAI,KAAK,OAAO;YAAE,OAAO,EAAE,CAAC;QAEpC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;YAC3D,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,cAAc,CAAa;YACzC,CAAC,CAAC,EAAE,CAAC;QAET,IAAI,IAAA,6BAAc,EAAC,GAAG,CAAC,YAAY,EAAE,YAAY,CAAC;YAAE,OAAO,EAAE,CAAC;QAE9D,OAAO,CAAC,IAAI,iBAAC,CAAC,CAAC,EAAE,GAAG,CAAC,YAAY,EAAE,8DAA8D,CAAC,CAAC,CAAC;IACxG,CAAC;CACJ,CAAC;AAEF,kBAAe,SAAS,CAAC","sourcesContent":["import { isPathExcluded } from '@webpieces/rules-config';\n\nimport type { FileRule, FileContext, Violation } from '../types';\nimport { Violation as V } from '../types';\n\nconst noJsFiles: FileRule = {\n name: 'no-js-files',\n description: 'Disallow writing new .js/.jsx files. Use .ts/.tsx instead.',\n scope: 'file',\n files: ['**/*.js', '**/*.jsx'],\n defaultOptions: {\n allowedPaths: [],\n },\n fixHint: [\n 'Write a .ts or .tsx file instead of .js/.jsx.',\n 'If this path must be .js (e.g. a generated or legacy file), add it to no-js-files.allowedPaths in webpieces.config.json',\n ],\n\n check(ctx: FileContext): readonly Violation[] {\n if (ctx.tool !== 'Write') return [];\n\n const allowedPaths = Array.isArray(ctx.options['allowedPaths'])\n ? ctx.options['allowedPaths'] as string[]\n : [];\n\n if (isPathExcluded(ctx.relativePath, allowedPaths)) return [];\n\n return [new V(1, ctx.relativePath, 'Writing .js/.jsx files is not allowed. Use .ts/.tsx instead.')];\n },\n};\n\nexport default noJsFiles;\n"]}
@@ -7,7 +7,6 @@ const rules_config_1 = require("@webpieces/rules-config");
7
7
  const types_1 = require("../types");
8
8
  const DEFAULT_EXCLUDE_PATHS = [
9
9
  'node_modules', 'dist', '.nx', '.git',
10
- 'architecture', 'tmp', 'scripts',
11
10
  '**/*.d.ts', '**/jest.config.ts',
12
11
  ];
13
12
  const DEFAULT_ALLOWED_ROOT_FILES = ['jest.setup.ts'];
@@ -1 +1 @@
1
- {"version":3,"file":"validate-ts-in-src.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/validate-ts-in-src.ts"],"names":[],"mappings":";;;AAAA,+CAAyB;AACzB,mDAA6B;AAE7B,0DAAyD;AAGzD,oCAA0C;AAE1C,MAAM,qBAAqB,GAAG;IAC1B,cAAc,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IACrC,cAAc,EAAE,KAAK,EAAE,SAAS;IAChC,WAAW,EAAE,mBAAmB;CACnC,CAAC;AACF,MAAM,0BAA0B,GAAG,CAAC,eAAe,CAAC,CAAC;AAErD,SAAS,eAAe,CAAC,QAAgB,EAAE,aAAqB;IAC5D,IAAI,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACjC,OAAO,GAAG,KAAK,aAAa,IAAI,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,EAAE,CAAC;QAC5D,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;YAAE,OAAO,GAAG,CAAC;QAC9D,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,MAAM,mBAAmB,GAAa;IAClC,IAAI,EAAE,oBAAoB;IAC1B,WAAW,EAAE,4DAA4D;IACzE,KAAK,EAAE,MAAM;IACb,KAAK,EAAE,CAAC,SAAS,EAAE,UAAU,CAAC;IAC9B,cAAc,EAAE;QACZ,YAAY,EAAE,qBAAqB;QACnC,gBA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AC,YAAY,CAAC,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,YAAY,KAAK,KAAK,EAAE,CAAC;YACvE,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;YAClE,OAAO,CAAC,IAAI,iBAAC,CACT,CAAC,EACD,GAAG,CAAC,YAAY,EAChB,4BAA4B,WAAW,uDAAuD,CACjG,CAAC,CAAC;QACP,CAAC;QAED,OAAO,EAAE,CAAC;IACd,CAAC;CACJ,CAAC;AAEF,kBAAe,mBAAmB,CAAC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nimport { isPathExcluded } from '@webpieces/rules-config';\n\nimport type { FileRule, FileContext, Violation } from '../types';\nimport { Violation as V } from '../types';\n\nconst DEFAULT_EXCLUDE_PATHS = [\n 'node_modules', 'dist', '.nx', '.git',\n 'architecture', 'tmp', 'scripts',\n '**/*.d.ts', '**/jest.config.ts',\n];\nconst DEFAULT_ALLOWED_ROOT_FILES = ['jest.setup.ts'];\n\nfunction findProjectRoot(filePath: string, workspaceRoot: string): string | null {\n let dir = path.dirname(filePath);\n while (dir !== workspaceRoot && dir.startsWith(workspaceRoot)) {\n if (fs.existsSync(path.join(dir, 'project.json'))) return dir;\n dir = path.dirname(dir);\n }\n return null;\n}\n\nconst validateTsInSrcRule: FileRule = {\n name: 'validate-ts-in-src',\n description: 'Every .ts file must belong to a project\\'s src/ directory.',\n scope: 'file',\n files: ['**/*.ts', '**/*.tsx'],\n defaultOptions: {\n excludePaths: DEFAULT_EXCLUDE_PATHS,\n allowedRootFiles: DEFAULT_ALLOWED_ROOT_FILES,\n },\n fixHint: [\n 'Move the file into an existing project\\'s src/ directory, or create a new project with project.json that owns the directory.',\n 'Add a dir or glob (e.g. \"**/codegen.ts\") to validate-ts-in-src.excludePaths in webpieces.config.json',\n ],\n\n check(ctx: FileContext): readonly Violation[] {\n if (ctx.tool !== 'Write') return [];\n\n const excludePaths = Array.isArray(ctx.options['excludePaths'])\n ? ctx.options['excludePaths'] as string[]\n : DEFAULT_EXCLUDE_PATHS;\n const allowedRootFiles = Array.isArray(ctx.options['allowedRootFiles'])\n ? ctx.options['allowedRootFiles'] as string[]\n : DEFAULT_ALLOWED_ROOT_FILES;\n\n // Holistic exclusion (Layer 1 + Layer 2): bare dir names + globs.\n if (isPathExcluded(ctx.relativePath, excludePaths)) return [];\n\n const relParts = ctx.relativePath.split(path.sep);\n if (relParts.length === 1 && allowedRootFiles.indexOf(relParts[0] ?? '') >= 0) return [];\n\n const projectRoot = findProjectRoot(ctx.filePath, ctx.workspaceRoot);\n\n if (!projectRoot) {\n return [new V(\n 1,\n ctx.relativePath,\n 'File is not inside any Nx project. Move it into a project\\'s src/ directory.',\n )];\n }\n\n const relToProject = path.relative(projectRoot, ctx.filePath);\n if (!relToProject.startsWith('src' + path.sep) && relToProject !== 'src') {\n const projectName = path.relative(ctx.workspaceRoot, projectRoot);\n return [new V(\n 1,\n ctx.relativePath,\n `File is inside project \\`${projectName}\\` but outside its src/ directory. Move it into src/.`,\n )];\n }\n\n return [];\n },\n};\n\nexport default validateTsInSrcRule;\n"]}
1
+ {"version":3,"file":"validate-ts-in-src.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/validate-ts-in-src.ts"],"names":[],"mappings":";;;AAAA,+CAAyB;AACzB,mDAA6B;AAE7B,0DAAyD;AAGzD,oCAA0C;AAE1C,MAAM,qBAAqB,GAAG;IAC1B,cAAc,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IACrC,WAAW,EAAE,mBAAmB;CACnC,CAAC;AACF,MAAM,0BAA0B,GAAG,CAAC,eAAe,CAAC,CAAC;AAErD,SAAS,eAAe,CAAC,QAAgB,EAAE,aAAqB;IAC5D,IAAI,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACjC,OAAO,GAAG,KAAK,aAAa,IAAI,GA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AC,MAAM,KAAK,CAAC,IAAI,gBAAgB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC;YAAE,OAAO,EAAE,CAAC;QAEzF,MAAM,WAAW,GAAG,eAAe,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;QAErE,IAAI,CAAC,WAAW,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,iBAAC,CACT,CAAC,EACD,GAAG,CAAC,YAAY,EAChB,8EAA8E,CACjF,CAAC,CAAC;QACP,CAAC;QAED,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC9D,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,YAAY,KAAK,KAAK,EAAE,CAAC;YACvE,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;YAClE,OAAO,CAAC,IAAI,iBAAC,CACT,CAAC,EACD,GAAG,CAAC,YAAY,EAChB,4BAA4B,WAAW,uDAAuD,CACjG,CAAC,CAAC;QACP,CAAC;QAED,OAAO,EAAE,CAAC;IACd,CAAC;CACJ,CAAC;AAEF,kBAAe,mBAAmB,CAAC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nimport { isPathExcluded } from '@webpieces/rules-config';\n\nimport type { FileRule, FileContext, Violation } from '../types';\nimport { Violation as V } from '../types';\n\nconst DEFAULT_EXCLUDE_PATHS = [\n 'node_modules', 'dist', '.nx', '.git',\n '**/*.d.ts', '**/jest.config.ts',\n];\nconst DEFAULT_ALLOWED_ROOT_FILES = ['jest.setup.ts'];\n\nfunction findProjectRoot(filePath: string, workspaceRoot: string): string | null {\n let dir = path.dirname(filePath);\n while (dir !== workspaceRoot && dir.startsWith(workspaceRoot)) {\n if (fs.existsSync(path.join(dir, 'project.json'))) return dir;\n dir = path.dirname(dir);\n }\n return null;\n}\n\nconst validateTsInSrcRule: FileRule = {\n name: 'validate-ts-in-src',\n description: 'Every .ts file must belong to a project\\'s src/ directory.',\n scope: 'file',\n files: ['**/*.ts', '**/*.tsx'],\n defaultOptions: {\n excludePaths: DEFAULT_EXCLUDE_PATHS,\n allowedRootFiles: DEFAULT_ALLOWED_ROOT_FILES,\n },\n fixHint: [\n 'Move the file into an existing project\\'s src/ directory, or create a new project with project.json that owns the directory.',\n 'Add a dir or glob (e.g. \"**/codegen.ts\") to validate-ts-in-src.excludePaths in webpieces.config.json',\n ],\n\n check(ctx: FileContext): readonly Violation[] {\n if (ctx.tool !== 'Write') return [];\n\n const excludePaths = Array.isArray(ctx.options['excludePaths'])\n ? ctx.options['excludePaths'] as string[]\n : DEFAULT_EXCLUDE_PATHS;\n const allowedRootFiles = Array.isArray(ctx.options['allowedRootFiles'])\n ? ctx.options['allowedRootFiles'] as string[]\n : DEFAULT_ALLOWED_ROOT_FILES;\n\n // Holistic exclusion (Layer 1 + Layer 2): bare dir names + globs.\n if (isPathExcluded(ctx.relativePath, excludePaths)) return [];\n\n const relParts = ctx.relativePath.split(path.sep);\n if (relParts.length === 1 && allowedRootFiles.indexOf(relParts[0] ?? '') >= 0) return [];\n\n const projectRoot = findProjectRoot(ctx.filePath, ctx.workspaceRoot);\n\n if (!projectRoot) {\n return [new V(\n 1,\n ctx.relativePath,\n 'File is not inside any Nx project. Move it into a project\\'s src/ directory.',\n )];\n }\n\n const relToProject = path.relative(projectRoot, ctx.filePath);\n if (!relToProject.startsWith('src' + path.sep) && relToProject !== 'src') {\n const projectName = path.relative(ctx.workspaceRoot, projectRoot);\n return [new V(\n 1,\n ctx.relativePath,\n `File is inside project \\`${projectName}\\` but outside its src/ directory. Move it into src/.`,\n )];\n }\n\n return [];\n },\n};\n\nexport default validateTsInSrcRule;\n"]}
@@ -11,24 +11,22 @@ const to_error_1 = require("./to-error");
11
11
  const report_1 = require("./report");
12
12
  const types_1 = require("./types");
13
13
  function run(toolKind, input, cwd) {
14
- // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
15
- try {
16
- return runInternal(toolKind, input, cwd);
17
- }
18
- catch (err) {
19
- const error = (0, to_error_1.toError)(err);
20
- console.error(`[ai-hooks] runner crashed (failing open): ${error.message}`);
21
- return null;
22
- }
14
+ return runInternal(toolKind, input, cwd);
23
15
  }
24
16
  function runInternal(toolKind, input, cwd) {
25
17
  const config = (0, load_config_1.loadConfig)(cwd);
26
- if (!config.configPath)
27
- return null;
18
+ if (!config.configPath) {
19
+ return new types_1.BlockedResult('webpieces.config.json not found.\n' +
20
+ 'Tell the human: run `./node_modules/.bin/wp-setup-ai-hooks` to initialize the project configuration.\n' +
21
+ 'Do not proceed until the human has done this.');
22
+ }
28
23
  const workspaceRoot = path.dirname(config.configPath);
29
24
  const rules = (0, load_rules_1.loadRules)(config, workspaceRoot);
30
25
  if (rules.length === 0)
31
26
  return null;
27
+ const outOfSync = checkConfigSync(rules, config);
28
+ if (outOfSync)
29
+ return outOfSync;
32
30
  const contexts = (0, build_context_1.buildContexts)(toolKind, input, workspaceRoot);
33
31
  const relativePath = path.relative(workspaceRoot, input.filePath);
34
32
  const editGroups = runEditRules(rules, contexts.editContexts, config);
@@ -40,24 +38,22 @@ function runInternal(toolKind, input, cwd) {
40
38
  return new types_1.BlockedResult(report);
41
39
  }
42
40
  function runBash(command, cwd) {
43
- // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
44
- try {
45
- return runBashInternal(command, cwd);
46
- }
47
- catch (err) {
48
- const error = (0, to_error_1.toError)(err);
49
- console.error(`[ai-hooks] bash runner crashed (failing open): ${error.message}`);
50
- return null;
51
- }
41
+ return runBashInternal(command, cwd);
52
42
  }
53
43
  function runBashInternal(command, cwd) {
54
44
  const config = (0, load_config_1.loadConfig)(cwd);
55
- if (!config.configPath)
56
- return null;
45
+ if (!config.configPath) {
46
+ return new types_1.BlockedResult('webpieces.config.json not found.\n' +
47
+ 'Tell the human: run `./node_modules/.bin/wp-setup-ai-hooks` to initialize the project configuration.\n' +
48
+ 'Do not proceed until the human has done this.');
49
+ }
57
50
  const workspaceRoot = path.dirname(config.configPath);
58
51
  const rules = (0, load_rules_1.loadRules)(config, workspaceRoot);
59
52
  if (rules.length === 0)
60
53
  return null;
54
+ const outOfSync = checkConfigSync(rules, config);
55
+ if (outOfSync)
56
+ return outOfSync;
61
57
  const ctx = (0, build_context_1.buildBashContext)(command, workspaceRoot);
62
58
  const groups = runBashRules(rules, ctx, config);
63
59
  if (groups.length === 0)
@@ -65,15 +61,31 @@ function runBashInternal(command, cwd) {
65
61
  const report = (0, report_1.formatReport)('<bash>', groups);
66
62
  return new types_1.BlockedResult(report);
67
63
  }
68
- function safeCheckBash(rule, ctx) {
64
+ function checkConfigSync(rules, config) {
65
+ const unconfigured = rules.filter((r) => !config.rules.has(r.name)).map((r) => r.name);
66
+ if (unconfigured.length === 0)
67
+ return null;
68
+ const lines = [
69
+ 'webpieces.config.json is out of sync — new rules have been added that are not yet configured.',
70
+ '',
71
+ `Unconfigured rules: ${unconfigured.join(', ')}`,
72
+ '',
73
+ 'Ask the human: for each rule above, do you want it ON or OFF (and any specific options)?',
74
+ 'Then run `./node_modules/.bin/wp-setup-ai-hooks --sync` to add missing rules (defaults to OFF),',
75
+ 'or edit webpieces.config.json manually to add an entry for each rule.',
76
+ 'Do not proceed until webpieces.config.json is updated.',
77
+ ];
78
+ return new types_1.BlockedResult(lines.join('\n'));
79
+ }
80
+ // N-legs pattern: each rule runs independently; crash → visible violation so AI sees it, not silent []
81
+ function runRuleCheck(rule, ctx) {
69
82
  // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
70
83
  try {
71
84
  return rule.check(ctx);
72
85
  }
73
86
  catch (err) {
74
87
  const error = (0, to_error_1.toError)(err);
75
- process.stderr.write(`[ai-hooks] rule ${rule.name} crashed: ${error.message}\n`);
76
- return [];
88
+ return [new types_1.Violation(0, '', `Rule '${rule.name}' crashed: ${error.message}`)];
77
89
  }
78
90
  }
79
91
  function runBashRules(rules, bashContext, config) {
@@ -85,7 +97,7 @@ function runBashRules(rules, bashContext, config) {
85
97
  if (!ruleConfig || ruleConfig.isOff)
86
98
  continue;
87
99
  bashContext.options = mergeOptions(rule.defaultOptions, ruleConfig);
88
- const vs = safeCheckBash(rule, bashContext);
100
+ const vs = runRuleCheck(rule, bashContext);
89
101
  if (vs.length > 0) {
90
102
  groups.push(new types_1.RuleGroup(rule.name, rule.description, [...rule.fixHint], [...vs]));
91
103
  }
@@ -112,28 +124,6 @@ function mergeOptions(defaultOptions, ruleConfig) {
112
124
  }
113
125
  return out;
114
126
  }
115
- function safeCheckEdit(rule, ctx) {
116
- // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
117
- try {
118
- return rule.check(ctx);
119
- }
120
- catch (err) {
121
- const error = (0, to_error_1.toError)(err);
122
- process.stderr.write(`[ai-hooks] rule ${rule.name} crashed: ${error.message}\n`);
123
- return [];
124
- }
125
- }
126
- function safeCheckFile(rule, ctx) {
127
- // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
128
- try {
129
- return rule.check(ctx);
130
- }
131
- catch (err) {
132
- const error = (0, to_error_1.toError)(err);
133
- process.stderr.write(`[ai-hooks] rule ${rule.name} crashed: ${error.message}\n`);
134
- return [];
135
- }
136
- }
137
127
  function runEditRules(rules, editContexts, config) {
138
128
  const groups = [];
139
129
  for (const rule of rules) {
@@ -147,7 +137,7 @@ function runEditRules(rules, editContexts, config) {
147
137
  if (!ruleMatchesFile(rule, ctx.relativePath))
148
138
  continue;
149
139
  ctx.options = mergeOptions(rule.defaultOptions, ruleConfig);
150
- const vs = safeCheckEdit(rule, ctx);
140
+ const vs = runRuleCheck(rule, ctx);
151
141
  for (const v of vs) {
152
142
  const copy = new types_1.Violation(v.line, v.snippet, v.message);
153
143
  copy.editIndex = ctx.editIndex;
@@ -172,7 +162,7 @@ function runFileRules(rules, fileContext, config) {
172
162
  if (!ruleMatchesFile(rule, fileContext.relativePath))
173
163
  continue;
174
164
  fileContext.options = mergeOptions(rule.defaultOptions, ruleConfig);
175
- const vs = safeCheckFile(rule, fileContext);
165
+ const vs = runRuleCheck(rule, fileContext);
176
166
  if (vs.length > 0) {
177
167
  groups.push(new types_1.RuleGroup(rule.name, rule.description, [...rule.fixHint], [...vs]));
178
168
  }
@@ -1 +1 @@
1
- {"version":3,"file":"runner.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/runner.ts"],"names":[],"mappings":";;AAcA,kBAaC;AA2BD,0BASC;;AA/DD,mDAA6B;AAE7B,mDAAkE;AAClE,+CAA2C;AAC3C,6CAAsD;AACtD,yCAAqC;AACrC,qCAAwC;AACxC,mCAKiB;AAEjB,SAAgB,GAAG,CACf,QAAkB,EAClB,KAA0B,EAC1B,GAAW;IAEX,8DAA8D;IAC9D,IAAI,CAAC;QACD,OAAO,WAAW,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IAC7C,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;QAC3B,OAAO,CAAC,KAAK,CAAC,6CAA6C,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAC5E,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC;AAED,SAAS,WAAW,CAChB,QAAkB,EAClB,KAA0B,EAC1B,GAAW;IAEX,MAAM,MAAM,GAAG,IAAA,wBAAU,EAAC,GAAG,CAAC,CAAC;IAC/B,IAAI,CAAC,MAAM,CAAC,UAAU;QAAE,OAAO,IAAI,CAAC;IAEpC,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACtD,MAAM,KAAK,GAAG,IAAA,sBAAS,EAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC/C,IAAI,KAAK,CAAC,MAAM,KAAK,CA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WAAW,CAAC,YAAY,CAAC;YAAE,SAAS;QAC/D,WAAW,CAAC,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;QACpE,MAAM,EAAE,GAAG,aAAa,CAAC,IAAgB,EAAE,WAAW,CAAC,CAAC;QACxD,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChB,MAAM,CAAC,IAAI,CAAC,IAAI,iBAAS,CACrB,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAC1D,CAAC,CAAC;QACP,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import * as path from 'path';\n\nimport { buildContexts, buildBashContext } from './build-context';\nimport { loadConfig } from './load-config';\nimport { loadRules, globMatches } from './load-rules';\nimport { toError } from './to-error';\nimport { formatReport } from './report';\nimport {\n ToolKind, NormalizedToolInput, BlockedResult,\n Rule, EditRule, FileRule, BashRule, Violation, RuleGroup,\n EditContext, FileContext, BashContext,\n ResolvedConfig, ResolvedRuleConfig, RuleOptions,\n} from './types';\n\nexport function run(\n toolKind: ToolKind,\n input: NormalizedToolInput,\n cwd: string,\n): BlockedResult | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return runInternal(toolKind, input, cwd);\n } catch (err: unknown) {\n const error = toError(err);\n console.error(`[ai-hooks] runner crashed (failing open): ${error.message}`);\n return null;\n }\n}\n\nfunction runInternal(\n toolKind: ToolKind,\n input: NormalizedToolInput,\n cwd: string,\n): BlockedResult | null {\n const config = loadConfig(cwd);\n if (!config.configPath) return null;\n\n const workspaceRoot = path.dirname(config.configPath);\n const rules = loadRules(config, workspaceRoot);\n if (rules.length === 0) return null;\n\n const contexts = buildContexts(toolKind, input, workspaceRoot);\n const relativePath = path.relative(workspaceRoot, input.filePath);\n\n const editGroups = runEditRules(rules, contexts.editContexts, config);\n const fileGroups = runFileRules(rules, contexts.fileContext, config);\n const allGroups = [...editGroups, ...fileGroups];\n\n if (allGroups.length === 0) return null;\n\n const report = formatReport(relativePath, allGroups);\n return new BlockedResult(report);\n}\n\nexport function runBash(command: string, cwd: string): BlockedResult | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return runBashInternal(command, cwd);\n } catch (err: unknown) {\n const error = toError(err);\n console.error(`[ai-hooks] bash runner crashed (failing open): ${error.message}`);\n return null;\n }\n}\n\nfunction runBashInternal(command: string, cwd: string): BlockedResult | null {\n const config = loadConfig(cwd);\n if (!config.configPath) return null;\n\n const workspaceRoot = path.dirname(config.configPath);\n const rules = loadRules(config, workspaceRoot);\n if (rules.length === 0) return null;\n\n const ctx = buildBashContext(command, workspaceRoot);\n const groups = runBashRules(rules, ctx, config);\n if (groups.length === 0) return null;\n\n const report = formatReport('<bash>', groups);\n return new BlockedResult(report);\n}\n\nfunction safeCheckBash(rule: BashRule, ctx: BashContext): readonly Violation[] {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return rule.check(ctx);\n } catch (err: unknown) {\n const error = toError(err);\n process.stderr.write(`[ai-hooks] rule ${rule.name} crashed: ${error.message}\\n`);\n return [];\n }\n}\n\nfunction runBashRules(\n rules: readonly Rule[],\n bashContext: BashContext,\n config: ResolvedConfig,\n): readonly RuleGroup[] {\n const groups: RuleGroup[] = [];\n for (const rule of rules) {\n if (rule.scope !== 'bash') continue;\n const ruleConfig = config.rules.get(rule.name);\n if (!ruleConfig || ruleConfig.isOff) continue;\n bashContext.options = mergeOptions(rule.defaultOptions, ruleConfig);\n const vs = safeCheckBash(rule as BashRule, bashContext);\n if (vs.length > 0) {\n groups.push(new RuleGroup(\n rule.name, rule.description, [...rule.fixHint], [...vs],\n ));\n }\n }\n return groups;\n}\n\nfunction ruleMatchesFile(rule: Rule, relativePath: string): boolean {\n for (const pattern of rule.files) {\n if (globMatches(pattern, relativePath)) return true;\n }\n return false;\n}\n\nfunction mergeOptions(defaultOptions: RuleOptions, ruleConfig: ResolvedRuleConfig): RuleOptions {\n // webpieces-disable no-any-unknown -- building an options bag from opaque RuleOptions\n const out: Record<string, unknown> = {};\n for (const key of Object.keys(defaultOptions)) out[key] = defaultOptions[key];\n for (const key of Object.keys(ruleConfig.options)) {\n // 'mode' is the framework-level on/off switch, not a rule option.\n if (key === 'mode') continue;\n out[key] = ruleConfig.options[key];\n }\n return out;\n}\n\nfunction safeCheckEdit(rule: EditRule, ctx: EditContext): readonly Violation[] {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return rule.check(ctx);\n } catch (err: unknown) {\n const error = toError(err);\n process.stderr.write(`[ai-hooks] rule ${rule.name} crashed: ${error.message}\\n`);\n return [];\n }\n}\n\nfunction safeCheckFile(rule: FileRule, ctx: FileContext): readonly Violation[] {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return rule.check(ctx);\n } catch (err: unknown) {\n const error = toError(err);\n process.stderr.write(`[ai-hooks] rule ${rule.name} crashed: ${error.message}\\n`);\n return [];\n }\n}\n\nfunction runEditRules(\n rules: readonly Rule[],\n editContexts: readonly EditContext[],\n config: ResolvedConfig,\n): readonly RuleGroup[] {\n const groups: RuleGroup[] = [];\n for (const rule of rules) {\n if (rule.scope !== 'edit') continue;\n const ruleConfig = config.rules.get(rule.name);\n if (!ruleConfig || ruleConfig.isOff) continue;\n const allViolations: Violation[] = [];\n for (const ctx of editContexts) {\n if (!ruleMatchesFile(rule, ctx.relativePath)) continue;\n ctx.options = mergeOptions(rule.defaultOptions, ruleConfig);\n const vs = safeCheckEdit(rule as EditRule, ctx);\n for (const v of vs) {\n const copy = new Violation(v.line, v.snippet, v.message);\n copy.editIndex = ctx.editIndex;\n copy.editCount = ctx.editCount;\n allViolations.push(copy);\n }\n }\n if (allViolations.length > 0) {\n groups.push(new RuleGroup(\n rule.name, rule.description, [...rule.fixHint], allViolations,\n ));\n }\n }\n return groups;\n}\n\nfunction runFileRules(\n rules: readonly Rule[],\n fileContext: FileContext,\n config: ResolvedConfig,\n): readonly RuleGroup[] {\n const groups: RuleGroup[] = [];\n for (const rule of rules) {\n if (rule.scope !== 'file') continue;\n const ruleConfig = config.rules.get(rule.name);\n if (!ruleConfig || ruleConfig.isOff) continue;\n if (!ruleMatchesFile(rule, fileContext.relativePath)) continue;\n fileContext.options = mergeOptions(rule.defaultOptions, ruleConfig);\n const vs = safeCheckFile(rule as FileRule, fileContext);\n if (vs.length > 0) {\n groups.push(new RuleGroup(\n rule.name, rule.description, [...rule.fixHint], [...vs],\n ));\n }\n }\n return groups;\n}\n"]}
1
+ {"version":3,"file":"runner.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/runner.ts"],"names":[],"mappings":";;AAcA,kBAMC;AAoCD,0BAEC;;AA1DD,mDAA6B;AAE7B,mDAAkE;AAClE,+CAA2C;AAC3C,6CAAsD;AACtD,yCAAqC;AACrC,qCAAwC;AACxC,mCAKiB;AAEjB,SAAgB,GAAG,CACf,QAAkB,EAClB,KAA0B,EAC1B,GAAW;IAEX,OAAO,WAAW,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,WAAW,CAChB,QAAkB,EAClB,KAA0B,EAC1B,GAAW;IAEX,MAAM,MAAM,GAAG,IAAA,wBAAU,EAAC,GAAG,CAAC,CAAC;IAC/B,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACrB,OAAO,IAAI,qBAAa,CACpB,oCAAoC;YACpC,wGAAwG;YACxG,+CAA+C,CAClD,CAAC;IACN,CAAC;IAED,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACtD,MAAM,KAAK,GAAG,IAAA,sBAAS,EAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC/C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QA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UAAU,CAAC,CAAC;QACpE,MAAM,EAAE,GAAG,YAAY,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;QAC3C,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChB,MAAM,CAAC,IAAI,CAAC,IAAI,iBAAS,CACrB,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAC1D,CAAC,CAAC;QACP,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import * as path from 'path';\n\nimport { buildContexts, buildBashContext } from './build-context';\nimport { loadConfig } from './load-config';\nimport { loadRules, globMatches } from './load-rules';\nimport { toError } from './to-error';\nimport { formatReport } from './report';\nimport {\n ToolKind, NormalizedToolInput, BlockedResult,\n Rule, EditRule, FileRule, BashRule, Violation, RuleGroup,\n EditContext, FileContext, BashContext,\n ResolvedConfig, ResolvedRuleConfig, RuleOptions,\n} from './types';\n\nexport function run(\n toolKind: ToolKind,\n input: NormalizedToolInput,\n cwd: string,\n): BlockedResult | null {\n return runInternal(toolKind, input, cwd);\n}\n\nfunction runInternal(\n toolKind: ToolKind,\n input: NormalizedToolInput,\n cwd: string,\n): BlockedResult | null {\n const config = loadConfig(cwd);\n if (!config.configPath) {\n return new BlockedResult(\n 'webpieces.config.json not found.\\n' +\n 'Tell the human: run `./node_modules/.bin/wp-setup-ai-hooks` to initialize the project configuration.\\n' +\n 'Do not proceed until the human has done this.',\n );\n }\n\n const workspaceRoot = path.dirname(config.configPath);\n const rules = loadRules(config, workspaceRoot);\n if (rules.length === 0) return null;\n\n const outOfSync = checkConfigSync(rules, config);\n if (outOfSync) return outOfSync;\n\n const contexts = buildContexts(toolKind, input, workspaceRoot);\n const relativePath = path.relative(workspaceRoot, input.filePath);\n\n const editGroups = runEditRules(rules, contexts.editContexts, config);\n const fileGroups = runFileRules(rules, contexts.fileContext, config);\n const allGroups = [...editGroups, ...fileGroups];\n\n if (allGroups.length === 0) return null;\n\n const report = formatReport(relativePath, allGroups);\n return new BlockedResult(report);\n}\n\nexport function runBash(command: string, cwd: string): BlockedResult | null {\n return runBashInternal(command, cwd);\n}\n\nfunction runBashInternal(command: string, cwd: string): BlockedResult | null {\n const config = loadConfig(cwd);\n if (!config.configPath) {\n return new BlockedResult(\n 'webpieces.config.json not found.\\n' +\n 'Tell the human: run `./node_modules/.bin/wp-setup-ai-hooks` to initialize the project configuration.\\n' +\n 'Do not proceed until the human has done this.',\n );\n }\n\n const workspaceRoot = path.dirname(config.configPath);\n const rules = loadRules(config, workspaceRoot);\n if (rules.length === 0) return null;\n\n const outOfSync = checkConfigSync(rules, config);\n if (outOfSync) return outOfSync;\n\n const ctx = buildBashContext(command, workspaceRoot);\n const groups = runBashRules(rules, ctx, config);\n if (groups.length === 0) return null;\n\n const report = formatReport('<bash>', groups);\n return new BlockedResult(report);\n}\n\nfunction checkConfigSync(rules: readonly Rule[], config: ResolvedConfig): BlockedResult | null {\n const unconfigured = rules.filter((r: Rule) => !config.rules.has(r.name)).map((r: Rule) => r.name);\n if (unconfigured.length === 0) return null;\n\n const lines = [\n 'webpieces.config.json is out of sync — new rules have been added that are not yet configured.',\n '',\n `Unconfigured rules: ${unconfigured.join(', ')}`,\n '',\n 'Ask the human: for each rule above, do you want it ON or OFF (and any specific options)?',\n 'Then run `./node_modules/.bin/wp-setup-ai-hooks --sync` to add missing rules (defaults to OFF),',\n 'or edit webpieces.config.json manually to add an entry for each rule.',\n 'Do not proceed until webpieces.config.json is updated.',\n ];\n return new BlockedResult(lines.join('\\n'));\n}\n\n// N-legs pattern: each rule runs independently; crash → visible violation so AI sees it, not silent []\nfunction runRuleCheck(rule: Rule, ctx: EditContext | FileContext | BashContext): readonly Violation[] {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return (rule as EditRule | FileRule | BashRule).check(ctx as never);\n } catch (err: unknown) {\n const error = toError(err);\n return [new Violation(0, '', `Rule '${rule.name}' crashed: ${error.message}`)];\n }\n}\n\nfunction runBashRules(\n rules: readonly Rule[],\n bashContext: BashContext,\n config: ResolvedConfig,\n): readonly RuleGroup[] {\n const groups: RuleGroup[] = [];\n for (const rule of rules) {\n if (rule.scope !== 'bash') continue;\n const ruleConfig = config.rules.get(rule.name);\n if (!ruleConfig || ruleConfig.isOff) continue;\n bashContext.options = mergeOptions(rule.defaultOptions, ruleConfig);\n const vs = runRuleCheck(rule, bashContext);\n if (vs.length > 0) {\n groups.push(new RuleGroup(\n rule.name, rule.description, [...rule.fixHint], [...vs],\n ));\n }\n }\n return groups;\n}\n\nfunction ruleMatchesFile(rule: Rule, relativePath: string): boolean {\n for (const pattern of rule.files) {\n if (globMatches(pattern, relativePath)) return true;\n }\n return false;\n}\n\nfunction mergeOptions(defaultOptions: RuleOptions, ruleConfig: ResolvedRuleConfig): RuleOptions {\n // webpieces-disable no-any-unknown -- building an options bag from opaque RuleOptions\n const out: Record<string, unknown> = {};\n for (const key of Object.keys(defaultOptions)) out[key] = defaultOptions[key];\n for (const key of Object.keys(ruleConfig.options)) {\n // 'mode' is the framework-level on/off switch, not a rule option.\n if (key === 'mode') continue;\n out[key] = ruleConfig.options[key];\n }\n return out;\n}\n\nfunction runEditRules(\n rules: readonly Rule[],\n editContexts: readonly EditContext[],\n config: ResolvedConfig,\n): readonly RuleGroup[] {\n const groups: RuleGroup[] = [];\n for (const rule of rules) {\n if (rule.scope !== 'edit') continue;\n const ruleConfig = config.rules.get(rule.name);\n if (!ruleConfig || ruleConfig.isOff) continue;\n const allViolations: Violation[] = [];\n for (const ctx of editContexts) {\n if (!ruleMatchesFile(rule, ctx.relativePath)) continue;\n ctx.options = mergeOptions(rule.defaultOptions, ruleConfig);\n const vs = runRuleCheck(rule, ctx);\n for (const v of vs) {\n const copy = new Violation(v.line, v.snippet, v.message);\n copy.editIndex = ctx.editIndex;\n copy.editCount = ctx.editCount;\n allViolations.push(copy);\n }\n }\n if (allViolations.length > 0) {\n groups.push(new RuleGroup(\n rule.name, rule.description, [...rule.fixHint], allViolations,\n ));\n }\n }\n return groups;\n}\n\nfunction runFileRules(\n rules: readonly Rule[],\n fileContext: FileContext,\n config: ResolvedConfig,\n): readonly RuleGroup[] {\n const groups: RuleGroup[] = [];\n for (const rule of rules) {\n if (rule.scope !== 'file') continue;\n const ruleConfig = config.rules.get(rule.name);\n if (!ruleConfig || ruleConfig.isOff) continue;\n if (!ruleMatchesFile(rule, fileContext.relativePath)) continue;\n fileContext.options = mergeOptions(rule.defaultOptions, ruleConfig);\n const vs = runRuleCheck(rule, fileContext);\n if (vs.length > 0) {\n groups.push(new RuleGroup(\n rule.name, rule.description, [...rule.fixHint], [...vs],\n ));\n }\n }\n return groups;\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { RuleOptions } from '@webpieces/rules-config';
2
- export { ResolvedConfig, ResolvedRuleConfig, RuleOptions } from '@webpieces/rules-config';
2
+ export { ResolvedConfig, ResolvedRuleConfig, RuleOptions, InformAiError } from '@webpieces/rules-config';
3
3
  export type ToolKind = 'Write' | 'Edit' | 'MultiEdit';
4
4
  export type RuleScope = 'edit' | 'file' | 'bash';
5
5
  export type IsLineDisabled = (lineNum: number, ruleName: string) => boolean;
package/src/core/types.js CHANGED
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BlockedResult = exports.RuleGroup = exports.FileContext = exports.BashContext = exports.EditContext = exports.NormalizedBashInput = exports.NormalizedToolInput = exports.NormalizedEdit = exports.Violation = exports.ResolvedRuleConfig = exports.ResolvedConfig = void 0;
3
+ exports.BlockedResult = exports.RuleGroup = exports.FileContext = exports.BashContext = exports.EditContext = exports.NormalizedBashInput = exports.NormalizedToolInput = exports.NormalizedEdit = exports.Violation = exports.InformAiError = exports.ResolvedRuleConfig = exports.ResolvedConfig = void 0;
4
4
  var rules_config_1 = require("@webpieces/rules-config");
5
5
  Object.defineProperty(exports, "ResolvedConfig", { enumerable: true, get: function () { return rules_config_1.ResolvedConfig; } });
6
6
  Object.defineProperty(exports, "ResolvedRuleConfig", { enumerable: true, get: function () { return rules_config_1.ResolvedRuleConfig; } });
7
+ Object.defineProperty(exports, "InformAiError", { enumerable: true, get: function () { return rules_config_1.InformAiError; } });
7
8
  class Violation {
8
9
  constructor(line, snippet, message) {
9
10
  this.line = line;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/types.ts"],"names":[],"mappings":";;;AAGA,wDAA0F;AAAjF,8GAAA,cAAc,OAAA;AAAE,kHAAA,kBAAkB,OAAA;AAM3C,MAAa,SAAS;IAOlB,YAAY,IAAY,EAAE,OAAe,EAAE,OAAe;QACtD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;CACJ;AAdD,8BAcC;AAED,MAAa,cAAc;IAIvB,YAAY,SAAiB,EAAE,SAAiB;QAC5C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;CACJ;AARD,wCAQC;AAED,MAAa,mBAAmB;IAI5B,YAAY,QAAgB,EAAE,KAAgC;QAC1D,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;CACJ;AARD,kDAQC;AAED,MAAa,mBAAmB;IAG5B,YAAY,OAAe;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CACJ;AAND,kDAMC;AAED,MAAa,WAAW;IAepB,YACI,IAAc,EACd,SAAiB,EACjB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AI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;QAC7C,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;IACtB,CAAC;CACJ;AA/BD,kCA+BC;AA2BD,MAAa,SAAS;IAMlB,YACI,QAAgB,EAChB,eAAuB,EACvB,OAA0B,EAC1B,UAAgC;QAEhC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;CACJ;AAjBD,8BAiBC;AAED,MAAa,aAAa;IAGtB,YAAY,MAAc;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CACJ;AAND,sCAMC","sourcesContent":["// ResolvedConfig / ResolvedRuleConfig / RuleOptions now live in @webpieces/rules-config\n// so ai-hooks and the Nx validate-code executor share one loader and one config file.\nimport { RuleOptions } from '@webpieces/rules-config';\nexport { ResolvedConfig, ResolvedRuleConfig, RuleOptions } from '@webpieces/rules-config';\n\nexport type ToolKind = 'Write' | 'Edit' | 'MultiEdit';\nexport type RuleScope = 'edit' | 'file' | 'bash';\nexport type IsLineDisabled = (lineNum: number, ruleName: string) => boolean;\n\nexport class Violation {\n readonly line: number;\n readonly snippet: string;\n readonly message: string;\n editIndex: number | undefined;\n editCount: number | undefined;\n\n constructor(line: number, snippet: string, message: string) {\n this.line = line;\n this.snippet = snippet;\n this.message = message;\n this.editIndex = undefined;\n this.editCount = undefined;\n }\n}\n\nexport class NormalizedEdit {\n readonly oldString: string;\n readonly newString: string;\n\n constructor(oldString: string, newString: string) {\n this.oldString = oldString;\n this.newString = newString;\n }\n}\n\nexport class NormalizedToolInput {\n readonly filePath: string;\n readonly edits: readonly NormalizedEdit[];\n\n constructor(filePath: string, edits: readonly NormalizedEdit[]) {\n this.filePath = filePath;\n this.edits = edits;\n }\n}\n\nexport class NormalizedBashInput {\n readonly command: string;\n\n constructor(command: string) {\n this.command = command;\n }\n}\n\nexport class EditContext {\n readonly tool: ToolKind;\n readonly editIndex: number;\n readonly editCount: number;\n readonly filePath: string;\n readonly relativePath: string;\n readonly workspaceRoot: string;\n readonly addedContent: string;\n readonly strippedContent: string;\n readonly lines: readonly string[];\n readonly strippedLines: readonly string[];\n readonly removedContent: string;\n readonly isLineDisabled: IsLineDisabled;\n options: RuleOptions;\n\n constructor(\n tool: ToolKind,\n editIndex: number,\n editCount: number,\n filePath: string,\n relativePath: string,\n workspaceRoot: string,\n addedContent: string,\n strippedContent: string,\n lines: readonly string[],\n strippedLines: readonly string[],\n removedContent: string,\n isLineDisabled: IsLineDisabled,\n ) {\n this.tool = tool;\n this.editIndex = editIndex;\n this.editCount = editCount;\n this.filePath = filePath;\n this.relativePath = relativePath;\n this.workspaceRoot = workspaceRoot;\n this.addedContent = addedContent;\n this.strippedContent = strippedContent;\n this.lines = lines;\n this.strippedLines = strippedLines;\n this.removedContent = removedContent;\n this.isLineDisabled = isLineDisabled;\n this.options = {};\n }\n}\n\nexport class BashContext {\n readonly tool: 'Bash';\n readonly command: string;\n readonly workspaceRoot: string;\n options: RuleOptions;\n\n constructor(command: string, workspaceRoot: string) {\n this.tool = 'Bash';\n this.command = command;\n this.workspaceRoot = workspaceRoot;\n this.options = {};\n }\n}\n\nexport class FileContext {\n readonly tool: ToolKind;\n readonly filePath: string;\n readonly relativePath: string;\n readonly workspaceRoot: string;\n readonly currentFileLines: number;\n readonly linesAdded: number;\n readonly linesRemoved: number;\n readonly projectedFileLines: number;\n options: RuleOptions;\n\n constructor(\n tool: ToolKind,\n filePath: string,\n relativePath: string,\n workspaceRoot: string,\n currentFileLines: number,\n linesAdded: number,\n linesRemoved: number,\n projectedFileLines: number,\n ) {\n this.tool = tool;\n this.filePath = filePath;\n this.relativePath = relativePath;\n this.workspaceRoot = workspaceRoot;\n this.currentFileLines = currentFileLines;\n this.linesAdded = linesAdded;\n this.linesRemoved = linesRemoved;\n this.projectedFileLines = projectedFileLines;\n this.options = {};\n }\n}\n\ninterface RuleBase {\n readonly name: string;\n readonly description: string;\n readonly files: readonly string[];\n readonly defaultOptions: RuleOptions;\n readonly fixHint: readonly string[];\n}\n\nexport interface EditRule extends RuleBase {\n readonly scope: 'edit';\n check(ctx: EditContext): readonly Violation[];\n}\n\nexport interface FileRule extends RuleBase {\n readonly scope: 'file';\n check(ctx: FileContext): readonly Violation[];\n}\n\nexport interface BashRule extends RuleBase {\n readonly scope: 'bash';\n check(ctx: BashContext): readonly Violation[];\n}\n\nexport type Rule = EditRule | FileRule | BashRule;\n\nexport class RuleGroup {\n readonly ruleName: string;\n readonly ruleDescription: string;\n readonly fixHint: readonly string[];\n readonly violations: readonly Violation[];\n\n constructor(\n ruleName: string,\n ruleDescription: string,\n fixHint: readonly string[],\n violations: readonly Violation[],\n ) {\n this.ruleName = ruleName;\n this.ruleDescription = ruleDescription;\n this.fixHint = fixHint;\n this.violations = violations;\n }\n}\n\nexport class BlockedResult {\n readonly report: string;\n\n constructor(report: string) {\n this.report = report;\n }\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/types.ts"],"names":[],"mappings":";;;AAGA,wDAAyG;AAAhG,8GAAA,cAAc,OAAA;AAAE,kHAAA,kBAAkB,OAAA;AAAe,6GAAA,aAAa,OAAA;AAMvE,MAAa,SAAS;IAOlB,YAAY,IAAY,EAAE,OAAe,EAAE,OAAe;QACtD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;Q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tB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CACJ;AAND,sCAMC","sourcesContent":["// ResolvedConfig / ResolvedRuleConfig / RuleOptions now live in @webpieces/rules-config\n// so ai-hooks and the Nx validate-code executor share one loader and one config file.\nimport { RuleOptions } from '@webpieces/rules-config';\nexport { ResolvedConfig, ResolvedRuleConfig, RuleOptions, InformAiError } from '@webpieces/rules-config';\n\nexport type ToolKind = 'Write' | 'Edit' | 'MultiEdit';\nexport type RuleScope = 'edit' | 'file' | 'bash';\nexport type IsLineDisabled = (lineNum: number, ruleName: string) => boolean;\n\nexport class Violation {\n readonly line: number;\n readonly snippet: string;\n readonly message: string;\n editIndex: number | undefined;\n editCount: number | undefined;\n\n constructor(line: number, snippet: string, message: string) {\n this.line = line;\n this.snippet = snippet;\n this.message = message;\n this.editIndex = undefined;\n this.editCount = undefined;\n }\n}\n\nexport class NormalizedEdit {\n readonly oldString: string;\n readonly newString: string;\n\n constructor(oldString: string, newString: string) {\n this.oldString = oldString;\n this.newString = newString;\n }\n}\n\nexport class NormalizedToolInput {\n readonly filePath: string;\n readonly edits: readonly NormalizedEdit[];\n\n constructor(filePath: string, edits: readonly NormalizedEdit[]) {\n this.filePath = filePath;\n this.edits = edits;\n }\n}\n\nexport class NormalizedBashInput {\n readonly command: string;\n\n constructor(command: string) {\n this.command = command;\n }\n}\n\nexport class EditContext {\n readonly tool: ToolKind;\n readonly editIndex: number;\n readonly editCount: number;\n readonly filePath: string;\n readonly relativePath: string;\n readonly workspaceRoot: string;\n readonly addedContent: string;\n readonly strippedContent: string;\n readonly lines: readonly string[];\n readonly strippedLines: readonly string[];\n readonly removedContent: string;\n readonly isLineDisabled: IsLineDisabled;\n options: RuleOptions;\n\n constructor(\n tool: ToolKind,\n editIndex: number,\n editCount: number,\n filePath: string,\n relativePath: string,\n workspaceRoot: string,\n addedContent: string,\n strippedContent: string,\n lines: readonly string[],\n strippedLines: readonly string[],\n removedContent: string,\n isLineDisabled: IsLineDisabled,\n ) {\n this.tool = tool;\n this.editIndex = editIndex;\n this.editCount = editCount;\n this.filePath = filePath;\n this.relativePath = relativePath;\n this.workspaceRoot = workspaceRoot;\n this.addedContent = addedContent;\n this.strippedContent = strippedContent;\n this.lines = lines;\n this.strippedLines = strippedLines;\n this.removedContent = removedContent;\n this.isLineDisabled = isLineDisabled;\n this.options = {};\n }\n}\n\nexport class BashContext {\n readonly tool: 'Bash';\n readonly command: string;\n readonly workspaceRoot: string;\n options: RuleOptions;\n\n constructor(command: string, workspaceRoot: string) {\n this.tool = 'Bash';\n this.command = command;\n this.workspaceRoot = workspaceRoot;\n this.options = {};\n }\n}\n\nexport class FileContext {\n readonly tool: ToolKind;\n readonly filePath: string;\n readonly relativePath: string;\n readonly workspaceRoot: string;\n readonly currentFileLines: number;\n readonly linesAdded: number;\n readonly linesRemoved: number;\n readonly projectedFileLines: number;\n options: RuleOptions;\n\n constructor(\n tool: ToolKind,\n filePath: string,\n relativePath: string,\n workspaceRoot: string,\n currentFileLines: number,\n linesAdded: number,\n linesRemoved: number,\n projectedFileLines: number,\n ) {\n this.tool = tool;\n this.filePath = filePath;\n this.relativePath = relativePath;\n this.workspaceRoot = workspaceRoot;\n this.currentFileLines = currentFileLines;\n this.linesAdded = linesAdded;\n this.linesRemoved = linesRemoved;\n this.projectedFileLines = projectedFileLines;\n this.options = {};\n }\n}\n\ninterface RuleBase {\n readonly name: string;\n readonly description: string;\n readonly files: readonly string[];\n readonly defaultOptions: RuleOptions;\n readonly fixHint: readonly string[];\n}\n\nexport interface EditRule extends RuleBase {\n readonly scope: 'edit';\n check(ctx: EditContext): readonly Violation[];\n}\n\nexport interface FileRule extends RuleBase {\n readonly scope: 'file';\n check(ctx: FileContext): readonly Violation[];\n}\n\nexport interface BashRule extends RuleBase {\n readonly scope: 'bash';\n check(ctx: BashContext): readonly Violation[];\n}\n\nexport type Rule = EditRule | FileRule | BashRule;\n\nexport class RuleGroup {\n readonly ruleName: string;\n readonly ruleDescription: string;\n readonly fixHint: readonly string[];\n readonly violations: readonly Violation[];\n\n constructor(\n ruleName: string,\n ruleDescription: string,\n fixHint: readonly string[],\n violations: readonly Violation[],\n ) {\n this.ruleName = ruleName;\n this.ruleDescription = ruleDescription;\n this.fixHint = fixHint;\n this.violations = violations;\n }\n}\n\nexport class BlockedResult {\n readonly report: string;\n\n constructor(report: string) {\n this.report = report;\n }\n}\n\n"]}
@@ -6,7 +6,7 @@
6
6
  "hooks": [
7
7
  {
8
8
  "type": "command",
9
- "command": "node .webpieces/ai-hooks/claude-code-hook.js"
9
+ "command": "./node_modules/.bin/wp-ai-hook"
10
10
  }
11
11
  ]
12
12
  }
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env node
2
- // Postinstall shim — delegates to compiled TypeScript.
3
- // Must be plain JS because it runs during `pnpm install` BEFORE any build step.
4
- //
5
- // In workspace: compiled .js doesn't exist yet → silently exits (no-op).
6
- // In consumer: compiled .js exists in npm package → runs full setup.
7
- 'use strict';
8
-
9
- const path = require('path');
10
- const fs = require('fs');
11
-
12
- const compiled = path.join(__dirname, '..', 'src', 'bin', 'postinstall.js');
13
- if (fs.existsSync(compiled)) {
14
- require(compiled).main();
15
- }
@@ -1,106 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.main = main;
5
- const tslib_1 = require("tslib");
6
- const fs = tslib_1.__importStar(require("fs"));
7
- const path = tslib_1.__importStar(require("path"));
8
- const BRIDGE_CONTENT = `#!/usr/bin/env node\nrequire('@webpieces/ai-hook-rules/claude-code').main();\n`;
9
- function findProjectRoot() {
10
- // Walk up from this file's location to escape node_modules
11
- // e.g. /project/node_modules/@webpieces/ai-hook-rules/src/bin/postinstall.js -> /project
12
- let dir = __dirname;
13
- while (dir !== path.dirname(dir)) {
14
- dir = path.dirname(dir);
15
- const base = path.basename(dir);
16
- if (base === 'node_modules') {
17
- return path.dirname(dir);
18
- }
19
- }
20
- return null;
21
- }
22
- function createBridgeFile(projectRoot) {
23
- const hooksDir = path.join(projectRoot, '.webpieces', 'ai-hooks');
24
- const bridgePath = path.join(hooksDir, 'claude-code-hook.js');
25
- fs.mkdirSync(hooksDir, { recursive: true });
26
- fs.writeFileSync(bridgePath, BRIDGE_CONTENT);
27
- fs.chmodSync(bridgePath, 0o755);
28
- console.log(' [ai-hook-rules] Created .webpieces/ai-hooks/claude-code-hook.js');
29
- }
30
- function seedConfigIfMissing(projectRoot) {
31
- const configPath = path.join(projectRoot, 'webpieces.config.json');
32
- if (fs.existsSync(configPath))
33
- return;
34
- const templatePath = path.join(__dirname, '..', '..', 'templates', 'webpieces.config.seed.json');
35
- if (!fs.existsSync(templatePath))
36
- return;
37
- fs.copyFileSync(templatePath, configPath);
38
- console.log(' [ai-hook-rules] Created webpieces.config.json (default config)');
39
- }
40
- function settingsAlreadyHasHook(settingsPath) {
41
- if (!fs.existsSync(settingsPath))
42
- return false;
43
- const content = fs.readFileSync(settingsPath, 'utf8');
44
- return content.includes('claude-code-hook.js');
45
- }
46
- function loadTemplate() {
47
- const templatePath = path.join(__dirname, '..', '..', 'templates', 'claude-settings-hook.json');
48
- const raw = fs.readFileSync(templatePath, 'utf8');
49
- return JSON.parse(raw);
50
- }
51
- function mergeHookIntoSettings(settingsPath) {
52
- const template = loadTemplate();
53
- const hookEntry = template.hooks.PreToolUse[0];
54
- let settings = {};
55
- if (fs.existsSync(settingsPath)) {
56
- settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
57
- }
58
- if (!settings.hooks) {
59
- settings.hooks = {};
60
- }
61
- if (!Array.isArray(settings.hooks.PreToolUse)) {
62
- settings.hooks.PreToolUse = [];
63
- }
64
- settings.hooks.PreToolUse.push(hookEntry);
65
- fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4) + '\n');
66
- }
67
- function backupSettings(settingsPath) {
68
- if (fs.existsSync(settingsPath)) {
69
- const bakPath = settingsPath + '.bak';
70
- fs.copyFileSync(settingsPath, bakPath);
71
- console.log(' [ai-hook-rules] Backed up .claude/settings.json to .claude/settings.json.bak');
72
- }
73
- }
74
- function setupSettings(settingsPath) {
75
- // pnpm hides all postinstall output, so no point prompting or logging.
76
- // The user already consented by running `pnpm approve-builds`.
77
- backupSettings(settingsPath);
78
- mergeHookIntoSettings(settingsPath);
79
- }
80
- function main() {
81
- const projectRoot = findProjectRoot();
82
- if (!projectRoot) {
83
- // Not running from node_modules (maybe local dev / workspace) — skip
84
- return;
85
- }
86
- // 1. Always create the bridge file
87
- createBridgeFile(projectRoot);
88
- // 2. Seed config if missing
89
- seedConfigIfMissing(projectRoot);
90
- // 3. Check if .claude/ exists — if not, skip settings.json
91
- const claudeDir = path.join(projectRoot, '.claude');
92
- if (!fs.existsSync(claudeDir)) {
93
- return;
94
- }
95
- // 4. Check if settings.json already has the hook — if yes, done
96
- const settingsPath = path.join(claudeDir, 'settings.json');
97
- if (settingsAlreadyHasHook(settingsPath)) {
98
- return;
99
- }
100
- // 5. Backup and add the hook (user consented via pnpm approve-builds)
101
- setupSettings(settingsPath);
102
- }
103
- if (require.main === module) {
104
- main();
105
- }
106
- //# sourceMappingURL=postinstall.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"postinstall.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/bin/postinstall.ts"],"names":[],"mappings":";;;AA4GA,oBA2BC;;AAtID,+CAAyB;AACzB,mDAA6B;AAE7B,MAAM,cAAc,GAAG,gFAAgF,CAAC;AAExG,SAAS,eAAe;IACpB,2DAA2D;IAC3D,yFAAyF;IACzF,IAAI,GAAG,GAAG,SAAS,CAAC;IACpB,OAAO,GAAG,KAAK,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/B,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACxB,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,IAAI,KAAK,cAAc,EAAE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\nconst BRIDGE_CONTENT = `#!/usr/bin/env node\\nrequire('@webpieces/ai-hook-rules/claude-code').main();\\n`;\n\nfunction findProjectRoot(): string | null {\n // Walk up from this file's location to escape node_modules\n // e.g. /project/node_modules/@webpieces/ai-hook-rules/src/bin/postinstall.js -> /project\n let dir = __dirname;\n while (dir !== path.dirname(dir)) {\n dir = path.dirname(dir);\n const base = path.basename(dir);\n if (base === 'node_modules') {\n return path.dirname(dir);\n }\n }\n return null;\n}\n\nfunction createBridgeFile(projectRoot: string): void {\n const hooksDir = path.join(projectRoot, '.webpieces', 'ai-hooks');\n const bridgePath = path.join(hooksDir, 'claude-code-hook.js');\n\n fs.mkdirSync(hooksDir, { recursive: true });\n fs.writeFileSync(bridgePath, BRIDGE_CONTENT);\n fs.chmodSync(bridgePath, 0o755);\n console.log(' [ai-hook-rules] Created .webpieces/ai-hooks/claude-code-hook.js');\n}\n\nfunction seedConfigIfMissing(projectRoot: string): void {\n const configPath = path.join(projectRoot, 'webpieces.config.json');\n if (fs.existsSync(configPath)) return;\n\n const templatePath = path.join(__dirname, '..', '..', 'templates', 'webpieces.config.seed.json');\n if (!fs.existsSync(templatePath)) return;\n\n fs.copyFileSync(templatePath, configPath);\n console.log(' [ai-hook-rules] Created webpieces.config.json (default config)');\n}\n\nfunction settingsAlreadyHasHook(settingsPath: string): boolean {\n if (!fs.existsSync(settingsPath)) return false;\n\n const content = fs.readFileSync(settingsPath, 'utf8');\n return content.includes('claude-code-hook.js');\n}\n\ninterface HookEntry {\n matcher: string;\n hooks: Array<{ type: string; command: string }>;\n}\n\ninterface SettingsTemplate {\n hooks: {\n PreToolUse: HookEntry[];\n };\n}\n\ninterface ClaudeSettings {\n hooks?: {\n PreToolUse?: HookEntry[];\n };\n [key: string]: string | number | boolean | object | null | undefined;\n}\n\nfunction loadTemplate(): SettingsTemplate {\n const templatePath = path.join(__dirname, '..', '..', 'templates', 'claude-settings-hook.json');\n const raw = fs.readFileSync(templatePath, 'utf8');\n return JSON.parse(raw) as SettingsTemplate;\n}\n\nfunction mergeHookIntoSettings(settingsPath: string): void {\n const template = loadTemplate();\n const hookEntry = template.hooks.PreToolUse[0];\n\n let settings: ClaudeSettings = {};\n if (fs.existsSync(settingsPath)) {\n settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8')) as ClaudeSettings;\n }\n\n if (!settings.hooks) {\n settings.hooks = {};\n }\n if (!Array.isArray(settings.hooks.PreToolUse)) {\n settings.hooks.PreToolUse = [];\n }\n\n settings.hooks.PreToolUse.push(hookEntry);\n fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4) + '\\n');\n}\n\nfunction backupSettings(settingsPath: string): void {\n if (fs.existsSync(settingsPath)) {\n const bakPath = settingsPath + '.bak';\n fs.copyFileSync(settingsPath, bakPath);\n console.log(' [ai-hook-rules] Backed up .claude/settings.json to .claude/settings.json.bak');\n }\n}\n\n\nfunction setupSettings(settingsPath: string): void {\n // pnpm hides all postinstall output, so no point prompting or logging.\n // The user already consented by running `pnpm approve-builds`.\n backupSettings(settingsPath);\n mergeHookIntoSettings(settingsPath);\n}\n\nexport function main(): void {\n const projectRoot = findProjectRoot();\n if (!projectRoot) {\n // Not running from node_modules (maybe local dev / workspace) — skip\n return;\n }\n\n // 1. Always create the bridge file\n createBridgeFile(projectRoot);\n\n // 2. Seed config if missing\n seedConfigIfMissing(projectRoot);\n\n // 3. Check if .claude/ exists — if not, skip settings.json\n const claudeDir = path.join(projectRoot, '.claude');\n if (!fs.existsSync(claudeDir)) {\n return;\n }\n\n // 4. Check if settings.json already has the hook — if yes, done\n const settingsPath = path.join(claudeDir, 'settings.json');\n if (settingsAlreadyHasHook(settingsPath)) {\n return;\n }\n\n // 5. Backup and add the hook (user consented via pnpm approve-builds)\n setupSettings(settingsPath);\n}\n\nif (require.main === module) {\n main();\n}\n"]}