sync-zed.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { tmpdir } from "os"
  4. import { join } from "path"
  5. const FORK_REPO = "anomalyco/zed-extensions"
  6. const UPSTREAM_REPO = "zed-industries/extensions"
  7. const EXTENSION_NAME = "opencode"
  8. async function main() {
  9. const version = process.argv[2]
  10. if (!version) throw new Error("Version argument required, ex: bun script/sync-zed.ts v1.0.52")
  11. const token = process.env.ZED_EXTENSIONS_PAT
  12. if (!token) throw new Error("ZED_EXTENSIONS_PAT environment variable required")
  13. const prToken = process.env.ZED_PR_PAT
  14. if (!prToken) throw new Error("ZED_PR_PAT environment variable required")
  15. const cleanVersion = version.replace(/^v/, "")
  16. console.log(`📦 Syncing Zed extension for version ${cleanVersion}`)
  17. const commitSha = await $`git rev-parse ${version}`.text()
  18. const sha = commitSha.trim()
  19. console.log(`🔍 Found commit SHA: ${sha}`)
  20. const extensionToml = await $`git show ${version}:packages/extensions/zed/extension.toml`.text()
  21. const parsed = Bun.TOML.parse(extensionToml) as { version: string }
  22. const extensionVersion = parsed.version
  23. if (extensionVersion !== cleanVersion) {
  24. throw new Error(`Version mismatch: extension.toml has ${extensionVersion} but tag is ${cleanVersion}`)
  25. }
  26. console.log(`✅ Version ${extensionVersion} matches tag`)
  27. // Clone the fork to a temp directory
  28. const workDir = join(tmpdir(), `zed-extensions-${Date.now()}`)
  29. console.log(`📁 Working in ${workDir}`)
  30. await $`git clone https://x-access-token:${token}@github.com/${FORK_REPO}.git ${workDir}`
  31. process.chdir(workDir)
  32. // Configure git identity
  33. await $`git config user.name "Aiden Cline"`
  34. await $`git config user.email "[email protected] "`
  35. // Sync fork with upstream (force reset to match exactly)
  36. console.log(`🔄 Syncing fork with upstream...`)
  37. await $`git remote add upstream https://github.com/${UPSTREAM_REPO}.git`
  38. await $`git fetch upstream`
  39. await $`git checkout main`
  40. await $`git reset --hard upstream/main`
  41. await $`git push origin main --force`
  42. console.log(`✅ Fork synced (force reset to upstream)`)
  43. // Create a new branch
  44. const branchName = `update-${EXTENSION_NAME}-${cleanVersion}`
  45. console.log(`🌿 Creating branch ${branchName}`)
  46. await $`git checkout -b ${branchName}`
  47. const submodulePath = `extensions/${EXTENSION_NAME}`
  48. console.log(`📌 Updating submodule to commit ${sha}`)
  49. await $`git submodule update --init ${submodulePath}`
  50. process.chdir(submodulePath)
  51. await $`git fetch`
  52. await $`git checkout ${sha}`
  53. process.chdir(workDir)
  54. await $`git add ${submodulePath}`
  55. console.log(`📝 Updating extensions.toml`)
  56. const extensionsTomlPath = "extensions.toml"
  57. const extensionsToml = await Bun.file(extensionsTomlPath).text()
  58. const versionRegex = new RegExp(`(\\[${EXTENSION_NAME}\\][\\s\\S]*?)version = "[^"]+"`)
  59. const updatedToml = extensionsToml.replace(versionRegex, `$1version = "${cleanVersion}"`)
  60. if (updatedToml === extensionsToml) {
  61. throw new Error(`Failed to update version in extensions.toml - pattern not found`)
  62. }
  63. await Bun.write(extensionsTomlPath, updatedToml)
  64. await $`git add extensions.toml`
  65. const commitMessage = `Update ${EXTENSION_NAME} to v${cleanVersion}`
  66. await $`git commit -m ${commitMessage}`
  67. console.log(`✅ Changes committed`)
  68. // Delete any existing branches for opencode updates
  69. console.log(`🔍 Checking for existing branches...`)
  70. const branches = await $`git ls-remote --heads https://x-access-token:${token}@github.com/${FORK_REPO}.git`.text()
  71. const branchPattern = `refs/heads/update-${EXTENSION_NAME}-`
  72. const oldBranches = branches
  73. .split("\n")
  74. .filter((line) => line.includes(branchPattern))
  75. .map((line) => line.split("refs/heads/")[1])
  76. .filter(Boolean)
  77. if (oldBranches.length > 0) {
  78. console.log(`🗑️ Found ${oldBranches.length} old branch(es), deleting...`)
  79. for (const branch of oldBranches) {
  80. await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git --delete ${branch}`
  81. console.log(`✅ Deleted branch ${branch}`)
  82. }
  83. }
  84. console.log(`🚀 Pushing to fork...`)
  85. await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git ${branchName}`
  86. console.log(`📬 Creating pull request...`)
  87. const prResult =
  88. await $`gh pr create --repo ${UPSTREAM_REPO} --base main --head ${FORK_REPO.split("/")[0]}:${branchName} --title "Update ${EXTENSION_NAME} to v${cleanVersion}" --body "Updating OpenCode extension to v${cleanVersion}"`
  89. .env({ ...process.env, GH_TOKEN: prToken })
  90. .nothrow()
  91. if (prResult.exitCode !== 0) {
  92. console.error("stderr:", prResult.stderr.toString())
  93. throw new Error(`Failed with exit code ${prResult.exitCode}`)
  94. }
  95. const prUrl = prResult.stdout.toString().trim()
  96. console.log(`✅ Pull request created: ${prUrl}`)
  97. console.log(`🎉 Done!`)
  98. }
  99. main().catch((err) => {
  100. console.error("❌ Error:", err.message)
  101. process.exit(1)
  102. })