publish.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. const dry = process.argv.includes("--dry")
  5. const snapshot = process.argv.includes("--snapshot")
  6. const version = snapshot
  7. ? `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  8. : await $`git describe --tags --exact-match HEAD`
  9. .text()
  10. .then((x) => x.substring(1).trim())
  11. .catch(() => {
  12. console.error("tag not found")
  13. process.exit(1)
  14. })
  15. console.log(`publishing ${version}`)
  16. const GOARCH: Record<string, string> = {
  17. arm64: "arm64",
  18. x64: "amd64",
  19. }
  20. const targets = [
  21. ["linux", "arm64"],
  22. ["linux", "x64"],
  23. ["darwin", "x64"],
  24. ["darwin", "arm64"],
  25. ["windows", "x64"],
  26. ]
  27. await $`rm -rf dist`
  28. const optionalDependencies: Record<string, string> = {}
  29. const npmTag = snapshot ? "snapshot" : "latest"
  30. for (const [os, arch] of targets) {
  31. console.log(`building ${os}-${arch}`)
  32. const name = `${pkg.name}-${os}-${arch}`
  33. await $`mkdir -p dist/${name}/bin`
  34. await $`GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X main.Version=${version}" -o ../opencode/dist/${name}/bin/tui ../tui/cmd/opencode/main.go`.cwd(
  35. "../tui",
  36. )
  37. await $`bun build --define OPENCODE_VERSION="'${version}'" --compile --minify --target=bun-${os}-${arch} --outfile=dist/${name}/bin/opencode ./src/index.ts ./dist/${name}/bin/tui`
  38. await $`rm -rf ./dist/${name}/bin/tui`
  39. await Bun.file(`dist/${name}/package.json`).write(
  40. JSON.stringify(
  41. {
  42. name,
  43. version,
  44. os: [os === "windows" ? "win32" : os],
  45. cpu: [arch],
  46. },
  47. null,
  48. 2,
  49. ),
  50. )
  51. if (!dry)
  52. await $`cd dist/${name} && npm publish --access public --tag ${npmTag}`
  53. optionalDependencies[name] = version
  54. }
  55. await $`mkdir -p ./dist/${pkg.name}`
  56. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  57. await $`cp ./script/postinstall.js ./dist/${pkg.name}/postinstall.js`
  58. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  59. JSON.stringify(
  60. {
  61. name: pkg.name + "-ai",
  62. bin: {
  63. [pkg.name]: `./bin/${pkg.name}`,
  64. },
  65. scripts: {
  66. postinstall: "node ./postinstall.js",
  67. },
  68. version,
  69. optionalDependencies,
  70. },
  71. null,
  72. 2,
  73. ),
  74. )
  75. if (!dry)
  76. await $`cd ./dist/${pkg.name} && npm publish --access public --tag ${npmTag}`
  77. if (!snapshot) {
  78. for (const key of Object.keys(optionalDependencies)) {
  79. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  80. }
  81. const previous = await fetch(
  82. "https://api.github.com/repos/sst/opencode/releases/latest",
  83. )
  84. .then((res) => res.json())
  85. .then((data) => data.tag_name)
  86. const commits = await fetch(
  87. `https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`,
  88. )
  89. .then((res) => res.json())
  90. .then((data) => data.commits || [])
  91. const notes = commits
  92. .map((commit: any) => `- ${commit.commit.message.split("\n")[0]}`)
  93. .filter((x: string) => {
  94. const lower = x.toLowerCase()
  95. return (
  96. !lower.includes("chore:") &&
  97. !lower.includes("ci:") &&
  98. !lower.includes("docs:")
  99. )
  100. })
  101. .join("\n")
  102. if (!dry)
  103. await $`gh release create v${version} --title "v${version}" --notes ${notes} ./dist/*.zip`
  104. }