|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { $ } from "bun" |
| 4 | +import { tmpdir } from "os" |
| 5 | +import { join } from "path" |
| 6 | + |
| 7 | +const FORK_REPO = "sst/zed-extensions" |
| 8 | +const UPSTREAM_REPO = "zed-industries/extensions" |
| 9 | +const EXTENSION_NAME = "opencode" |
| 10 | +const OPENCODE_REPO = "sst/opencode" |
| 11 | + |
| 12 | +async function main() { |
| 13 | + const version = process.argv[2] |
| 14 | + if (!version) throw new Error("Version argument required: bun script/sync-zed.ts v1.0.52") |
| 15 | + |
| 16 | + const token = process.env.GITHUB_TOKEN |
| 17 | + if (!token) throw new Error("GITHUB_TOKEN environment variable required") |
| 18 | + |
| 19 | + const cleanVersion = version.replace(/^v/, "") |
| 20 | + console.log(`📦 Syncing Zed extension for version ${cleanVersion}`) |
| 21 | + |
| 22 | + // Get the commit SHA for this version tag |
| 23 | + const commitSha = await $`git rev-parse ${version}`.text() |
| 24 | + const sha = commitSha.trim() |
| 25 | + console.log(`🔍 Found commit SHA: ${sha}`) |
| 26 | + |
| 27 | + // Read the extension.toml from this commit to verify version |
| 28 | + const extensionToml = await $`git show ${version}:packages/extensions/zed/extension.toml`.text() |
| 29 | + const parsed = Bun.TOML.parse(extensionToml) as { version: string } |
| 30 | + const extensionVersion = parsed.version |
| 31 | + |
| 32 | + if (extensionVersion !== cleanVersion) { |
| 33 | + throw new Error(`Version mismatch: extension.toml has ${extensionVersion} but tag is ${cleanVersion}`) |
| 34 | + } |
| 35 | + console.log(`✅ Version ${extensionVersion} matches tag`) |
| 36 | + |
| 37 | + // Clone the fork to a temp directory |
| 38 | + const workDir = join(tmpdir(), `zed-extensions-${Date.now()}`) |
| 39 | + console.log(`📁 Working in ${workDir}`) |
| 40 | + |
| 41 | + await $`git clone https://x-access-token:${token}@github.com/${FORK_REPO}.git ${workDir}` |
| 42 | + process.chdir(workDir) |
| 43 | + |
| 44 | + // Sync fork with upstream |
| 45 | + console.log(`🔄 Syncing fork with upstream...`) |
| 46 | + await $`git remote add upstream https://github.com/${UPSTREAM_REPO}.git` |
| 47 | + await $`git fetch upstream` |
| 48 | + await $`git checkout main` |
| 49 | + await $`git merge upstream/main --ff-only` |
| 50 | + await $`git push origin main` |
| 51 | + console.log(`✅ Fork synced`) |
| 52 | + |
| 53 | + // Create a new branch |
| 54 | + const branchName = `update-${EXTENSION_NAME}-${cleanVersion}` |
| 55 | + console.log(`🌿 Creating branch ${branchName}`) |
| 56 | + await $`git checkout -b ${branchName}` |
| 57 | + |
| 58 | + // Update the submodule for opencode extension |
| 59 | + const submodulePath = `extensions/${EXTENSION_NAME}` |
| 60 | + console.log(`📌 Updating submodule to commit ${sha}`) |
| 61 | + await $`git submodule update --init ${submodulePath}` |
| 62 | + process.chdir(submodulePath) |
| 63 | + await $`git fetch` |
| 64 | + await $`git checkout ${sha}` |
| 65 | + process.chdir(workDir) |
| 66 | + await $`git add ${submodulePath}` |
| 67 | + |
| 68 | + // Update extensions.toml |
| 69 | + console.log(`📝 Updating extensions.toml`) |
| 70 | + const extensionsTomlPath = "extensions.toml" |
| 71 | + const extensionsToml = await Bun.file(extensionsTomlPath).text() |
| 72 | + |
| 73 | + // Update version field for opencode extension |
| 74 | + const versionRegex = new RegExp(`(\\[${EXTENSION_NAME}\\]\\s+(?:.*\\s*)?)version = "[^"]+"`) |
| 75 | + const updatedToml = extensionsToml.replace(versionRegex, `$1version = "${cleanVersion}"`) |
| 76 | + |
| 77 | + await Bun.write(extensionsTomlPath, updatedToml) |
| 78 | + await $`git add extensions.toml` |
| 79 | + |
| 80 | + // Commit changes |
| 81 | + const commitMessage = `Update ${EXTENSION_NAME} to v${cleanVersion} |
| 82 | +
|
| 83 | +Release notes: |
| 84 | +
|
| 85 | +https://github.com/${OPENCODE_REPO}/releases/tag/v${cleanVersion}` |
| 86 | + |
| 87 | + await $`git commit -m ${commitMessage}` |
| 88 | + console.log(`✅ Changes committed`) |
| 89 | + |
| 90 | + // Push to fork |
| 91 | + console.log(`🚀 Pushing to fork...`) |
| 92 | + await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git ${branchName}` |
| 93 | + |
| 94 | + // Create PR using gh CLI |
| 95 | + console.log(`📬 Creating pull request...`) |
| 96 | + const prUrl = |
| 97 | + 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() |
| 98 | + |
| 99 | + console.log(`✅ Pull request created: ${prUrl}`) |
| 100 | + console.log(`🎉 Done!`) |
| 101 | +} |
| 102 | + |
| 103 | +main().catch((err) => { |
| 104 | + console.error("❌ Error:", err.message) |
| 105 | + process.exit(1) |
| 106 | +}) |
0 commit comments