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.

6.6 KB
Raw Blame
#!/usr/bin/env node

// Migrate existing worktree-local Electron distributions to shared cache without waiting for next install.

import { execFileSync } from 'node:child_process'
import { randomUUID } from 'node:crypto'
import {
  accessSync,
  constants,
  existsSync,
  readFileSync,
  readdirSync,
  realpathSync,
  renameSync,
  rmSync,
  statSync,
  writeFileSync
} from 'node:fs'
import path from 'node:path'
import {
  hasAdoptedSharedElectronDist,
  isUsableElectronDist,
  publishSharedElectronDist,
  recordAdoptedSharedElectronDist,
  resolveSharedElectronDistEntry,
  shareElectronDistFromCache
} from './shared-electron-dist-cache.mjs'
import { getElectronPlatformPath } from './electron-platform-path.mjs'

const apply = process.argv.includes('--apply')
const repoRoot = process.argv.includes('--repo')
  ? path.resolve(process.argv[process.argv.indexOf('--repo') + 1])
  : process.cwd()

const excludeIndex = process.argv.indexOf('--exclude-worktree')
const excludeArgument = excludeIndex === -1 ? undefined : process.argv[excludeIndex + 1]
if (excludeIndex !== -1 && (!excludeArgument || excludeArgument.startsWith('-'))) {
  throw new Error('--exclude-worktree requires a directory')
}
const excludeWorktree = excludeArgument ? realpathSync(excludeArgument) : null

function isExcluded(worktree) {
  if (!excludeWorktree) {
    return false
  }
  try {
    return realpathSync(worktree) === excludeWorktree
  } catch {
    return path.resolve(worktree) === excludeWorktree
  }
}

function listWorktrees(root) {
  const raw = execFileSync('git', ['-C', root, 'worktree', 'list', '--porcelain'], {
    encoding: 'utf8'
  })
  return raw
    .split('\n')
    .filter((line) => line.startsWith('worktree '))
    .map((line) => line.slice('worktree '.length).trim())
}

/** Apparent size, walked in Node: `du` does not exist on Windows, where it silently reported 0. */
function measure(targetPath) {
  let total = 0
  let entries
  try {
    entries = readdirSync(targetPath, { withFileTypes: true })
  } catch {
    return 0
  }
  for (const entry of entries) {
    const entryPath = path.join(targetPath, entry.name)
    if (entry.isDirectory()) {
      total += measure(entryPath)
    } else if (!entry.isSymbolicLink()) {
      total += statSync(entryPath, { throwIfNoEntry: false })?.size ?? 0
    }
  }
  return total
}

/** Swap in a shared copy behind a rename, so an interrupted run never leaves a partial dist. */
function adoptInto(distPath, entry, identity) {
  const stagePath = `${distPath}.reclaim-${process.pid}-${randomUUID()}`
  if (!shareElectronDistFromCache(entry, stagePath, identity)) {
    rmSync(stagePath, { recursive: true, force: true })
    return false
  }
  const previousPath = `${distPath}.previous-${process.pid}-${randomUUID()}`
  renameSync(distPath, previousPath)
  try {
    renameSync(stagePath, distPath)
  } catch (error) {
    renameSync(previousPath, distPath)
    rmSync(stagePath, { recursive: true, force: true })
    throw error
  }
  rmSync(previousPath, { recursive: true, force: true })
  return true
}

function main() {
  let reclaimed = 0
  let converted = 0
  let skipped = 0

  for (const worktree of listWorktrees(repoRoot)) {
    if (isExcluded(worktree)) {
      continue
    }
    const electronPackageDir = path.join(worktree, 'node_modules', 'electron')
    const distPath = path.join(electronPackageDir, 'dist')
    if (!existsSync(path.join(electronPackageDir, 'package.json')) || !existsSync(distPath)) {
      continue
    }
    if (statSync(distPath, { throwIfNoEntry: false })?.isDirectory() !== true) {
      continue
    }

    let version
    try {
      version = JSON.parse(
        readFileSync(path.join(electronPackageDir, 'package.json'), 'utf8')
      ).version
    } catch {
      continue
    }
    const targetPlatform = process.platform
    const targetArch = process.arch
    let platformPath
    try {
      platformPath = getElectronPlatformPath(targetPlatform)
    } catch {
      continue
    }
    if (!isUsableElectronDist(distPath, version, platformPath)) {
      console.log(`skip  ${worktree}  (dist is not a complete Electron ${version})`)
      skipped += 1
      continue
    }

    const entry = resolveSharedElectronDistEntry({
      repoRoot: worktree,
      electronPackageDir,
      version,
      targetPlatform,
      targetArch
    })
    if (entry === null) {
      continue
    }
    if (hasAdoptedSharedElectronDist(entry)) {
      continue
    }

    try {
      // Elevated POSIX users can pass W_OK even when the install is deliberately read-only.
      if (process.platform !== 'win32' && (statSync(electronPackageDir).mode & 0o222) === 0) {
        throw new Error('Electron package directory has no write permission bits')
      }
      accessSync(electronPackageDir, constants.W_OK)
    } catch {
      console.log(`skip  ${worktree}  (Electron package directory is not writable)`)
      skipped += 1
      continue
    }

    const size = measure(distPath)
    if (!apply) {
      console.log(`would share  ${worktree}  ${(size / 1024 ** 3).toFixed(2)} GiB  (${version})`)
      reclaimed += size
      converted += 1
      continue
    }

    try {
      if (!existsSync(entry.entryPath)) {
        if (publishSharedElectronDist(distPath, entry, { version, platformPath })) {
          recordAdoptedSharedElectronDist(entry, writeFileSync)
          console.log(`seeded  ${worktree}  -> ${entry.entryPath}`)
          converted += 1
        } else {
          console.warn(`skip  ${worktree}  (shared Electron cache could not be prepared)`)
          skipped += 1
        }
        continue
      }
      if (adoptInto(distPath, entry, { version, platformPath })) {
        recordAdoptedSharedElectronDist(entry, writeFileSync)
        reclaimed += size
        converted += 1
        console.log(`shared  ${worktree}  ${(size / 1024 ** 3).toFixed(2)} GiB logical file data`)
      } else {
        console.warn(`skip  ${worktree}  (shared Electron copy could not be prepared)`)
        skipped += 1
      }
    } catch (error) {
      // A worktree that fails is left exactly as it was; it still has its own working dist.
      console.warn(`skip  ${worktree}  (${error instanceof Error ? error.message : String(error)})`)
      skipped += 1
    }
  }

  console.log(
    `\n${apply ? 'Shared' : 'Would share'} ${converted} worktree(s); ` +
      `logical file data ~${(reclaimed / 1024 ** 3).toFixed(2)} GiB (shared blocks affect disk savings)` +
      `${skipped > 0 ? `; skipped ${skipped}` : ''}` +
      `${apply ? '' : '\nRe-run with --apply to do it.'}`
  )
}

// Guarded so importing this module for tests does not sweep the whole repository.
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(import.meta.filename)) {
  main()
}