sync-zed.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. const OPENCODE_REPO = "sst/opencode"
  9. async function main() {
  10. const version = process.argv[2]
  11. if (!version) throw new Error("Version argument required: bun script/sync-zed.ts v1.0.52")
  12. const token = process.env.GITHUB_TOKEN
  13. if (!token) throw new Error("GITHUB_TOKEN environment variable required")
  14. const cleanVersion = version.replace(/^v/, "")
  15. console.log(`📦 Syncing Zed extension for version ${cleanVersion}`)
  16. // Get the commit SHA for this version tag
  17. const commitSha = await $`git rev-parse ${version}`.text()
  18. const sha = commitSha.trim()
  19. console.log(`🔍 Found commit SHA: ${sha}`)
  20. // Read the extension.toml from this commit to verify version
  21. const extensionToml = await $`git show ${version}:packages/extensions/zed/extension.toml`.text()
  22. const parsed = Bun.TOML.parse(extensionToml) as { version: string }
  23. const extensionVersion = parsed.version
  24. if (extensionVersion !== cleanVersion) {
  25. throw new Error(`Version mismatch: extension.toml has ${extensionVersion} but tag is ${cleanVersion}`)
  26. }
  27. console.log(`✅ Version ${extensionVersion} matches tag`)
  28. // Clone the fork to a temp directory
  29. const workDir = join(tmpdir(), `zed-extensions-${Date.now()}`)
  30. console.log(`📁 Working in ${workDir}`)
  31. await $`git clone https://x-access-token:${token}@github.com/${FORK_REPO}.git ${workDir}`
  32. process.chdir(workDir)
  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. // Update the submodule for opencode extension
  46. const submodulePath = `extensions/${EXTENSION_NAME}`
  47. console.log(`📌 Updating submodule to commit ${sha}`)
  48. await $`git submodule update --init ${submodulePath}`
  49. process.chdir(submodulePath)
  50. await $`git fetch`
  51. await $`git checkout ${sha}`
  52. process.chdir(workDir)
  53. await $`git add ${submodulePath}`
  54. // Update extensions.toml
  55. console.log(`📝 Updating extensions.toml`)
  56. const extensionsTomlPath = "extensions.toml"
  57. const extensionsToml = await Bun.file(extensionsTomlPath).text()
  58. // Update version field for opencode extension
  59. const versionRegex = new RegExp(`(\\[${EXTENSION_NAME}\\]\\s+(?:.*\\s*)?)version = "[^"]+"`)
  60. const updatedToml = extensionsToml.replace(versionRegex, `$1version = "${cleanVersion}"`)
  61. await Bun.write(extensionsTomlPath, updatedToml)
  62. await $`git add extensions.toml`
  63. // Commit changes
  64. const commitMessage = `Update ${EXTENSION_NAME} to v${cleanVersion}
  65. Release notes:
  66. https://github.com/${OPENCODE_REPO}/releases/tag/v${cleanVersion}`
  67. await $`git commit -m ${commitMessage}`
  68. console.log(`✅ Changes committed`)
  69. // Push to fork
  70. console.log(`🚀 Pushing to fork...`)
  71. await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git ${branchName}`
  72. // Create PR using gh CLI
  73. console.log(`📬 Creating pull request...`)
  74. const prUrl =
  75. await $`gh pr create --repo ${UPSTREAM_REPO} --base main --head ${FORK_REPO.split("/")[0]}:${branchName} --title "Update ${EXTENSION_NAME} to v${cleanVersion}" --body "Release notes:\n\nhttps://github.com/${OPENCODE_REPO}/releases/tag/v${cleanVersion}"`.text()
  76. console.log(`✅ Pull request created: ${prUrl}`)
  77. console.log(`🎉 Done!`)
  78. }
  79. main().catch((err) => {
  80. console.error("❌ Error:", err.message)
  81. process.exit(1)
  82. })