townmouse 0.0.0-stage → 0.4.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.
Files changed (108) hide show
  1. package/CHANGELOG.md +196 -0
  2. package/DESIGN.md +153 -0
  3. package/LICENSE +661 -0
  4. package/README.md +600 -2
  5. package/SECURITY.md +60 -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 +371 -0
  13. package/docs/browser-workspace.md +237 -0
  14. package/docs/panes.md +442 -0
  15. package/native/TownmouseApp.swift +108 -0
  16. package/npm-shrinkwrap.json +1282 -0
  17. package/package.json +93 -4
  18. package/public/apple-touch-icon.png +0 -0
  19. package/public/brand/floydslabs-credit.png +0 -0
  20. package/public/brand/floydslabs-credit.webp +0 -0
  21. package/public/brand/townmouse-aristocrat.webp +0 -0
  22. package/public/brand/townmouse-monocled.webp +0 -0
  23. package/public/browser-picture.mjs +159 -0
  24. package/public/clipboard-text.mjs +5 -0
  25. package/public/favicon-16x16.png +0 -0
  26. package/public/favicon-32x32.png +0 -0
  27. package/public/favicon.ico +0 -0
  28. package/public/favicon.svg +1 -0
  29. package/public/features/app-version.mjs +35 -0
  30. package/public/features/autosave.mjs +80 -0
  31. package/public/features/clipboard.mjs +69 -0
  32. package/public/features/command-palette.mjs +181 -0
  33. package/public/features/desktop-palette.mjs +19 -0
  34. package/public/features/export.mjs +37 -0
  35. package/public/features/file-drop.mjs +77 -0
  36. package/public/features/floating-palette.mjs +113 -0
  37. package/public/features/fontsize.mjs +72 -0
  38. package/public/features/haptic-keys.mjs +42 -0
  39. package/public/features/history-undo.mjs +102 -0
  40. package/public/features/keepalive.mjs +36 -0
  41. package/public/features/keybar-config.mjs +70 -0
  42. package/public/features/keyboard-shortcuts.mjs +74 -0
  43. package/public/features/links.mjs +139 -0
  44. package/public/features/multi-window-sync.mjs +76 -0
  45. package/public/features/osc-bridge.mjs +64 -0
  46. package/public/features/phone-landscape-focus.mjs +157 -0
  47. package/public/features/recent-commands.mjs +100 -0
  48. package/public/features/search.mjs +134 -0
  49. package/public/features/session-lock.mjs +96 -0
  50. package/public/features/session-manager.mjs +251 -0
  51. package/public/features/settings-sync.mjs +77 -0
  52. package/public/features/shortcut-help.mjs +85 -0
  53. package/public/features/smart-completions.mjs +101 -0
  54. package/public/features/smart-suggest.mjs +124 -0
  55. package/public/features/snippets.mjs +85 -0
  56. package/public/features/split-view.mjs +102 -0
  57. package/public/features/status-metrics.mjs +137 -0
  58. package/public/features/stylus-support.mjs +124 -0
  59. package/public/features/swipe-history.mjs +141 -0
  60. package/public/features/swipe-sessions.mjs +85 -0
  61. package/public/features/tab-reorder.mjs +70 -0
  62. package/public/features/theme-system.mjs +51 -0
  63. package/public/features/toast-status.mjs +118 -0
  64. package/public/features/two-finger-menu.mjs +87 -0
  65. package/public/features/voice-input.mjs +244 -0
  66. package/public/features/wake-fullscreen.mjs +67 -0
  67. package/public/features/window-title.mjs +47 -0
  68. package/public/features/zoom.mjs +80 -0
  69. package/public/icon-128.png +0 -0
  70. package/public/icon-180.png +0 -0
  71. package/public/icon-192.png +0 -0
  72. package/public/icon-48.png +0 -0
  73. package/public/icon-512-maskable.png +0 -0
  74. package/public/icon-512.png +0 -0
  75. package/public/icon.svg +1 -0
  76. package/public/index.html +2304 -0
  77. package/public/input-chunks.mjs +22 -0
  78. package/public/input-guard.mjs +218 -0
  79. package/public/manifest.json +56 -0
  80. package/public/panes.css +1433 -0
  81. package/public/panes.html +211 -0
  82. package/public/panes.mjs +3183 -0
  83. package/public/service-worker.js +67 -0
  84. package/public/workspace.css +1621 -0
  85. package/public/workspace.mjs +1715 -0
  86. package/scripts/install-service.sh +303 -0
  87. package/scripts/node-runtime.sh +138 -0
  88. package/scripts/panes +568 -0
  89. package/scripts/prepare-native.sh +35 -0
  90. package/scripts/retire-legacy-service.sh +76 -0
  91. package/scripts/run-hooks +69 -0
  92. package/scripts/townmouse.sh +152 -0
  93. package/scripts/uninstall-service.sh +65 -0
  94. package/src/agent-guide.js +329 -0
  95. package/src/attachments.js +128 -0
  96. package/src/browser-dom.js +548 -0
  97. package/src/browser-workspace.js +1733 -0
  98. package/src/chrome-resolve.js +80 -0
  99. package/src/owner-page.js +43 -0
  100. package/src/pty-permissions.js +99 -0
  101. package/src/regex-worker.js +13 -0
  102. package/src/replay-text.js +189 -0
  103. package/src/request-boundary.js +58 -0
  104. package/src/safe-regex.js +87 -0
  105. package/src/server.js +1582 -0
  106. package/src/shell-resolve.js +42 -0
  107. package/src/static-deps.js +61 -0
  108. package/src/workspace-panes.js +1431 -0
@@ -0,0 +1,303 @@
1
+ #!/bin/bash
2
+ #
3
+ # Townmouse — install the always-on macOS LaunchAgent (launchd).
4
+ #
5
+ # Townmouse keeps its own service name and port so it can sit beside TerminalOne,
6
+ # the engine it embeds. TerminalOne is `com.floyd.terminalone` on 11001 with the
7
+ # `t1` launcher; none of those are touched here.
8
+ #
9
+ # EFFECT RADIUS (what this changes on your machine):
10
+ # - Writes ~/Library/LaunchAgents/com.floyd.townmouse.plist
11
+ # - Registers a per-user LaunchAgent that runs `node src/server.js` on PORT 11002
12
+ # - RunAtLoad (starts at login) + KeepAlive (auto-restart on crash)
13
+ # - KeepAlive is gated on PathState: it only runs while
14
+ # the installed checkout's `src/server.js` exists (i.e. the app volume is
15
+ # mounted), and auto-starts when the volume re-appears.
16
+ # - Logs to ~/Library/Logs/com.floyd.townmouse.{out,err}.log (INTERNAL disk;
17
+ # launchd cannot reliably create log files on the external volume -> EX_CONFIG).
18
+ # - Installs a self-contained `townmouse` launcher on the INTERNAL disk (first
19
+ # writable of ~/.local/bin, /usr/local/bin, ~/bin) so `townmouse` works from
20
+ # any shell and does NOT dangle when the external volume unmounts (it
21
+ # mount-checks and errors clearly).
22
+ # - Creates /Applications/Townmouse.app (with brand icon, quarantine cleared) so
23
+ # it launches with no terminal: Spotlight (Cmd-Space), Launchpad, and the Dock.
24
+ # - Retires the service that Townmouse 0.2.x installed under TerminalOne's name,
25
+ # if this Mac still has one (scripts/retire-legacy-service.sh).
26
+ # Fully reversible: scripts/uninstall-service.sh
27
+ #
28
+ set -euo pipefail
29
+
30
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
31
+ LABEL="com.floyd.townmouse"
32
+ APP_DIR="${TERMINALONE_APP_DIR:-$(cd "$SCRIPT_DIR/.." && pwd -P)}"
33
+ PORT="${PORT:-11002}"
34
+ NODE="${TERMINALONE_NODE:-$("$APP_DIR/scripts/node-runtime.sh" path 2>/dev/null || true)}"
35
+ PLIST_DIR="${TERMINALONE_PLIST_DIR:-$HOME/Library/LaunchAgents}"
36
+ PLIST="$PLIST_DIR/$LABEL.plist"
37
+ LOG_DIR="${TERMINALONE_SYSTEM_LOG_DIR:-$HOME/Library/Logs}"
38
+ DOMAIN="gui/$(id -u)"
39
+ APP_PARENT_OVERRIDE="${TERMINALONE_APPLICATIONS_DIR:-}"
40
+ BIN_DIR_OVERRIDE="${TERMINALONE_BIN_DIR:-}"
41
+
42
+ [ -n "$NODE" ] || { echo "ERROR: Townmouse requires Node 22.x; run scripts/node-runtime.sh doctor"; exit 1; }
43
+ [ -f "$APP_DIR/src/server.js" ] || { echo "ERROR: $APP_DIR/src/server.js not found (is the volume mounted?)"; exit 1; }
44
+ # Pick the directory for the `townmouse` launcher before anything is changed, so
45
+ # an unusable choice stops the install with nothing half-done. The directory must
46
+ # be on the INTERNAL disk: macOS keeps writable paths on the Data volume, and a
47
+ # launcher on a volume that unmounts would dangle. A `townmouse` that this
48
+ # installer did not write (npm's own command for the package, or someone else's)
49
+ # is never overwritten; such a directory is passed over.
50
+ INTERNAL_DEV="$(df /System/Volumes/Data 2>/dev/null | tail -1 | awk '{print $1}')"
51
+ [ -n "$INTERNAL_DEV" ] || INTERNAL_DEV="$(df / | tail -1 | awk '{print $1}')"
52
+ is_internal() { case "$1" in /Volumes/*) return 1;; esac; [ "$(df "$1" 2>/dev/null | tail -1 | awk '{print $1}')" = "$INTERNAL_DEV" ]; }
53
+ launcher_slot_is_free() {
54
+ local launcher="$1/townmouse"
55
+ { [ -e "$launcher" ] || [ -L "$launcher" ]; } || return 0
56
+ [ -f "$launcher" ] && [ ! -L "$launcher" ] && grep -q 'Townmouse launcher' "$launcher" 2>/dev/null
57
+ }
58
+ BIN_DIR_SELECTED=""
59
+ if [ -n "$BIN_DIR_OVERRIDE" ]; then
60
+ if [ -e "$BIN_DIR_OVERRIDE/townmouse" ] && ! launcher_slot_is_free "$BIN_DIR_OVERRIDE"; then
61
+ echo "ERROR: $BIN_DIR_OVERRIDE/townmouse exists and was not written by this installer, so it will not be overwritten. Remove it, or point TERMINALONE_BIN_DIR at another directory on the internal disk." >&2
62
+ exit 1
63
+ fi
64
+ mkdir -p "$BIN_DIR_OVERRIDE"
65
+ BIN_DIR_SELECTED="$BIN_DIR_OVERRIDE"
66
+ else
67
+ for d in "$HOME/.local/bin" /usr/local/bin "$HOME/bin"; do
68
+ if [ -d "$d" ] && [ -w "$d" ] && is_internal "$d" && launcher_slot_is_free "$d"; then BIN_DIR_SELECTED="$d"; break; fi
69
+ done
70
+ if [ -z "$BIN_DIR_SELECTED" ]; then
71
+ # Nothing writable, internal and free of a foreign `townmouse`: create ~/bin on the internal disk.
72
+ if mkdir -p "$HOME/bin" 2>/dev/null && is_internal "$HOME/bin" && launcher_slot_is_free "$HOME/bin"; then
73
+ BIN_DIR_SELECTED="$HOME/bin"
74
+ else
75
+ BIN_DIR_SELECTED="$HOME/.local/bin"
76
+ if ! launcher_slot_is_free "$BIN_DIR_SELECTED"; then
77
+ echo "ERROR: $BIN_DIR_SELECTED/townmouse exists and was not written by this installer, so it will not be overwritten. Remove it, or set TERMINALONE_BIN_DIR to a directory on the internal disk." >&2
78
+ exit 1
79
+ fi
80
+ mkdir -p "$BIN_DIR_SELECTED"
81
+ echo " WARN : no internal-disk bin dir found; townmouse placed on $BIN_DIR_SELECTED (may be external)."
82
+ fi
83
+ fi
84
+ fi
85
+ # A package tarball can lose execute bits on helper scripts. Restore the ones
86
+ # this installer calls before it calls them.
87
+ chmod +x "$APP_DIR/scripts/run-hooks" "$APP_DIR/scripts/townmouse.sh" "$APP_DIR/scripts/node-runtime.sh" 2>/dev/null || true
88
+ if [ "${TERMINALONE_SKIP_RUNTIME_CHECK:-0}" != "1" ]; then
89
+ "$APP_DIR/scripts/node-runtime.sh" doctor >/dev/null || {
90
+ echo "Repairing Townmouse native modules for $($NODE -v)..."
91
+ "$APP_DIR/scripts/node-runtime.sh" repair
92
+ }
93
+ fi
94
+ "$APP_DIR/scripts/run-hooks" pre-install
95
+ mkdir -p "$PLIST_DIR" "$LOG_DIR"
96
+
97
+ # 0.2.x installed this app under TerminalOne's launchd label and port. Take that
98
+ # service out of TerminalOne's way before starting ours.
99
+ bash "$SCRIPT_DIR/retire-legacy-service.sh"
100
+
101
+ # NOTE: no WorkingDirectory key — the server resolves all paths via __dirname, and a
102
+ # chdir into the external volume from the launchd context can itself trigger EX_CONFIG.
103
+ cat > "$PLIST" <<PLISTEOF
104
+ <?xml version="1.0" encoding="UTF-8"?>
105
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
106
+ <plist version="1.0">
107
+ <dict>
108
+ <key>Label</key><string>$LABEL</string>
109
+ <key>ProgramArguments</key>
110
+ <array>
111
+ <string>$NODE</string>
112
+ <string>$APP_DIR/src/server.js</string>
113
+ </array>
114
+ <key>EnvironmentVariables</key>
115
+ <dict>
116
+ <key>PORT</key><string>$PORT</string>
117
+ <key>TERMINALONE_TRUSTED_ORIGINS</key><string>${TERMINALONE_TRUSTED_ORIGINS:-}</string>
118
+ <key>PATH</key><string>$(dirname "$NODE"):/usr/bin:/bin:/usr/sbin:/sbin</string>
119
+ </dict>
120
+ <key>RunAtLoad</key><true/>
121
+ <key>KeepAlive</key>
122
+ <dict>
123
+ <key>PathState</key>
124
+ <dict>
125
+ <key>$APP_DIR/src/server.js</key><true/>
126
+ </dict>
127
+ </dict>
128
+ <key>StandardOutPath</key><string>$LOG_DIR/$LABEL.out.log</string>
129
+ <key>StandardErrorPath</key><string>$LOG_DIR/$LABEL.err.log</string>
130
+ <key>ProcessType</key><string>Background</string>
131
+ </dict>
132
+ </plist>
133
+ PLISTEOF
134
+
135
+ # (Re)load cleanly and IDEMPOTENTLY. bootout is async and KeepAlive/PathState can
136
+ # respawn the job mid-teardown, so bootstrap can race it (Input/output error 5).
137
+ # Wait for the label to actually disappear (up to ~5s). If it's still present after
138
+ # the wait, the service is truly still loaded and we reload via kickstart in place.
139
+ # If it disappeared, bootstrap should succeed — any failure is a real error.
140
+ launchctl bootout "$DOMAIN/$LABEL" 2>/dev/null || true
141
+ LABEL_STILL_PRESENT=false
142
+ for _ in $(seq 1 20); do
143
+ if launchctl print "$DOMAIN/$LABEL" >/dev/null 2>&1; then
144
+ LABEL_STILL_PRESENT=true
145
+ sleep 0.25
146
+ else
147
+ LABEL_STILL_PRESENT=false
148
+ break
149
+ fi
150
+ done
151
+ launchctl enable "$DOMAIN/$LABEL" 2>/dev/null || true
152
+ if $LABEL_STILL_PRESENT; then
153
+ # Label survived bootout + 5s wait — service is legitimately still loaded.
154
+ # bootstrap would fail because the label exists; reload via kickstart instead.
155
+ echo " note : service already loaded; reloading in place."
156
+ launchctl kickstart -k "$DOMAIN/$LABEL" || {
157
+ echo "ERROR: failed to kickstart existing service." >&2
158
+ exit 1
159
+ }
160
+ else
161
+ # Label gone after bootout — fresh bootstrap MUST succeed.
162
+ if ! launchctl bootstrap "$DOMAIN" "$PLIST"; then
163
+ echo "ERROR: launchctl bootstrap failed — service was not installed." >&2
164
+ exit 1
165
+ fi
166
+ launchctl kickstart -k "$DOMAIN/$LABEL" || true
167
+ fi
168
+
169
+ # --- Install `townmouse` as a REAL self-contained launcher on the INTERNAL disk.
170
+ # Not a symlink into the external volume: a symlink would dangle ("no such file")
171
+ # the moment the app volume unmounts. This generated stub lives on the internal disk,
172
+ # bakes in the app path, checks the mount, and fails LOUDLY (stderr + GUI dialog)
173
+ # instead of cryptically when the volume is gone.
174
+ chmod +x "$APP_DIR/scripts/townmouse.sh" 2>/dev/null || true
175
+ LAUNCHER_LINK="$BIN_DIR_SELECTED/townmouse"
176
+ LAUNCHER_TMP="$(mktemp "$BIN_DIR_SELECTED/.townmouse.XXXXXX")"
177
+ cat > "$LAUNCHER_TMP" <<LAUNCHEREOF
178
+ #!/bin/bash
179
+ # Townmouse launcher — generated by install-service.sh. Self-contained; lives on
180
+ # the internal disk so it survives "$APP_DIR" unmounting. Regenerate via reinstall.
181
+ set -euo pipefail
182
+ TOWNMOUSE_APP="$APP_DIR/scripts/townmouse.sh"
183
+ if [ ! -f "\$TOWNMOUSE_APP" ]; then
184
+ MSG="Townmouse is unavailable: its volume is not mounted (expected at $APP_DIR). Reconnect the drive, then try again."
185
+ printf '\\033[31mtownmouse: %s\\033[0m\\n' "\$MSG" >&2
186
+ # If launched without a terminal (Finder/Spotlight/Dock), surface a GUI dialog too.
187
+ if [ ! -t 2 ] && command -v osascript >/dev/null 2>&1; then
188
+ osascript -e "display dialog \"\$MSG\" with title \"Townmouse\" buttons {\"OK\"} default button \"OK\" with icon caution" >/dev/null 2>&1 || true
189
+ fi
190
+ exit 1
191
+ fi
192
+ case "\${1:-}" in
193
+ doctor) exec "$APP_DIR/scripts/node-runtime.sh" doctor ;;
194
+ repair) exec "$APP_DIR/scripts/node-runtime.sh" repair ;;
195
+ 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 ;;
196
+ esac
197
+ exec /bin/bash "\$TOWNMOUSE_APP" "\$@"
198
+ LAUNCHEREOF
199
+ chmod +x "$LAUNCHER_TMP"
200
+ mv -f "$LAUNCHER_TMP" "$LAUNCHER_LINK"
201
+
202
+ # --- Make Townmouse launchable from anywhere WITHOUT a terminal:
203
+ # a tiny .app bundle -> shows in Spotlight, Launchpad, and Dock. Prefer /Applications
204
+ # (canonically Spotlight-indexed); fall back to ~/Applications if it's not writable.
205
+ if [ -n "$APP_PARENT_OVERRIDE" ]; then
206
+ APP_PARENT="$APP_PARENT_OVERRIDE"
207
+ mkdir -p "$APP_PARENT"
208
+ elif [ -w /Applications ]; then
209
+ APP_PARENT="/Applications"
210
+ else
211
+ APP_PARENT="$HOME/Applications"
212
+ mkdir -p "$APP_PARENT"
213
+ fi
214
+ APP_BUNDLE="$APP_PARENT/Townmouse.app"
215
+ mkdir -p "$APP_BUNDLE/Contents/MacOS" "$APP_BUNDLE/Contents/Resources"
216
+ cat > "$APP_BUNDLE/Contents/Info.plist" <<APPEOF
217
+ <?xml version="1.0" encoding="UTF-8"?>
218
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
219
+ <plist version="1.0">
220
+ <dict>
221
+ <key>CFBundleName</key><string>Townmouse</string>
222
+ <key>CFBundleDisplayName</key><string>Townmouse</string>
223
+ <key>CFBundleIdentifier</key><string>com.floyd.townmouse</string>
224
+ <key>CFBundleVersion</key><string>1.0</string>
225
+ <key>CFBundlePackageType</key><string>APPL</string>
226
+ <key>CFBundleExecutable</key><string>Townmouse</string>
227
+ <key>CFBundleIconFile</key><string>Townmouse</string>
228
+ <key>LSMinimumSystemVersion</key><string>13.0</string>
229
+ <key>NSHighResolutionCapable</key><true/>
230
+ </dict>
231
+ </plist>
232
+ APPEOF
233
+ # Say which macOS versions the wrapper runs on, matching LSMinimumSystemVersion in
234
+ # the bundle. Without a target the compiler stamps its own default, which can be
235
+ # newer than the Mac it is built on; Launch Services then refuses the icon with
236
+ # kLSIncompatibleSystemVersionErr even though the program itself would run.
237
+ SWIFT_TARGET="$(uname -m)-apple-macosx13.0"
238
+ if [ -n "${TERMINALONE_SWIFTC:-}" ]; then
239
+ [ -x "$TERMINALONE_SWIFTC" ] || { echo "ERROR: TERMINALONE_SWIFTC is not executable." >&2; exit 1; }
240
+ "$TERMINALONE_SWIFTC" -parse-as-library -O -target "$SWIFT_TARGET" -framework AppKit -framework WebKit \
241
+ "$APP_DIR/native/TownmouseApp.swift" -o "$APP_BUNDLE/Contents/MacOS/Townmouse"
242
+ else
243
+ xcrun --find swiftc >/dev/null 2>&1 || { echo "ERROR: Swift compiler unavailable; install Apple Command Line Tools." >&2; exit 1; }
244
+ xcrun swiftc -parse-as-library -O -target "$SWIFT_TARGET" -framework AppKit -framework WebKit \
245
+ "$APP_DIR/native/TownmouseApp.swift" -o "$APP_BUNDLE/Contents/MacOS/Townmouse"
246
+ fi
247
+ printf '%s\n' "$LAUNCHER_LINK" > "$APP_BUNDLE/Contents/Resources/launcher-path.txt"
248
+ # Build the .icns from the brand icon so it shows in Spotlight/Launchpad/Dock.
249
+ RES_DIR="$APP_BUNDLE/Contents/Resources"
250
+ mkdir -p "$RES_DIR"
251
+ ICON_SRC="$APP_DIR/public/icon-512.png"
252
+ if [ -f "$ICON_SRC" ] && command -v iconutil >/dev/null 2>&1; then
253
+ ICONSET="$(mktemp -d)/Townmouse.iconset"
254
+ mkdir -p "$ICONSET"
255
+ for sz in 16 32 64 128 256 512; do
256
+ sips -z $sz $sz "$ICON_SRC" --out "$ICONSET/icon_${sz}x${sz}.png" >/dev/null 2>&1 || true
257
+ dbl=$((sz*2))
258
+ sips -z $dbl $dbl "$ICON_SRC" --out "$ICONSET/icon_${sz}x${sz}@2x.png" >/dev/null 2>&1 || true
259
+ done
260
+ iconutil -c icns "$ICONSET" -o "$RES_DIR/Townmouse.icns" 2>/dev/null || cp "$ICON_SRC" "$RES_DIR/Townmouse.png"
261
+ rm -rf "$(dirname "$ICONSET")"
262
+ fi
263
+ # Clear any quarantine flag so the locally-built bundle never triggers a Gatekeeper
264
+ # prompt on first Finder/Spotlight launch. Safe no-op if no flag is present.
265
+ xattr -dr com.apple.quarantine "$APP_BUNDLE" 2>/dev/null || true
266
+ # Register with Launch Services so Spotlight/Launchpad index it promptly.
267
+ if [ "${TERMINALONE_SKIP_LSREGISTER:-0}" != "1" ]; then
268
+ /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$APP_BUNDLE" 2>/dev/null || true
269
+ fi
270
+ "$APP_DIR/scripts/run-hooks" post-install --best-effort
271
+ # Ensure the chosen bin dir is actually on PATH. If not, add a marked line to the
272
+ # user's shell rc (idempotent — only once). Covers zsh (default) and bash.
273
+ LAUNCHER_PATH_ADDED=""
274
+ case ":$PATH:" in
275
+ *":$BIN_DIR_SELECTED:"*) : ;; # already on PATH this session
276
+ *)
277
+ MARK="# added by Townmouse install-service.sh"
278
+ LINE="export PATH=\"$BIN_DIR_SELECTED:\$PATH\" $MARK"
279
+ for rc in "$HOME/.zshrc" "$HOME/.bash_profile"; do
280
+ if [ ! -e "$rc" ]; then
281
+ if [ "$rc" = "$HOME/.zshrc" ]; then touch "$rc"; else continue; fi
282
+ fi
283
+ if ! grep -qF "$MARK" "$rc" 2>/dev/null; then
284
+ printf '\n%s\n' "$LINE" >> "$rc" && LAUNCHER_PATH_ADDED="$rc"
285
+ fi
286
+ done
287
+ ;;
288
+ esac
289
+
290
+ echo "Installed $LABEL"
291
+ echo " plist : $PLIST"
292
+ echo " node : $NODE"
293
+ echo " logs : $LOG_DIR/$LABEL.{out,err}.log and $LOG_DIR/Townmouse/local-launch.err.log"
294
+ echo " url : http://localhost:$PORT"
295
+ echo " townmouse : ${LAUNCHER_LINK:-(failed to link)}"
296
+ echo " app : $APP_BUNDLE (Spotlight: Cmd-Space -> Townmouse)"
297
+ echo " app : opens the UI only after health is confirmed; falls back to direct local startup if launchd is unavailable."
298
+ echo "Manage: launchctl print $DOMAIN/$LABEL | stop: scripts/uninstall-service.sh"
299
+ if [ -n "$LAUNCHER_PATH_ADDED" ]; then
300
+ echo " PATH : added $BIN_DIR_SELECTED to $LAUNCHER_PATH_ADDED — run 'source $LAUNCHER_PATH_ADDED' or open a new shell, then 'townmouse'."
301
+ else
302
+ echo " PATH : $BIN_DIR_SELECTED already on PATH — run 'townmouse' now."
303
+ 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