Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.5 KB
Raw Blame
import { watch, type FSWatcher } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'

const MAC_BUNDLE_UPDATE_TIMEOUT_MS = 120_000

export function getMacAppBundlePath(executable: string): string | null {
  if (process.platform !== 'darwin') {
    return null
  }
  const macOsDir = dirname(executable)
  const contentsDir = dirname(macOsDir)
  const appBundlePath = dirname(contentsDir)
  return appBundlePath.endsWith('.app') ? appBundlePath : null
}

export async function waitForMacBundleVersion(
  executable: string,
  targetVersion: string,
  timeoutMs = MAC_BUNDLE_UPDATE_TIMEOUT_MS,
  signal?: AbortSignal
): Promise<boolean> {
  const appBundlePath = getMacAppBundlePath(executable)
  if (!appBundlePath) {
    return false
  }
  const infoPlistPath = resolve(appBundlePath, 'Contents', 'Info.plist')
  if ((await readMacBundleVersion(infoPlistPath)) === targetVersion) {
    return true
  }

  return new Promise((resolveWait) => {
    let settled = false
    let checking = false
    let watcher: FSWatcher | null = null
    let poll: ReturnType<typeof setInterval> | null = null
    const finish = (ready: boolean): void => {
      if (settled) {
        return
      }
      settled = true
      clearTimeout(timeout)
      if (poll) {
        clearInterval(poll)
      }
      watcher?.close()
      resolveWait(ready)
    }
    const check = (): void => {
      if (checking || settled) {
        return
      }
      checking = true
      void readMacBundleVersion(infoPlistPath)
        .then((version) => {
          if (version === targetVersion) {
            finish(true)
          }
        })
        .finally(() => {
          checking = false
        })
    }
    const timeout = setTimeout(() => finish(false), timeoutMs)
    if (signal?.aborted) {
      finish(false)
      return
    }
    signal?.addEventListener('abort', () => finish(false), { once: true })
    poll = setInterval(check, 250)
    try {
      // ShipIt replaces the whole bundle; watch its stable parent.
      watcher = watch(dirname(appBundlePath), check)
      watcher.on('error', () => {
        watcher?.close()
        watcher = null
      })
    } catch {
      watcher = null
    }
    check()
  })
}

async function readMacBundleVersion(infoPlistPath: string): Promise<string | null> {
  try {
    const plist = await readFile(infoPlistPath, 'utf8')
    const match = /<key>CFBundleShortVersionString<\/key>\s*<string>([^<]+)<\/string>/.exec(plist)
    return match?.[1]?.trim() || null
  } catch {
    return null
  }
}