import { accessSync, constants } from 'node:fs' import { alerterCanNotify, alerterCanShow } from './backends/alerter.js' import type { AskBackendName, AskRequest, BackendName, NotifyBackendName, NotifyRequest, } from './backends/types.js' export type DetectOptions = { platform: NodeJS.Platform env: Record /** Defaults to `fs.accessSync(path, X_OK)` succeeding. */ isExecutable?: (path: string) => boolean } export type ForcedBackends = { ask?: AskBackendName; notify?: NotifyBackendName } export type Availability = { available: ReadonlySet missing: Partial> } export type BackendSelection = { ask?: { name: AskBackendName; forced: boolean } notify?: { name: NotifyBackendName; forced: boolean } /** Install hint, or the forced-unavailable message. */ askProblem?: string notifyProblem?: string } const ALL_BACKENDS: Array = ['alerter', 'osascript', 'zenity', 'notify-send'] const PLATFORM_OF: Record = { alerter: 'darwin', osascript: 'darwin', zenity: 'linux', 'notify-send': 'linux', } const DISPLAY_MISSING = 'DISPLAY and WAYLAND_DISPLAY are not set' const DBUS_MISSING = 'DBUS_SESSION_BUS_ADDRESS is not set' const GUI_SESSION_NOTE = 'The process must run in the desktop user GUI session (not a daemon, cron job or system service).' function defaultIsExecutable(path: string): boolean { try { accessSync(path, constants.X_OK) return true } catch { return false } } function isSet(value: string | undefined): boolean { return value != null && value !== '' } export function detectAvailability(options: DetectOptions): Availability { const { platform, env } = options const isExecutable = options.isExecutable ?? defaultIsExecutable const dirs = (env.PATH ?? '').split(':').filter((dir) => dir !== '') const onPath = (name: string) => dirs.some((dir) => isExecutable(`${dir}/${name}`)) const available = new Set() const missing: Partial> = {} for (const name of ALL_BACKENDS) { let reason: string | undefined if (PLATFORM_OF[name] !== platform) { reason = `not supported on ${platform}` } else if (!onPath(name)) { reason = 'not on PATH' } else if (name === 'zenity' && !isSet(env.DISPLAY) && !isSet(env.WAYLAND_DISPLAY)) { reason = DISPLAY_MISSING } else if (name === 'notify-send' && !isSet(env.DBUS_SESSION_BUS_ADDRESS)) { reason = DBUS_MISSING } if (reason == null) { available.add(name) } else { missing[name] = reason } } return { available, missing } } type Capability = 'dialog' | 'notify' function installHint( capability: Capability, availability: Availability, platform: NodeJS.Platform, ): string { if (platform === 'darwin') { const base = capability === 'dialog' ? 'No dialog backend found. Install alerter (brew install vjeantet/tap/alerter) or make osascript available.' : 'No notification backend found. Install alerter (brew install vjeantet/tap/alerter) or make osascript available.' return `${base} ${GUI_SESSION_NOTE}` } if (platform === 'linux') { if (capability === 'dialog') { const cause = availability.missing.zenity === DISPLAY_MISSING ? ' DISPLAY or WAYLAND_DISPLAY must be set.' : '' return `No dialog backend found. Install zenity.${cause} ${GUI_SESSION_NOTE}` } const cause = availability.missing['notify-send'] === DBUS_MISSING ? ' DBUS_SESSION_BUS_ADDRESS must be set.' : '' return `No notification backend found. Install notify-send (libnotify-bin).${cause} ${GUI_SESSION_NOTE}` } return `Desktop ${capability === 'dialog' ? 'dialogs are' : 'notifications are'} not supported on ${platform}. ${GUI_SESSION_NOTE}` } function select( capability: Capability, order: Array, forced: Name | undefined, availability: Availability, platform: NodeJS.Platform, ): { chosen?: { name: Name; forced: boolean }; problem?: string } { if (forced != null) { if (availability.available.has(forced)) { return { chosen: { name: forced, forced: true } } } const why = availability.missing[forced] ?? 'unavailable' return { problem: `Forced ${capability} backend ${forced} is unavailable: ${why}` } } const name = order.find((candidate) => availability.available.has(candidate)) if (name != null) { return { chosen: { name, forced: false } } } return { problem: installHint(capability, availability, platform) } } export function selectBackends( availability: Availability, forced: ForcedBackends, platform: NodeJS.Platform, ): BackendSelection { const askOrder: Array = platform === 'darwin' ? ['alerter', 'osascript'] : platform === 'linux' ? ['zenity'] : [] const notifyOrder: Array = platform === 'darwin' ? ['alerter', 'osascript'] : platform === 'linux' ? ['notify-send'] : [] const ask = select('dialog', askOrder, forced.ask, availability, platform) const notify = select('notify', notifyOrder, forced.notify, availability, platform) return { ask: ask.chosen, notify: notify.chosen, askProblem: ask.problem, notifyProblem: notify.problem, } } export function askBackendFor( request: AskRequest, selection: BackendSelection, availability: Availability, ): { ok: true; name: AskBackendName } | { ok: false; reason: string } { const ask = selection.ask if (ask == null) { return { ok: false, reason: selection.askProblem ?? 'No dialog backend is available' } } if (ask.name !== 'alerter') { return { ok: true, name: ask.name } } const shown = alerterCanShow(request) if (shown.ok) { return { ok: true, name: ask.name } } if (ask.forced) { return { ok: false, reason: `${shown.reason}; change the request or use another dialog backend`, } } if (availability.available.has('osascript')) { return { ok: true, name: 'osascript' } } return { ok: false, reason: `${shown.reason}, and the osascript fallback is unavailable: ${availability.missing.osascript ?? 'unavailable'}. Make osascript available or change the request.`, } } /** The notify backend for a request: an auto-selected alerter falls back to osascript when it cannot show it. */ export function notifyBackendFor( request: NotifyRequest, group: string | undefined, selection: BackendSelection, availability: Availability, ): { ok: true; name: NotifyBackendName } | { ok: false; reason: string } { const notify = selection.notify if (notify == null) { return { ok: false, reason: selection.notifyProblem ?? 'No notification backend is available' } } if (notify.name !== 'alerter') { return { ok: true, name: notify.name } } const shown = alerterCanNotify(request, group) if (shown.ok) { return { ok: true, name: notify.name } } if (notify.forced) { return { ok: false, reason: `${shown.reason}; use another notification backend` } } if (availability.available.has('osascript')) { return { ok: true, name: 'osascript' } } return { ok: false, reason: `${shown.reason}, and the osascript fallback is unavailable: ${availability.missing.osascript ?? 'unavailable'}`, } } export function createDetector( options: DetectOptions & { forced?: ForcedBackends }, ): () => { availability: Availability; selection: BackendSelection } { let cached: { availability: Availability; selection: BackendSelection } | undefined return () => { if (cached == null) { const availability = detectAvailability(options) cached = { availability, selection: selectBackends(availability, options.forced ?? {}, options.platform), } } return cached } }