2026-04-03 21:37:16 +09:00
|
|
|
import { log } from "../shared/logger"
|
|
|
|
|
|
|
|
|
|
declare const Bun: {
|
|
|
|
|
which(commandName: string): string | null
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-27 17:17:13 +09:00
|
|
|
type Platform = "darwin" | "linux" | "win32" | "unsupported"
|
|
|
|
|
|
|
|
|
|
async function findCommand(commandName: string): Promise<string | null> {
|
|
|
|
|
try {
|
2026-02-01 17:13:54 +07:00
|
|
|
return Bun.which(commandName)
|
2026-04-03 21:37:16 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
log("[session-notification] failed to resolve command path", {
|
|
|
|
|
commandName,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
})
|
2025-12-27 17:17:13 +09:00
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 21:37:16 +09:00
|
|
|
function logBackgroundCheckError(commandName: string, error: unknown): void {
|
|
|
|
|
log("[session-notification] background command check failed", {
|
|
|
|
|
commandName,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 15:46:14 +09:00
|
|
|
function createCommandFinder(commandName: string): () => Promise<string | null> {
|
|
|
|
|
let cachedPath: string | null = null
|
|
|
|
|
let pending: Promise<string | null> | null = null
|
2025-12-27 17:17:13 +09:00
|
|
|
|
2026-01-31 15:46:14 +09:00
|
|
|
return async () => {
|
|
|
|
|
if (cachedPath !== null) return cachedPath
|
|
|
|
|
if (pending) return pending
|
2025-12-27 17:17:13 +09:00
|
|
|
|
2026-01-31 15:46:14 +09:00
|
|
|
pending = (async () => {
|
|
|
|
|
const path = await findCommand(commandName)
|
|
|
|
|
cachedPath = path
|
|
|
|
|
return path
|
|
|
|
|
})()
|
2025-12-27 17:17:13 +09:00
|
|
|
|
2026-01-31 15:46:14 +09:00
|
|
|
return pending
|
|
|
|
|
}
|
2025-12-27 17:17:13 +09:00
|
|
|
}
|
|
|
|
|
|
2026-01-31 15:46:14 +09:00
|
|
|
export const getNotifySendPath = createCommandFinder("notify-send")
|
|
|
|
|
export const getOsascriptPath = createCommandFinder("osascript")
|
|
|
|
|
export const getPowershellPath = createCommandFinder("powershell")
|
|
|
|
|
export const getAfplayPath = createCommandFinder("afplay")
|
|
|
|
|
export const getPaplayPath = createCommandFinder("paplay")
|
|
|
|
|
export const getAplayPath = createCommandFinder("aplay")
|
2026-02-27 14:39:07 +09:00
|
|
|
export const getTerminalNotifierPath = createCommandFinder("terminal-notifier")
|
2025-12-27 17:17:13 +09:00
|
|
|
|
|
|
|
|
export function startBackgroundCheck(platform: Platform): void {
|
|
|
|
|
if (platform === "darwin") {
|
2026-04-03 21:37:16 +09:00
|
|
|
getOsascriptPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("osascript", error)
|
|
|
|
|
})
|
|
|
|
|
getAfplayPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("afplay", error)
|
|
|
|
|
})
|
|
|
|
|
getTerminalNotifierPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("terminal-notifier", error)
|
|
|
|
|
})
|
2025-12-27 17:17:13 +09:00
|
|
|
} else if (platform === "linux") {
|
2026-04-03 21:37:16 +09:00
|
|
|
getNotifySendPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("notify-send", error)
|
|
|
|
|
})
|
|
|
|
|
getPaplayPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("paplay", error)
|
|
|
|
|
})
|
|
|
|
|
getAplayPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("aplay", error)
|
|
|
|
|
})
|
2025-12-27 17:17:13 +09:00
|
|
|
} else if (platform === "win32") {
|
2026-04-03 21:37:16 +09:00
|
|
|
getPowershellPath().catch((error) => {
|
|
|
|
|
logBackgroundCheckError("powershell", error)
|
|
|
|
|
})
|
2025-12-27 17:17:13 +09:00
|
|
|
}
|
|
|
|
|
}
|