sync-zed.ts 4.4 KB

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