(n: DesktopNotification)
| 36 | * when the (very fast) osascript call finishes. Never rejects. |
| 37 | */ |
| 38 | export async function notifyDesktop(n: DesktopNotification): Promise<void> { |
| 39 | if (process.platform !== 'darwin') return; // macOS only |
| 40 | |
| 41 | // Build: display notification "msg" with title "t" subtitle "s" sound name "Glass" |
| 42 | let script = `display notification "${escapeAppleScript(n.message)}" with title "${escapeAppleScript(n.title)}"`; |
| 43 | if (n.subtitle) script += ` subtitle "${escapeAppleScript(n.subtitle)}"`; |
| 44 | if (n.sound) script += ` sound name "Glass"`; |
| 45 | |
| 46 | await new Promise<void>((resolve) => { |
| 47 | let done = false; |
| 48 | const finish = () => { if (!done) { done = true; resolve(); } }; |
| 49 | try { |
| 50 | const child = spawn('osascript', ['-e', script], { stdio: 'ignore' }); |
| 51 | child.on('error', (e: any) => { |
| 52 | logger.debug?.('desktop notification unavailable', { err: e?.message }); |
| 53 | finish(); |
| 54 | }); |
| 55 | child.on('close', () => finish()); |
| 56 | // Safety: never hang the caller on a stuck osascript. |
| 57 | setTimeout(() => { try { child.kill(); } catch {} finish(); }, 5000); |
| 58 | } catch (e: any) { |
| 59 | logger.debug?.('desktop notification failed to spawn', { err: e?.message }); |
| 60 | finish(); |
| 61 | } |
| 62 | }); |
| 63 | } |
no test coverage detected