townmouse 0.0.0-stage → 0.2.1

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.
Files changed (103) hide show
  1. package/CHANGELOG.md +126 -0
  2. package/DESIGN.md +169 -0
  3. package/LICENSE +661 -0
  4. package/README.md +368 -2
  5. package/SECURITY.md +57 -0
  6. package/TOWNMOUSE.md +91 -0
  7. package/assets/brand/mascot/README.md +23 -0
  8. package/assets/brand/mascot/townmouse-aristocrat.png +0 -0
  9. package/assets/brand/mascot/townmouse-icon.svg +1 -0
  10. package/assets/brand/mascot/townmouse-monocled.png +0 -0
  11. package/bin/townmouse +68 -0
  12. package/docs/PACKAGING.md +275 -0
  13. package/docs/browser-workspace.md +184 -0
  14. package/docs/panes.md +252 -0
  15. package/native/TownmouseApp.swift +108 -0
  16. package/package.json +89 -4
  17. package/public/apple-touch-icon.png +0 -0
  18. package/public/brand/floydslabs-credit.png +0 -0
  19. package/public/brand/floydslabs-credit.webp +0 -0
  20. package/public/brand/townmouse-aristocrat.webp +0 -0
  21. package/public/brand/townmouse-monocled.webp +0 -0
  22. package/public/clipboard-text.mjs +5 -0
  23. package/public/favicon-16x16.png +0 -0
  24. package/public/favicon-32x32.png +0 -0
  25. package/public/favicon.ico +0 -0
  26. package/public/favicon.svg +1 -0
  27. package/public/features/autosave.mjs +76 -0
  28. package/public/features/clipboard.mjs +69 -0
  29. package/public/features/command-palette.mjs +181 -0
  30. package/public/features/desktop-palette.mjs +19 -0
  31. package/public/features/export.mjs +37 -0
  32. package/public/features/file-drop.mjs +77 -0
  33. package/public/features/floating-palette.mjs +113 -0
  34. package/public/features/fontsize.mjs +72 -0
  35. package/public/features/haptic-keys.mjs +42 -0
  36. package/public/features/history-undo.mjs +102 -0
  37. package/public/features/keepalive.mjs +36 -0
  38. package/public/features/keybar-config.mjs +70 -0
  39. package/public/features/keyboard-shortcuts.mjs +74 -0
  40. package/public/features/links.mjs +139 -0
  41. package/public/features/multi-window-sync.mjs +74 -0
  42. package/public/features/osc-bridge.mjs +59 -0
  43. package/public/features/phone-landscape-focus.mjs +157 -0
  44. package/public/features/recent-commands.mjs +100 -0
  45. package/public/features/search.mjs +134 -0
  46. package/public/features/session-lock.mjs +96 -0
  47. package/public/features/session-manager.mjs +149 -0
  48. package/public/features/settings-sync.mjs +77 -0
  49. package/public/features/shortcut-help.mjs +85 -0
  50. package/public/features/smart-completions.mjs +101 -0
  51. package/public/features/smart-suggest.mjs +124 -0
  52. package/public/features/snippets.mjs +85 -0
  53. package/public/features/split-view.mjs +102 -0
  54. package/public/features/status-metrics.mjs +114 -0
  55. package/public/features/stylus-support.mjs +124 -0
  56. package/public/features/swipe-history.mjs +141 -0
  57. package/public/features/swipe-sessions.mjs +85 -0
  58. package/public/features/tab-reorder.mjs +62 -0
  59. package/public/features/theme-system.mjs +51 -0
  60. package/public/features/toast-status.mjs +118 -0
  61. package/public/features/two-finger-menu.mjs +87 -0
  62. package/public/features/voice-input.mjs +244 -0
  63. package/public/features/wake-fullscreen.mjs +67 -0
  64. package/public/features/window-title.mjs +40 -0
  65. package/public/features/zoom.mjs +80 -0
  66. package/public/icon-128.png +0 -0
  67. package/public/icon-180.png +0 -0
  68. package/public/icon-192.png +0 -0
  69. package/public/icon-48.png +0 -0
  70. package/public/icon-512-maskable.png +0 -0
  71. package/public/icon-512.png +0 -0
  72. package/public/icon.svg +1 -0
  73. package/public/index.html +1765 -0
  74. package/public/input-chunks.mjs +22 -0
  75. package/public/input-guard.mjs +210 -0
  76. package/public/manifest.json +56 -0
  77. package/public/panes.css +1257 -0
  78. package/public/panes.html +208 -0
  79. package/public/panes.mjs +2625 -0
  80. package/public/service-worker.js +67 -0
  81. package/public/workspace.css +1447 -0
  82. package/public/workspace.mjs +1475 -0
  83. package/scripts/install-service.sh +269 -0
  84. package/scripts/node-runtime.sh +138 -0
  85. package/scripts/panes +439 -0
  86. package/scripts/prepare-native.sh +35 -0
  87. package/scripts/run-hooks +69 -0
  88. package/scripts/t1.sh +113 -0
  89. package/scripts/uninstall-service.sh +49 -0
  90. package/src/agent-guide.js +260 -0
  91. package/src/attachments.js +128 -0
  92. package/src/browser-dom.js +545 -0
  93. package/src/browser-workspace.js +1246 -0
  94. package/src/chrome-resolve.js +80 -0
  95. package/src/pty-permissions.js +99 -0
  96. package/src/regex-worker.js +13 -0
  97. package/src/replay-text.js +189 -0
  98. package/src/request-boundary.js +58 -0
  99. package/src/safe-regex.js +87 -0
  100. package/src/server.js +1122 -0
  101. package/src/shell-resolve.js +42 -0
  102. package/src/static-deps.js +61 -0
  103. package/src/workspace-panes.js +1099 -0
@@ -0,0 +1,269 @@
1
+ #!/bin/bash
2
+ #
3
+ # Townmouse — install the always-on macOS LaunchAgent (launchd).
4
+ #
5
+ # EFFECT RADIUS (what this changes on your machine):
6
+ # - Writes ~/Library/LaunchAgents/com.floyd.terminalone.plist
7
+ # - Registers a per-user LaunchAgent that runs `node src/server.js` on PORT 11001
8
+ # - RunAtLoad (starts at login) + KeepAlive (auto-restart on crash)
9
+ # - KeepAlive is gated on PathState: it only runs while
10
+ # the installed checkout's `src/server.js` exists (i.e. the app volume is
11
+ # mounted), and auto-starts when the volume re-appears.
12
+ # - Logs to ~/Library/Logs/com.floyd.terminalone.{out,err}.log (INTERNAL disk;
13
+ # launchd cannot reliably create log files on the external volume -> EX_CONFIG).
14
+ # - Installs a self-contained `t1` binary on the INTERNAL disk (first writable of
15
+ # ~/.local/bin, /usr/local/bin, ~/bin) so `t1` works from any shell and does NOT
16
+ # dangle when the external volume unmounts (it mount-checks and errors clearly).
17
+ # - Creates ~/Applications/Townmouse.app (with brand icon, quarantine cleared) so
18
+ # it launches with no terminal: Spotlight (Cmd-Space), Launchpad, and the Dock.
19
+ # Fully reversible: scripts/uninstall-service.sh
20
+ #
21
+ set -euo pipefail
22
+
23
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
24
+ LABEL="com.floyd.terminalone"
25
+ APP_DIR="${TERMINALONE_APP_DIR:-$(cd "$SCRIPT_DIR/.." && pwd -P)}"
26
+ PORT="${PORT:-11001}"
27
+ NODE="${TERMINALONE_NODE:-$($APP_DIR/scripts/node-runtime.sh path 2>/dev/null || true)}"
28
+ PLIST_DIR="${TERMINALONE_PLIST_DIR:-$HOME/Library/LaunchAgents}"
29
+ PLIST="$PLIST_DIR/$LABEL.plist"
30
+ LOG_DIR="${TERMINALONE_SYSTEM_LOG_DIR:-$HOME/Library/Logs}"
31
+ DOMAIN="gui/$(id -u)"
32
+ APP_PARENT_OVERRIDE="${TERMINALONE_APPLICATIONS_DIR:-}"
33
+ T1_BIN_DIR_OVERRIDE="${TERMINALONE_BIN_DIR:-}"
34
+
35
+ [ -n "$NODE" ] || { echo "ERROR: Townmouse requires Node 22.x; run scripts/node-runtime.sh doctor"; exit 1; }
36
+ [ -f "$APP_DIR/src/server.js" ] || { echo "ERROR: $APP_DIR/src/server.js not found (is the volume mounted?)"; exit 1; }
37
+ # A package tarball can lose execute bits on helper scripts. Restore the ones
38
+ # this installer calls before it calls them.
39
+ chmod +x "$APP_DIR/scripts/run-hooks" "$APP_DIR/scripts/t1.sh" "$APP_DIR/scripts/node-runtime.sh" 2>/dev/null || true
40
+ if [ "${TERMINALONE_SKIP_RUNTIME_CHECK:-0}" != "1" ]; then
41
+ "$APP_DIR/scripts/node-runtime.sh" doctor >/dev/null || {
42
+ echo "Repairing Townmouse native modules for $($NODE -v)..."
43
+ "$APP_DIR/scripts/node-runtime.sh" repair
44
+ }
45
+ fi
46
+ "$APP_DIR/scripts/run-hooks" pre-install
47
+ mkdir -p "$PLIST_DIR" "$LOG_DIR"
48
+
49
+ # NOTE: no WorkingDirectory key — the server resolves all paths via __dirname, and a
50
+ # chdir into the external volume from the launchd context can itself trigger EX_CONFIG.
51
+ cat > "$PLIST" <<PLISTEOF
52
+ <?xml version="1.0" encoding="UTF-8"?>
53
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
54
+ <plist version="1.0">
55
+ <dict>
56
+ <key>Label</key><string>$LABEL</string>
57
+ <key>ProgramArguments</key>
58
+ <array>
59
+ <string>$NODE</string>
60
+ <string>$APP_DIR/src/server.js</string>
61
+ </array>
62
+ <key>EnvironmentVariables</key>
63
+ <dict>
64
+ <key>PORT</key><string>$PORT</string>
65
+ <key>TERMINALONE_TRUSTED_ORIGINS</key><string>${TERMINALONE_TRUSTED_ORIGINS:-}</string>
66
+ <key>PATH</key><string>$(dirname "$NODE"):/usr/bin:/bin:/usr/sbin:/sbin</string>
67
+ </dict>
68
+ <key>RunAtLoad</key><true/>
69
+ <key>KeepAlive</key>
70
+ <dict>
71
+ <key>PathState</key>
72
+ <dict>
73
+ <key>$APP_DIR/src/server.js</key><true/>
74
+ </dict>
75
+ </dict>
76
+ <key>StandardOutPath</key><string>$LOG_DIR/$LABEL.out.log</string>
77
+ <key>StandardErrorPath</key><string>$LOG_DIR/$LABEL.err.log</string>
78
+ <key>ProcessType</key><string>Background</string>
79
+ </dict>
80
+ </plist>
81
+ PLISTEOF
82
+
83
+ # (Re)load cleanly and IDEMPOTENTLY. bootout is async and KeepAlive/PathState can
84
+ # respawn the job mid-teardown, so bootstrap can race it (Input/output error 5).
85
+ # Wait for the label to actually disappear (up to ~5s). If it's still present after
86
+ # the wait, the service is truly still loaded and we reload via kickstart in place.
87
+ # If it disappeared, bootstrap should succeed — any failure is a real error.
88
+ launchctl bootout "$DOMAIN/$LABEL" 2>/dev/null || true
89
+ LABEL_STILL_PRESENT=false
90
+ for _ in $(seq 1 20); do
91
+ if launchctl print "$DOMAIN/$LABEL" >/dev/null 2>&1; then
92
+ LABEL_STILL_PRESENT=true
93
+ sleep 0.25
94
+ else
95
+ LABEL_STILL_PRESENT=false
96
+ break
97
+ fi
98
+ done
99
+ launchctl enable "$DOMAIN/$LABEL" 2>/dev/null || true
100
+ if $LABEL_STILL_PRESENT; then
101
+ # Label survived bootout + 5s wait — service is legitimately still loaded.
102
+ # bootstrap would fail because the label exists; reload via kickstart instead.
103
+ echo " note : service already loaded; reloading in place."
104
+ launchctl kickstart -k "$DOMAIN/$LABEL" || {
105
+ echo "ERROR: failed to kickstart existing service." >&2
106
+ exit 1
107
+ }
108
+ else
109
+ # Label gone after bootout — fresh bootstrap MUST succeed.
110
+ if ! launchctl bootstrap "$DOMAIN" "$PLIST"; then
111
+ echo "ERROR: launchctl bootstrap failed — service was not installed." >&2
112
+ exit 1
113
+ fi
114
+ launchctl kickstart -k "$DOMAIN/$LABEL" || true
115
+ fi
116
+
117
+ # --- Install `t1` as a REAL self-contained binary on the INTERNAL disk.
118
+ # Not a symlink into the external volume: a symlink would dangle ("no such file")
119
+ # the moment the app volume unmounts. This generated stub lives on the internal disk,
120
+ # bakes in the app path, checks the mount, and fails LOUDLY (stderr + GUI dialog)
121
+ # instead of cryptically when the volume is gone.
122
+ chmod +x "$APP_DIR/scripts/t1.sh" 2>/dev/null || true
123
+ # Pick a bin dir on the INTERNAL disk. macOS: / is a sealed snapshot but writable
124
+ # paths live on the Data volume (same physical internal container); we match that
125
+ # device and reject any /Volumes/* external mount so `t1` can't live on a disk that
126
+ # unmounts. Prefer dirs already on PATH.
127
+ INTERNAL_DEV="$(df /System/Volumes/Data 2>/dev/null | tail -1 | awk '{print $1}')"
128
+ [ -n "$INTERNAL_DEV" ] || INTERNAL_DEV="$(df / | tail -1 | awk '{print $1}')"
129
+ is_internal() { case "$1" in /Volumes/*) return 1;; esac; [ "$(df "$1" 2>/dev/null | tail -1 | awk '{print $1}')" = "$INTERNAL_DEV" ]; }
130
+ T1_BIN_DIR=""
131
+ if [ -n "$T1_BIN_DIR_OVERRIDE" ]; then
132
+ mkdir -p "$T1_BIN_DIR_OVERRIDE"
133
+ T1_BIN_DIR="$T1_BIN_DIR_OVERRIDE"
134
+ else
135
+ for d in "$HOME/.local/bin" /usr/local/bin "$HOME/bin"; do
136
+ if [ -d "$d" ] && [ -w "$d" ] && is_internal "$d"; then T1_BIN_DIR="$d"; break; fi
137
+ done
138
+ if [ -z "$T1_BIN_DIR" ]; then
139
+ # Nothing writable+internal+existing; create ~/bin on the internal disk.
140
+ if mkdir -p "$HOME/bin" 2>/dev/null && is_internal "$HOME/bin"; then
141
+ T1_BIN_DIR="$HOME/bin"
142
+ else
143
+ T1_BIN_DIR="$HOME/.local/bin"; mkdir -p "$T1_BIN_DIR"
144
+ echo " WARN : no internal-disk bin dir found; t1 placed on $T1_BIN_DIR (may be external)."
145
+ fi
146
+ fi
147
+ fi
148
+ T1_LINK="$T1_BIN_DIR/t1"
149
+ T1_TMP="$(mktemp "$T1_BIN_DIR/.t1.XXXXXX")"
150
+ cat > "$T1_TMP" <<T1EOF
151
+ #!/bin/bash
152
+ # Townmouse launcher — generated by install-service.sh. Self-contained; lives on
153
+ # the internal disk so it survives "$APP_DIR" unmounting. Regenerate via reinstall.
154
+ set -euo pipefail
155
+ T1_APP="$APP_DIR/scripts/t1.sh"
156
+ if [ ! -f "\$T1_APP" ]; then
157
+ MSG="Townmouse is unavailable: its volume is not mounted (expected at $APP_DIR). Reconnect the drive, then try again."
158
+ printf '\\033[31mt1: %s\\033[0m\\n' "\$MSG" >&2
159
+ # If launched without a terminal (Finder/Spotlight/Dock), surface a GUI dialog too.
160
+ if [ ! -t 2 ] && command -v osascript >/dev/null 2>&1; then
161
+ osascript -e "display dialog \"\$MSG\" with title \"Townmouse\" buttons {\"OK\"} default button \"OK\" with icon caution" >/dev/null 2>&1 || true
162
+ fi
163
+ exit 1
164
+ fi
165
+ case "\${1:-}" in
166
+ doctor) exec "$APP_DIR/scripts/node-runtime.sh" doctor ;;
167
+ repair) exec "$APP_DIR/scripts/node-runtime.sh" repair ;;
168
+ hooks) for d in "$APP_DIR/.terminal-hooks" "$HOME/.config/terminal-workspace/hooks/terminalone"; do [ ! -d "\$d" ] || find "\$d" -maxdepth 2 -type f; done | sort ; exit 0 ;;
169
+ esac
170
+ exec /bin/bash "\$T1_APP" "\$@"
171
+ T1EOF
172
+ chmod +x "$T1_TMP"
173
+ mv -f "$T1_TMP" "$T1_LINK"
174
+
175
+ # --- Make Townmouse launchable from anywhere WITHOUT a terminal:
176
+ # a tiny .app bundle -> shows in Spotlight, Launchpad, and Dock. Prefer /Applications
177
+ # (canonically Spotlight-indexed); fall back to ~/Applications if it's not writable.
178
+ if [ -n "$APP_PARENT_OVERRIDE" ]; then
179
+ APP_PARENT="$APP_PARENT_OVERRIDE"
180
+ mkdir -p "$APP_PARENT"
181
+ elif [ -w /Applications ]; then
182
+ APP_PARENT="/Applications"
183
+ else
184
+ APP_PARENT="$HOME/Applications"
185
+ mkdir -p "$APP_PARENT"
186
+ fi
187
+ APP_BUNDLE="$APP_PARENT/Townmouse.app"
188
+ mkdir -p "$APP_BUNDLE/Contents/MacOS" "$APP_BUNDLE/Contents/Resources"
189
+ cat > "$APP_BUNDLE/Contents/Info.plist" <<APPEOF
190
+ <?xml version="1.0" encoding="UTF-8"?>
191
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
192
+ <plist version="1.0">
193
+ <dict>
194
+ <key>CFBundleName</key><string>Townmouse</string>
195
+ <key>CFBundleDisplayName</key><string>Townmouse</string>
196
+ <key>CFBundleIdentifier</key><string>com.floyd.terminalone</string>
197
+ <key>CFBundleVersion</key><string>1.0</string>
198
+ <key>CFBundlePackageType</key><string>APPL</string>
199
+ <key>CFBundleExecutable</key><string>Townmouse</string>
200
+ <key>CFBundleIconFile</key><string>Townmouse</string>
201
+ <key>LSMinimumSystemVersion</key><string>13.0</string>
202
+ <key>NSHighResolutionCapable</key><true/>
203
+ </dict>
204
+ </plist>
205
+ APPEOF
206
+ if [ -n "${TERMINALONE_SWIFTC:-}" ]; then
207
+ [ -x "$TERMINALONE_SWIFTC" ] || { echo "ERROR: TERMINALONE_SWIFTC is not executable." >&2; exit 1; }
208
+ "$TERMINALONE_SWIFTC" -parse-as-library -O -framework AppKit -framework WebKit \
209
+ "$APP_DIR/native/TownmouseApp.swift" -o "$APP_BUNDLE/Contents/MacOS/Townmouse"
210
+ else
211
+ xcrun --find swiftc >/dev/null 2>&1 || { echo "ERROR: Swift compiler unavailable; install Apple Command Line Tools." >&2; exit 1; }
212
+ xcrun swiftc -parse-as-library -O -framework AppKit -framework WebKit \
213
+ "$APP_DIR/native/TownmouseApp.swift" -o "$APP_BUNDLE/Contents/MacOS/Townmouse"
214
+ fi
215
+ printf '%s\n' "$T1_LINK" > "$APP_BUNDLE/Contents/Resources/launcher-path.txt"
216
+ # Build the .icns from the brand icon so it shows in Spotlight/Launchpad/Dock.
217
+ RES_DIR="$APP_BUNDLE/Contents/Resources"
218
+ mkdir -p "$RES_DIR"
219
+ ICON_SRC="$APP_DIR/public/icon-512.png"
220
+ if [ -f "$ICON_SRC" ] && command -v iconutil >/dev/null 2>&1; then
221
+ ICONSET="$(mktemp -d)/Townmouse.iconset"
222
+ mkdir -p "$ICONSET"
223
+ for sz in 16 32 64 128 256 512; do
224
+ sips -z $sz $sz "$ICON_SRC" --out "$ICONSET/icon_${sz}x${sz}.png" >/dev/null 2>&1 || true
225
+ dbl=$((sz*2))
226
+ sips -z $dbl $dbl "$ICON_SRC" --out "$ICONSET/icon_${sz}x${sz}@2x.png" >/dev/null 2>&1 || true
227
+ done
228
+ iconutil -c icns "$ICONSET" -o "$RES_DIR/Townmouse.icns" 2>/dev/null || cp "$ICON_SRC" "$RES_DIR/Townmouse.png"
229
+ rm -rf "$(dirname "$ICONSET")"
230
+ fi
231
+ # Clear any quarantine flag so the locally-built bundle never triggers a Gatekeeper
232
+ # prompt on first Finder/Spotlight launch. Safe no-op if no flag is present.
233
+ xattr -dr com.apple.quarantine "$APP_BUNDLE" 2>/dev/null || true
234
+ # Register with Launch Services so Spotlight/Launchpad index it promptly.
235
+ /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$APP_BUNDLE" 2>/dev/null || true
236
+ "$APP_DIR/scripts/run-hooks" post-install --best-effort
237
+ # Ensure the chosen bin dir is actually on PATH. If not, add a marked line to the
238
+ # user's shell rc (idempotent — only once). Covers zsh (default) and bash.
239
+ T1_PATH_ADDED=""
240
+ case ":$PATH:" in
241
+ *":$T1_BIN_DIR:"*) : ;; # already on PATH this session
242
+ *)
243
+ MARK="# added by Townmouse install-service.sh"
244
+ LINE="export PATH=\"$T1_BIN_DIR:\$PATH\" $MARK"
245
+ for rc in "$HOME/.zshrc" "$HOME/.bash_profile"; do
246
+ if [ ! -e "$rc" ]; then
247
+ if [ "$rc" = "$HOME/.zshrc" ]; then touch "$rc"; else continue; fi
248
+ fi
249
+ if ! grep -qF "$MARK" "$rc" 2>/dev/null; then
250
+ printf '\n%s\n' "$LINE" >> "$rc" && T1_PATH_ADDED="$rc"
251
+ fi
252
+ done
253
+ ;;
254
+ esac
255
+
256
+ echo "Installed $LABEL"
257
+ echo " plist : $PLIST"
258
+ echo " node : $NODE"
259
+ echo " logs : $LOG_DIR/$LABEL.{out,err}.log and $LOG_DIR/Townmouse/local-launch.err.log"
260
+ echo " url : http://localhost:$PORT"
261
+ echo " t1 : ${T1_LINK:-(failed to link)}"
262
+ echo " app : $APP_BUNDLE (Spotlight: Cmd-Space -> Townmouse)"
263
+ echo " app : opens the UI only after health is confirmed; falls back to direct local startup if launchd is unavailable."
264
+ echo "Manage: launchctl print $DOMAIN/$LABEL | stop: scripts/uninstall-service.sh"
265
+ if [ -n "$T1_PATH_ADDED" ]; then
266
+ echo " PATH : added $T1_BIN_DIR to $T1_PATH_ADDED — run 'source $T1_PATH_ADDED' or open a new shell, then 't1'."
267
+ else
268
+ echo " PATH : $T1_BIN_DIR already on PATH — run 't1' now."
269
+ fi
@@ -0,0 +1,138 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+
4
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
5
+ EXPECTED_MAJOR="${TERMINALONE_NODE_MAJOR:-22}"
6
+ NATIVE_MODULES=(node-pty)
7
+
8
+ resolve_node() {
9
+ local candidate major fallback=""
10
+ for candidate in "${TERMINALONE_NODE:-}" "$(command -v node 2>/dev/null || true)" /usr/local/bin/node /opt/homebrew/bin/node; do
11
+ [ -n "$candidate" ] && [ -x "$candidate" ] || continue
12
+ major="$($candidate -p 'process.versions.node.split(".")[0]' 2>/dev/null || true)"
13
+ if [ "$major" = "$EXPECTED_MAJOR" ]; then
14
+ printf '%s\n' "$candidate"
15
+ return 0
16
+ fi
17
+ # node-pty is built on Node-API, so it loads across supported majors. Prefer
18
+ # the documented major, but do not refuse a machine that only has a newer
19
+ # LTS — the package's engines field already permits 22 through 26.
20
+ if [ -z "$fallback" ] && (cd "$ROOT" && "$candidate" -e 'require("node-pty")') >/dev/null 2>&1; then
21
+ fallback="$candidate"
22
+ fi
23
+ done
24
+ if [ -n "$fallback" ]; then
25
+ echo "Townmouse: Node $EXPECTED_MAJOR.x not found; using $("$fallback" -v)." >&2
26
+ printf '%s\n' "$fallback"
27
+ return 0
28
+ fi
29
+ echo "Townmouse requires Node $EXPECTED_MAJOR.x (or a newer release where node-pty loads). Set TERMINALONE_NODE to its absolute path." >&2
30
+ return 1
31
+ }
32
+
33
+ NODE_BIN="$(resolve_node)"
34
+ PATH="$(dirname "$NODE_BIN"):$PATH"
35
+ export PATH
36
+
37
+ # npm hoists dependencies for a normal install and nests them for a global one, so
38
+ # the package-local path is not where node-pty lives in either case. Ask Node.
39
+ resolve_node_pty_dir() {
40
+ (
41
+ cd "$ROOT" || exit 1
42
+ "$NODE_BIN" -e '
43
+ const path = require("path");
44
+ const fs = require("fs");
45
+ let dir = path.dirname(require.resolve("node-pty"));
46
+ while (dir !== path.dirname(dir) && !fs.existsSync(path.join(dir, "package.json"))) {
47
+ dir = path.dirname(dir);
48
+ }
49
+ process.stdout.write(dir);
50
+ ' 2>/dev/null
51
+ )
52
+ }
53
+
54
+ # Mark every macOS spawn helper executable. `npm pack` does not preserve the
55
+ # execute bit, and a non-executable helper makes spawn() fail with
56
+ # "posix_spawnp failed" while the rest of the app looks healthy.
57
+ fix_spawn_helpers() {
58
+ local pty_dir="$1" helper found=false
59
+ [ -n "$pty_dir" ] && [ -d "$pty_dir" ] || return 0
60
+ for helper in "$pty_dir"/prebuilds/*/spawn-helper "$pty_dir"/build/Release/spawn-helper; do
61
+ [ -e "$helper" ] || continue
62
+ found=true
63
+ chmod +x "$helper" 2>/dev/null || true
64
+ done
65
+ $found || return 0
66
+ }
67
+
68
+ verify_native_modules() {
69
+ local pty_dir helper found=false
70
+ (cd "$ROOT" && "$NODE_BIN" - <<'NODE'
71
+ require('node-pty');
72
+ NODE
73
+ ) >/dev/null 2>&1 || return 1
74
+ [ "$(uname -s)" != "Darwin" ] && return 0
75
+ pty_dir="$(resolve_node_pty_dir)" || return 1
76
+ [ -n "$pty_dir" ] || return 1
77
+ for helper in "$pty_dir"/prebuilds/*/spawn-helper "$pty_dir"/build/Release/spawn-helper; do
78
+ [ -e "$helper" ] || continue
79
+ found=true
80
+ [ -x "$helper" ] || return 1
81
+ done
82
+ $found
83
+ }
84
+
85
+ repair_native_modules() {
86
+ local npm_bin pty_dir project_root
87
+ npm_bin="$(command -v npm 2>/dev/null || true)"
88
+ [ -n "$npm_bin" ] || { echo "npm is required to rebuild Townmouse native modules." >&2; return 1; }
89
+ pty_dir="$(resolve_node_pty_dir || true)"
90
+ [ -n "$pty_dir" ] || pty_dir="$ROOT/node_modules/node-pty"
91
+ # The owning project is whatever sits above the LAST node_modules segment:
92
+ # a hoisted consumer root, or the package itself for a global install.
93
+ project_root="${pty_dir%/node_modules/*}"
94
+ [ "$project_root" = "$pty_dir" ] && project_root="$ROOT"
95
+ (cd "$project_root" && "$npm_bin" rebuild "${NATIVE_MODULES[@]}")
96
+ fix_spawn_helpers "$pty_dir"
97
+ verify_native_modules
98
+ }
99
+
100
+ case "${1:-doctor}" in
101
+ path)
102
+ printf '%s\n' "$NODE_BIN"
103
+ ;;
104
+ doctor)
105
+ printf 'node=%s version=%s abi=%s\n' "$NODE_BIN" "$($NODE_BIN -v)" "$($NODE_BIN -p 'process.versions.modules')"
106
+ if verify_native_modules; then
107
+ echo "native_modules=ok"
108
+ else
109
+ echo "native_modules=unusable (node-pty failed to load or its spawn helper is not executable); run: scripts/node-runtime.sh repair" >&2
110
+ exit 1
111
+ fi
112
+ ;;
113
+ repair)
114
+ repair_native_modules
115
+ echo "native_modules=repaired"
116
+ ;;
117
+ exec)
118
+ shift
119
+ fix_spawn_helpers "$(resolve_node_pty_dir || true)"
120
+ verify_native_modules || {
121
+ echo "Townmouse native modules do not match $($NODE_BIN -v). Run: scripts/node-runtime.sh repair" >&2
122
+ exit 70
123
+ }
124
+ exec "$NODE_BIN" "$@"
125
+ ;;
126
+ run)
127
+ shift
128
+ verify_native_modules || {
129
+ echo "Townmouse native modules do not match $($NODE_BIN -v). Run: scripts/node-runtime.sh repair" >&2
130
+ exit 70
131
+ }
132
+ exec "$@"
133
+ ;;
134
+ *)
135
+ echo "Usage: $0 {path|doctor|repair|exec NODE_ARGS...|run COMMAND...}" >&2
136
+ exit 64
137
+ ;;
138
+ esac