@lilmgenius/win-hooks 1.10.0

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.
@@ -0,0 +1,393 @@
1
+ #!/bin/bash
2
+ # win-hooks: codex-verify
3
+ # Post-patch health check for installed Codex plugin hooks.
4
+ # Detects: invalid JSON, BOM, CRLF, missing commandWindows wrappers,
5
+ # missing wrapper files, broken wrapper bodies, recursive wrappers, and
6
+ # bare Python hooks that may fail under Windows hook dispatch.
7
+
8
+ set -uo pipefail
9
+
10
+ FIX_MODE=false
11
+ [[ "${1:-}" == "--fix" ]] && FIX_MODE=true
12
+
13
+ ISSUES=$(mktemp)
14
+ trap 'rm -f "$ISSUES"' EXIT
15
+
16
+ add_issue() {
17
+ printf '%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" >> "$ISSUES"
18
+ printf '%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4"
19
+ }
20
+
21
+ if ! command -v codex >/dev/null 2>&1 || ! command -v node >/dev/null 2>&1; then
22
+ echo "win-hooks codex verify: skipped"
23
+ exit 0
24
+ fi
25
+
26
+ PLUGIN_LIST=$(codex plugin list --json 2>/dev/null) || { echo "win-hooks codex verify: skipped"; exit 0; }
27
+ export PLUGIN_LIST
28
+
29
+ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
30
+ PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
31
+ SCANNER="${SCRIPT_DIR}/codex-find-incompatible"
32
+ APPLIER="${SCRIPT_DIR}/codex-apply-patches"
33
+ TEMPLATE_CMD="${PLUGIN_ROOT}/hooks/run-hook.cmd"
34
+
35
+ LIB="${SCRIPT_DIR}/lib/plugins.sh"
36
+ if [[ ! -f "$LIB" ]]; then
37
+ echo "win-hooks: shared lib not found: $LIB" >&2
38
+ exit 1
39
+ fi
40
+ source "$LIB"
41
+
42
+ list_codex_plugins() {
43
+ node <<'JSEOF'
44
+ const fs = require('fs');
45
+ const path = require('path');
46
+ const list = JSON.parse(process.env.PLUGIN_LIST || '{}');
47
+ function normalize(p) {
48
+ return String(p || '').replace(/^\\\\\?\\/, '').replace(/\\/g, '/');
49
+ }
50
+ for (const plugin of list.installed || []) {
51
+ if (!plugin.installed || !plugin.enabled) continue;
52
+ const pluginId = plugin.pluginId || `${plugin.name || 'unknown'}@${plugin.marketplaceName || 'unknown'}`;
53
+ if (/^(win-hooks|codex-win-hooks)@/.test(pluginId) || plugin.name === 'win-hooks') continue;
54
+ const installPath = normalize(plugin.source && plugin.source.path);
55
+ if (!installPath) continue;
56
+ const manifest = path.join(installPath, '.codex-plugin', 'plugin.json');
57
+ if (!fs.existsSync(manifest)) continue;
58
+ let meta;
59
+ try {
60
+ meta = JSON.parse(fs.readFileSync(manifest, 'utf8').replace(/^\uFEFF/, ''));
61
+ } catch {
62
+ continue;
63
+ }
64
+ if (!meta.hooks) continue;
65
+ const hooksFile = normalize(path.resolve(installPath, meta.hooks));
66
+ console.log([pluginId, installPath, hooksFile].join('\t'));
67
+ }
68
+ JSEOF
69
+ }
70
+
71
+ extract_rel_path() {
72
+ wh_decode_json_command "$1" \
73
+ | sed -nE 's#.*"?\$(\{PLUGIN_ROOT\}|PLUGIN_ROOT)"?/([A-Za-z0-9_./-]+).*#\2#p' \
74
+ | head -1
75
+ }
76
+
77
+ wrapper_name_for() {
78
+ local cmd="$1" rel base name
79
+ rel=$(extract_rel_path "$cmd")
80
+ if [[ -n "$rel" ]]; then
81
+ base=$(basename "$rel")
82
+ base="${base%.*}"
83
+ printf '%s' "${base//_/-}" | sed 's/[^a-zA-Z0-9-]//g'
84
+ return
85
+ fi
86
+ name=$(wh_decode_json_command "$cmd" | awk '{for(i=1;i<=3&&i<=NF;i++){printf "%s",$i;if(i<3&&i<NF)printf "-"}}')
87
+ printf '%s' "$name" | sed 's/[^a-zA-Z0-9-]//g'
88
+ }
89
+
90
+ create_wrapper_body() {
91
+ local cmd="$1" rel interp shell_cmd
92
+ shell_cmd=$(wh_decode_json_command "$cmd")
93
+ rel=$(extract_rel_path "$cmd")
94
+ if [[ -z "$rel" ]]; then
95
+ printf '#!/bin/bash\n# win-hooks: recreated fallback wrapper\nexec bash "$@"\n'
96
+ return
97
+ fi
98
+
99
+ interp=$(printf '%s' "$shell_cmd" | awk '{print $1}' | sed "s/[\"'\\\\]//g")
100
+ wh_wrapper_exec_body "$rel" "$interp"
101
+ }
102
+
103
+ backup_command_for_wrapper() {
104
+ local bak="$1" wrapper="$2"
105
+ node - "$bak" "$wrapper" <<'JSEOF'
106
+ const fs = require('fs');
107
+ const path = require('path');
108
+ const file = process.argv[2];
109
+ const wanted = process.argv[3];
110
+ let data;
111
+ try {
112
+ data = JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
113
+ } catch {
114
+ process.exit(0);
115
+ }
116
+ function extractRel(cmd) {
117
+ const text = String(cmd || '').replace(/\\"/g, '"');
118
+ const match = text.match(/"?\$(?:\{PLUGIN_ROOT\}|PLUGIN_ROOT)"?\/([A-Za-z0-9_./-]+)/);
119
+ return match ? match[1] : '';
120
+ }
121
+ function wrapperName(cmd) {
122
+ const rel = extractRel(cmd);
123
+ if (rel) {
124
+ return path.basename(rel).replace(/\.[^.]*$/, '').replace(/_/g, '-').replace(/[^a-zA-Z0-9-]/g, '');
125
+ }
126
+ return String(cmd || '').replace(/\\"/g, '"').trim().split(/\s+/).slice(0, 3).join('-').replace(/[^a-zA-Z0-9-]/g, '');
127
+ }
128
+ for (const groups of Object.values(data.hooks || {})) {
129
+ if (!Array.isArray(groups)) continue;
130
+ for (const group of groups) {
131
+ const hooks = Array.isArray(group && group.hooks) ? group.hooks : [];
132
+ for (const hook of hooks) {
133
+ if (!hook || hook.type !== 'command') continue;
134
+ if (wrapperName(hook.command || '') === wanted) {
135
+ console.log(hook.command || '');
136
+ process.exit(0);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ JSEOF
142
+ }
143
+
144
+ recreate_missing_wrapper() {
145
+ local install_path="$1" hooks_file="$2" wrapper="$3" rest="$4"
146
+ local hooks_dir="${install_path}/_codex_hooks"
147
+ local target="${hooks_dir}/${wrapper}"
148
+ local bak="${hooks_file}.codex-win-hooks.bak" rawcmd=""
149
+
150
+ mkdir -p "$hooks_dir" 2>/dev/null || true
151
+
152
+ if printf '%s' "$rest" | grep -q 'PLUGIN_ROOT'; then
153
+ printf '#!/bin/bash\n# win-hooks: recreated; exec the real target passed by run-hook.cmd\nexec bash "$@"\n' > "$target"
154
+ echo " FIXED: recreated missing wrapper _codex_hooks/${wrapper} (passthrough)" >&2
155
+ return 0
156
+ fi
157
+
158
+ if [[ ! -f "$bak" ]]; then
159
+ echo " WARN: cannot recreate _codex_hooks/${wrapper}; no ${bak}" >&2
160
+ return 1
161
+ fi
162
+
163
+ rawcmd=$(backup_command_for_wrapper "$bak" "$wrapper" | head -1)
164
+
165
+ if [[ -z "$rawcmd" ]]; then
166
+ echo " WARN: cannot map _codex_hooks/${wrapper} to ${bak}" >&2
167
+ return 1
168
+ fi
169
+
170
+ create_wrapper_body "$rawcmd" > "$target"
171
+ echo " FIXED: recreated missing wrapper _codex_hooks/${wrapper} (from backup)" >&2
172
+ }
173
+
174
+ check_bom() {
175
+ local plugin="$1" install_path="$2" hooks_file="$3"
176
+ local hooks_parent f bom rel
177
+ hooks_parent="$(dirname "$hooks_file")"
178
+ {
179
+ for dir in "$hooks_parent" "${install_path}/_codex_hooks"; do
180
+ [[ -d "$dir" ]] || continue
181
+ find "$dir" -maxdepth 1 -type f 2>/dev/null
182
+ done
183
+ if [[ -f "$hooks_file" ]]; then
184
+ grep -oE '(\$\{PLUGIN_ROOT\}|\$PLUGIN_ROOT)/[A-Za-z0-9_./-]+' "$hooks_file" 2>/dev/null \
185
+ | sed -E "s#^(\\$\\{PLUGIN_ROOT\\}|\\$PLUGIN_ROOT)/#${install_path}/#"
186
+ fi
187
+ } | sort -u | while IFS= read -r f; do
188
+ [[ -z "$f" || ! -f "$f" ]] && continue
189
+ case "$f" in *.codex-win-hooks.bak|*.tmp) continue ;; esac
190
+ bom=$(od -A n -t x1 -N 3 "$f" 2>/dev/null | tr -d ' \n')
191
+ [[ "$bom" != "efbbbf" ]] && continue
192
+ rel="${f#$install_path/}"
193
+ add_issue "$plugin" "$install_path" "bom" "$rel"
194
+ if $FIX_MODE; then
195
+ tail -c +4 "$f" > "${f}.tmp" && mv "${f}.tmp" "$f"
196
+ echo " FIXED: stripped BOM from $rel" >&2
197
+ fi
198
+ done
199
+ }
200
+
201
+ check_crlf() {
202
+ local plugin="$1" install_path="$2" hooks_file="$3"
203
+ [[ -f "$hooks_file" ]] || return 0
204
+ if od -c "$hooks_file" 2>/dev/null | grep -q '\\r \\n'; then
205
+ add_issue "$plugin" "$install_path" "json_crlf" "CRLF line endings detected"
206
+ if $FIX_MODE; then
207
+ sed -i 's/\r$//' "$hooks_file" 2>/dev/null || {
208
+ sed 's/\r$//' "$hooks_file" > "${hooks_file}.tmp" && mv "${hooks_file}.tmp" "$hooks_file"
209
+ }
210
+ echo " FIXED: normalized CRLF in $hooks_file" >&2
211
+ fi
212
+ fi
213
+ }
214
+
215
+ check_json_valid() {
216
+ local plugin="$1" install_path="$2" hooks_file="$3"
217
+ local err
218
+ [[ -f "$hooks_file" ]] || return 1
219
+ err=$(wh_validate_json "$hooks_file" 2>&1) || {
220
+ add_issue "$plugin" "$install_path" "json_invalid" "$err"
221
+ return 1
222
+ }
223
+ return 0
224
+ }
225
+
226
+ check_recursive_wrapper() {
227
+ local plugin="$1" install_path="$2" hooks_file="$3"
228
+ local hooks_parent dir f first_line base
229
+ hooks_parent="$(dirname "$hooks_file")"
230
+ for dir in "$hooks_parent" "${install_path}/_codex_hooks"; do
231
+ [[ -d "$dir" ]] || continue
232
+ for f in "$dir"/*; do
233
+ [[ -f "$f" ]] || continue
234
+ [[ "$(basename "$f")" == "run-hook.cmd" ]] && continue
235
+ first_line=$(head -c 64 "$f" | tr -d '\r' | wh_strip_bom | head -1)
236
+ [[ "$first_line" != "#!/bin/bash"* && "$first_line" != "#!/usr/bin/env bash"* ]] && continue
237
+ base=$(basename "$f")
238
+ if grep -qE "(python3?|node)[[:space:]].*${base}" "$f" 2>/dev/null; then
239
+ add_issue "$plugin" "$install_path" "recursive_wrapper" "${base} calls interpreter on itself"
240
+ if $FIX_MODE; then
241
+ printf '#!/bin/bash\n# win-hooks: disabled; bash wrapper called interpreter on itself\nexit 0\n' > "$f"
242
+ echo " FIXED: disabled recursive wrapper ${base}" >&2
243
+ fi
244
+ fi
245
+ done
246
+ done
247
+ }
248
+
249
+ check_wrapper_bodies() {
250
+ local plugin="$1" install_path="$2" hooks_file="$3"
251
+ local dir="${install_path}/_codex_hooks"
252
+ local f base rel first_segment
253
+ [[ -d "$dir" ]] || return 0
254
+
255
+ for f in "$dir"/*; do
256
+ [[ -f "$f" ]] || continue
257
+ base=$(basename "$f")
258
+ [[ "$base" == "run-hook.cmd" ]] && continue
259
+
260
+ rel=$(sed -nE 's#.*\\"\$PLUGIN_ROOT\\"/([A-Za-z0-9_./-]+).*#\1#p' "$f" 2>/dev/null | head -1)
261
+ if [[ -n "$rel" ]]; then
262
+ add_issue "$plugin" "$install_path" "wrapper_broken" "_codex_hooks/${base} contains literal escaped quotes around PLUGIN_ROOT"
263
+ if $FIX_MODE; then
264
+ if [[ "$rel" == *.sh ]]; then
265
+ printf '#!/bin/bash\nSCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"\nPLUGIN_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"\nexec bash "$PLUGIN_ROOT/%s" "$@"\n' "$rel" > "$f"
266
+ else
267
+ printf '#!/bin/bash\nSCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"\nPLUGIN_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"\nexec "$PLUGIN_ROOT/%s" "$@"\n' "$rel" > "$f"
268
+ fi
269
+ echo " FIXED: repaired escaped-quote wrapper _codex_hooks/${base}" >&2
270
+ fi
271
+ continue
272
+ fi
273
+
274
+ rel=$(sed -nE 's#.*"\$PLUGIN_ROOT/([A-Za-z0-9_./-]+)".*#\1#p' "$f" 2>/dev/null | head -1)
275
+ first_segment="${rel%%/*}"
276
+ if [[ -n "$rel" ]] && { [[ "$first_segment" =~ ^(bash|sh|python3?|node|npx|npm)$ ]] || [[ ! -e "${install_path}/${rel}" ]]; }; then
277
+ add_issue "$plugin" "$install_path" "wrapper_broken" "_codex_hooks/${base} execs invalid \$PLUGIN_ROOT/${rel}"
278
+ if $FIX_MODE; then
279
+ recreate_missing_wrapper "$install_path" "$hooks_file" "$base" ""
280
+ fi
281
+ fi
282
+ done
283
+ }
284
+
285
+ check_python_stub() {
286
+ local plugin="$1" install_path="$2" hooks_file="$3"
287
+ local py
288
+ node - "$hooks_file" <<'JSEOF' | grep -q '^python$' || return 0
289
+ const fs = require('fs');
290
+ const file = process.argv[2];
291
+ let data;
292
+ try {
293
+ data = JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
294
+ } catch {
295
+ process.exit(0);
296
+ }
297
+ for (const groups of Object.values(data.hooks || {})) {
298
+ if (!Array.isArray(groups)) continue;
299
+ for (const group of groups) {
300
+ const hooks = Array.isArray(group && group.hooks) ? group.hooks : [];
301
+ for (const hook of hooks) {
302
+ if (!hook || hook.type !== 'command' || hook.commandWindows) continue;
303
+ if (/^python3?\s/.test(String(hook.command || '').trim())) {
304
+ console.log('python');
305
+ }
306
+ }
307
+ }
308
+ }
309
+ JSEOF
310
+ for py in python3 python py; do
311
+ command -v "$py" >/dev/null 2>&1 && "$py" -c "" >/dev/null 2>&1 && return 0
312
+ done
313
+ add_issue "$plugin" "$install_path" "python3_stub" "hook uses python but no working python3/python/py is available"
314
+ }
315
+
316
+ check_wrappers() {
317
+ local plugin="$1" install_path="$2" hooks_file="$3"
318
+ local hooks_dir="${install_path}/_codex_hooks"
319
+ local wrapper rest
320
+ node - "$hooks_file" <<'JSEOF' | while IFS=$'\t' read -r wrapper rest; do
321
+ const fs = require('fs');
322
+ const file = process.argv[2];
323
+ const data = JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
324
+ for (const groups of Object.values(data.hooks || {})) {
325
+ if (!Array.isArray(groups)) continue;
326
+ for (const group of groups) {
327
+ const hooks = Array.isArray(group && group.hooks) ? group.hooks : [];
328
+ for (const hook of hooks) {
329
+ const cw = String(hook && hook.commandWindows || '');
330
+ const normalized = cw.replace(/\\/g, '/');
331
+ if (!normalized.includes('_codex_hooks') || !/run-hook\.cmd/i.test(normalized)) continue;
332
+ const rest = normalized.replace(/^.*run-hook\.cmd["']?\s*/i, '');
333
+ const match = rest.match(/^["']?([^"'\s]+)/);
334
+ console.log([match ? match[1] : '', rest].join('\t'));
335
+ }
336
+ }
337
+ }
338
+ JSEOF
339
+ if [[ ! -f "${hooks_dir}/run-hook.cmd" ]]; then
340
+ add_issue "$plugin" "$install_path" "cmd_missing" "_codex_hooks/run-hook.cmd not found"
341
+ if $FIX_MODE && [[ -f "$TEMPLATE_CMD" ]]; then
342
+ mkdir -p "$hooks_dir" 2>/dev/null || true
343
+ cp "$TEMPLATE_CMD" "${hooks_dir}/run-hook.cmd"
344
+ echo " FIXED: restored _codex_hooks/run-hook.cmd" >&2
345
+ fi
346
+ fi
347
+ if [[ -z "$wrapper" ]]; then
348
+ add_issue "$plugin" "$install_path" "wrapper_missing" "commandWindows wrapper name not parseable"
349
+ continue
350
+ fi
351
+ if [[ ! -f "${hooks_dir}/${wrapper}" ]]; then
352
+ add_issue "$plugin" "$install_path" "wrapper_missing" "_codex_hooks/${wrapper} not found"
353
+ if $FIX_MODE; then
354
+ recreate_missing_wrapper "$install_path" "$hooks_file" "$wrapper" "$rest"
355
+ fi
356
+ fi
357
+ done
358
+ }
359
+
360
+ if [[ -f "$SCANNER" ]]; then
361
+ if $FIX_MODE && [[ -f "$APPLIER" && -f "$TEMPLATE_CMD" ]]; then
362
+ scan_out=$(bash "$SCANNER" 2>/dev/null || true)
363
+ if [[ -n "$scan_out" ]]; then
364
+ printf '%s\n' "$scan_out" | bash "$APPLIER" "$TEMPLATE_CMD" >&2 || true
365
+ fi
366
+ fi
367
+ while IFS=$'\t' read -r plugin install_path hooks_file event group_index hook_index cmd; do
368
+ [[ -z "${plugin:-}" ]] && continue
369
+ add_issue "$plugin" "$install_path" "incompatible" "${event}[${group_index}].hooks[${hook_index}] lacks commandWindows: ${cmd}"
370
+ done < <(bash "$SCANNER" 2>/dev/null || true)
371
+ fi
372
+
373
+ while IFS=$'\t' read -r plugin install_path hooks_file; do
374
+ [[ -z "${plugin:-}" ]] && continue
375
+ [[ -f "$hooks_file" ]] || continue
376
+
377
+ check_bom "$plugin" "$install_path" "$hooks_file"
378
+ check_crlf "$plugin" "$install_path" "$hooks_file"
379
+ check_recursive_wrapper "$plugin" "$install_path" "$hooks_file"
380
+ check_wrapper_bodies "$plugin" "$install_path" "$hooks_file"
381
+ check_python_stub "$plugin" "$install_path" "$hooks_file"
382
+ if check_json_valid "$plugin" "$install_path" "$hooks_file"; then
383
+ check_wrappers "$plugin" "$install_path" "$hooks_file"
384
+ fi
385
+ done < <(list_codex_plugins)
386
+
387
+ count=$(wc -l < "$ISSUES" | tr -d ' ')
388
+ if [[ "$count" == "0" ]]; then
389
+ echo "win-hooks codex verify: all Codex plugins healthy" >&2
390
+ exit 0
391
+ fi
392
+ echo "win-hooks codex verify: ${count} issue(s) found" >&2
393
+ exit "$(( count > 125 ? 125 : count ))"
@@ -0,0 +1,82 @@
1
+ #!/bin/bash
2
+ # win-hooks: find-incompatible
3
+ # Scans installed Claude Code plugins for hooks with Windows-incompatible commands.
4
+ # Outputs tab-separated lines: plugin\tinstall_path\thooks_file\tevent\tcommand
5
+ # Empty output means all plugins are compatible.
6
+ #
7
+ # The command field contains the value as it appears in the JSON file (with any
8
+ # JSON escaping like \" intact), so apply-patches can do exact string matching.
9
+
10
+ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
11
+ LIB="${SCRIPT_DIR}/lib/plugins.sh"
12
+ if [[ ! -f "$LIB" ]]; then
13
+ echo "win-hooks: shared lib not found: $LIB" >&2
14
+ exit 1
15
+ fi
16
+ source "$LIB"
17
+
18
+ INSTALLED="${HOME}/.claude/plugins/installed_plugins.json"
19
+ [[ ! -f "$INSTALLED" ]] && exit 0
20
+
21
+ # Check if a hook command is incompatible with Windows.
22
+ # Returns 0 (true) if incompatible, 1 (false) if compatible.
23
+ is_incompatible() {
24
+ local cmd="$1"
25
+ local shell_cmd
26
+ shell_cmd=$(printf '%s' "$cmd" | sed 's/\\"/"/g')
27
+ [[ -z "$cmd" ]] && return 1
28
+ # Already uses .cmd wrapper
29
+ [[ "$cmd" == *".cmd"* ]] && return 1
30
+ # Direct .sh call
31
+ [[ "$shell_cmd" == *".sh"* ]] && return 0
32
+ # CLAUDE_PLUGIN_ROOT path
33
+ if [[ "$shell_cmd" == *'${CLAUDE_PLUGIN_ROOT}'* || "$shell_cmd" == *'$CLAUDE_PLUGIN_ROOT'* ]]; then
34
+ local first_word
35
+ first_word=$(printf '%s' "$shell_cmd" | awk '{print $1}' | tr -d "\"'\\\\")
36
+ case "$first_word" in
37
+ # python3/python prefix: ALWAYS wrap on Windows. The bare interpreter may
38
+ # be a dead Microsoft Store stub, and even when it works in Git Bash the
39
+ # cmd.exe that dispatches the hook may resolve a different (stub) python.
40
+ # Routing through the bash wrapper, which execs a python resolved by a
41
+ # functional probe at patch time, normalizes this across all machines.
42
+ python3|python) return 0 ;;
43
+ node|npx|npm) return 1 ;;
44
+ esac
45
+ [[ "$shell_cmd" == *".py"* ]] && return 1
46
+ return 0
47
+ fi
48
+ # Bare command — check if binary exists on PATH
49
+ local first_word
50
+ first_word=$(printf '%s' "$shell_cmd" | awk '{print $1}')
51
+ if [[ "$first_word" != *"/"* && "$first_word" != *'$'* && "$first_word" != *"\\"* ]]; then
52
+ command -v "$first_word" &>/dev/null || return 0
53
+ fi
54
+ return 1
55
+ }
56
+
57
+ # Extract hook commands from a hooks.json file — output: event\tcommand
58
+ extract_commands() {
59
+ local hooks_file="$1"
60
+ local current_event=""
61
+ local re_event='"([A-Za-z]+)":[[:space:]]*\['
62
+ local re_cmd='"command":[[:space:]]*"(.*)"[,[:space:]]*$'
63
+
64
+ while IFS= read -r line; do
65
+ if [[ "$line" =~ $re_event ]]; then
66
+ [[ "${BASH_REMATCH[1]}" != "hooks" ]] && current_event="${BASH_REMATCH[1]}"
67
+ fi
68
+ if [[ "$line" =~ $re_cmd ]]; then
69
+ local cmd="${BASH_REMATCH[1]}"
70
+ [[ -n "$current_event" && -n "$cmd" ]] && printf '%s\t%s\n' "$current_event" "$cmd"
71
+ fi
72
+ done < <(wh_strip_bom < "$hooks_file")
73
+ }
74
+
75
+ # Main: scan all plugins
76
+ wh_each_plugin_hooksjson "$INSTALLED" | while IFS=$'\t' read -r plugin install_path hooks_file; do
77
+ extract_commands "$hooks_file" | while IFS=$'\t' read -r event cmd; do
78
+ if is_incompatible "$cmd"; then
79
+ printf '%s\t%s\t%s\t%s\t%s\n' "$plugin" "$install_path" "$hooks_file" "$event" "$cmd"
80
+ fi
81
+ done
82
+ done
@@ -0,0 +1,94 @@
1
+ #!/bin/bash
2
+ # win-hooks: fix-backslash-paths
3
+ # Converts Windows backslash paths (C:\...) to forward slashes (C:/...)
4
+ # in hook commands found in ~/.claude/settings.json.
5
+ #
6
+ # Fixes "MODULE_NOT_FOUND" errors caused by backslash path mangling
7
+ # during Claude Code's hook execution on Windows.
8
+ #
9
+ # Requires: node (guaranteed by Claude Code)
10
+ #
11
+ # Usage: fix-backslash-paths [--dry-run]
12
+ # --dry-run: show what would change without modifying files
13
+ #
14
+ # Output: tab-separated lines to stdout:
15
+ # event\told_command\tnew_command
16
+ #
17
+ # Exit code: 0 = success or nothing to fix, 1 = error
18
+
19
+ set -uo pipefail
20
+
21
+ DRY_RUN=false
22
+ [[ "${1:-}" == "--dry-run" ]] && DRY_RUN=true
23
+
24
+ SETTINGS="${HOME}/.claude/settings.json"
25
+ [[ ! -f "$SETTINGS" ]] && exit 0
26
+
27
+ if ! command -v node &>/dev/null; then
28
+ echo "win-hooks: node not found, cannot fix backslash paths" >&2
29
+ exit 1
30
+ fi
31
+
32
+ node - "$SETTINGS" "$DRY_RUN" << 'JSEOF'
33
+ const fs = require('fs');
34
+ const settingsPath = process.argv[2];
35
+ const dryRun = process.argv[3] === 'true';
36
+
37
+ const raw = fs.readFileSync(settingsPath, 'utf8').replace(/^\uFEFF/, '');
38
+ let data;
39
+ try {
40
+ data = JSON.parse(raw);
41
+ } catch (e) {
42
+ process.stderr.write('win-hooks: settings.json parse error: ' + e.message + '\n');
43
+ process.exit(1);
44
+ }
45
+
46
+ if (!data.hooks) process.exit(0);
47
+
48
+ const changes = [];
49
+
50
+ function fixPaths(obj, location) {
51
+ if (Array.isArray(obj)) {
52
+ obj.forEach((item, i) => fixPaths(item, location + '[' + i + ']'));
53
+ } else if (obj && typeof obj === 'object') {
54
+ if (typeof obj.command === 'string') {
55
+ // Match Windows absolute paths: C:\... pattern
56
+ // Replace backslashes with forward slashes within each matched path
57
+ const fixed = obj.command.replace(
58
+ /([A-Za-z]):\\([^\s"]*)/g,
59
+ (m) => m.replace(/\\/g, '/')
60
+ );
61
+ if (fixed !== obj.command) {
62
+ changes.push({ location, old: obj.command, fixed });
63
+ if (!dryRun) obj.command = fixed;
64
+ }
65
+ }
66
+ for (const [key, val] of Object.entries(obj)) {
67
+ fixPaths(val, location + '.' + key);
68
+ }
69
+ }
70
+ }
71
+
72
+ fixPaths(data.hooks, 'hooks');
73
+
74
+ if (changes.length === 0) {
75
+ process.exit(0);
76
+ }
77
+
78
+ // Report changes (tab-separated to stdout)
79
+ for (const c of changes) {
80
+ console.log(c.location + '\t' + c.old + '\t' + c.fixed);
81
+ }
82
+
83
+ if (!dryRun) {
84
+ // Backup (only first time)
85
+ const bakPath = settingsPath + '.winhooks.bak';
86
+ if (!fs.existsSync(bakPath)) {
87
+ fs.copyFileSync(settingsPath, bakPath);
88
+ }
89
+ fs.writeFileSync(settingsPath, JSON.stringify(data, null, 2) + '\n');
90
+ process.stderr.write('win-hooks: fixed ' + changes.length + ' backslash path(s) in settings.json\n');
91
+ } else {
92
+ process.stderr.write('win-hooks: would fix ' + changes.length + ' backslash path(s) (dry-run)\n');
93
+ }
94
+ JSEOF