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. const commitSha = await $`git rev-parse ${version}`.text()
  17. const sha = commitSha.trim()
  18. console.log(`🔍 Found commit SHA: ${sha}`)
  19. const extensionToml = await $`git show ${version}:packages/extensions/zed/extension.toml`.text()
  20. const parsed = Bun.TOML.parse(extensionToml) as { version: string }
  21. const extensionVersion = parsed.version
  22. if (extensionVersion !== cleanVersion) {
  23. throw new Error(`Version mismatch: extension.toml has ${extensionVersion} but tag is ${cleanVersion}`)
  24. }
  25. console.log(`✅ Version ${extensionVersion} matches tag`)
  26. // Clone the fork to a temp directory
  27. const workDir = join(tmpdir(), `zed-extensions-${Date.now()}`)
  28. console.log(`📁 Working in ${workDir}`)
  29. await $`git clone https://x-access-token:${token}@github.com/${FORK_REPO}.git ${workDir}`
  30. process.chdir(workDir)
  31. // Configure git identity
  32. await $`git config user.name "github-actions[bot]"`
  33. await $`git config user.email "github-actions[bot]@users.noreply.github.com"`
  34. // Sync fork with upstream
  35. console.log(`🔄 Syncing fork with upstream...`)
  36. await $`git remote add upstream https://github.com/${UPSTREAM_REPO}.git`
  37. await $`git fetch upstream`
  38. await $`git checkout main`
  39. await $`git merge upstream/main --ff-only`
  40. await $`git push origin main`
  41. console.log(`✅ Fork synced`)
  42. // Create a new branch
  43. const branchName = `update-${EXTENSION_NAME}-${cleanVersion}`
  44. console.log(`🌿 Creating branch ${branchName}`)
  45. await $`git checkout -b ${branchName}`
  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. console.log(`📝 Updating extensions.toml`)
  55. const extensionsTomlPath = "extensions.toml"
  56. const extensionsToml = await Bun.file(extensionsTomlPath).text()
  57. const versionRegex = new RegExp(`(\\[${EXTENSION_NAME}\\][\\s\\S]*?)version = "[^"]+"`)
  58. const updatedToml = extensionsToml.replace(versionRegex, `$1version = "${cleanVersion}"`)
  59. if (updatedToml === extensionsToml) {
  60. throw new Error(`Failed to update version in extensions.toml - pattern not found`)
  61. }
  62. await Bun.write(extensionsTomlPath, updatedToml)
  63. await $`git add extensions.toml`
  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. console.log(`🚀 Pushing to fork...`)
  70. await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git ${branchName}`
  71. console.log(`📬 Creating pull request...`)
  72. const prUrl =
  73. 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()
  74. console.log(`✅ Pull request created: ${prUrl}`)
  75. console.log(`🎉 Done!`)
  76. }
  77. main().catch((err) => {
  78. console.error("❌ Error:", err.message)
  79. process.exit(1)
  80. })