MCPcopy Index your code
hub / github.com/codeaashu/claude-code / detectMacosTerminal

Function detectMacosTerminal

src/utils/deepLink/terminalLauncher.ts:64–121  ·  view source on GitHub ↗

* Detect the user's preferred terminal on macOS. * Checks running processes first (most likely to be what the user prefers), * then falls back to checking installed .app bundles.

()

Source from the content-addressed store, hash-verified

62 * then falls back to checking installed .app bundles.
63 */
64async function detectMacosTerminal(): Promise<TerminalInfo> {
65 // Stored preference from a previous interactive session. This is the only
66 // signal that survives into the headless LaunchServices context — the env
67 // var check below never hits when we're launched from a browser link.
68 const stored = getGlobalConfig().deepLinkTerminal
69 if (stored) {
70 const match = MACOS_TERMINALS.find(t => t.app === stored)
71 if (match) {
72 return { name: match.name, command: match.app }
73 }
74 }
75
76 // Check the TERM_PROGRAM env var — if set, the user has a clear preference.
77 // TERM_PROGRAM may include a .app suffix (e.g., "iTerm.app"), so strip it.
78 const termProgram = process.env.TERM_PROGRAM
79 if (termProgram) {
80 const normalized = termProgram.replace(/\.app$/i, '').toLowerCase()
81 const match = MACOS_TERMINALS.find(
82 t =>
83 t.app.toLowerCase() === normalized ||
84 t.name.toLowerCase() === normalized,
85 )
86 if (match) {
87 return { name: match.name, command: match.app }
88 }
89 }
90
91 // Check which terminals are installed by looking for .app bundles.
92 // Try mdfind first (Spotlight), but fall back to checking /Applications
93 // directly since mdfind can return empty results if Spotlight is disabled
94 // or hasn't indexed the app yet.
95 for (const terminal of MACOS_TERMINALS) {
96 const { code, stdout } = await execFileNoThrow(
97 'mdfind',
98 [`kMDItemCFBundleIdentifier == "${terminal.bundleId}"`],
99 { timeout: 5000, useCwd: false },
100 )
101 if (code === 0 && stdout.trim().length > 0) {
102 return { name: terminal.name, command: terminal.app }
103 }
104 }
105
106 // Fallback: check /Applications directly (mdfind may not work if
107 // Spotlight indexing is disabled or incomplete)
108 for (const terminal of MACOS_TERMINALS) {
109 const { code: lsCode } = await execFileNoThrow(
110 'ls',
111 [`/Applications/${terminal.app}.app`],
112 { timeout: 1000, useCwd: false },
113 )
114 if (lsCode === 0) {
115 return { name: terminal.name, command: terminal.app }
116 }
117 }
118
119 // Terminal.app is always available on macOS
120 return { name: 'Terminal.app', command: 'Terminal' }
121}

Callers 1

detectTerminalFunction · 0.85

Calls 2

getGlobalConfigFunction · 0.85
execFileNoThrowFunction · 0.85

Tested by

no test coverage detected