publish.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { Glob } from "bun"
  4. import pkg from "../package.json"
  5. const dry = process.argv.includes("--dry")
  6. const snapshot = process.argv.includes("--snapshot")
  7. const version = snapshot
  8. ? `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  9. : await $`git describe --tags --exact-match HEAD`
  10. .text()
  11. .then((x) => x.substring(1).trim())
  12. .catch(() => {
  13. console.error("tag not found")
  14. process.exit(1)
  15. })
  16. console.log(`publishing ${version}`)
  17. const GOARCH: Record<string, string> = {
  18. arm64: "arm64",
  19. x64: "amd64",
  20. }
  21. const targets = [
  22. ["linux", "arm64"],
  23. ["linux", "x64"],
  24. ["darwin", "x64"],
  25. ["darwin", "arm64"],
  26. ["windows", "x64"],
  27. ]
  28. await $`rm -rf dist`
  29. const optionalDependencies: Record<string, string> = {}
  30. const npmTag = snapshot ? "snapshot" : "latest"
  31. for (const [os, arch] of targets) {
  32. console.log(`building ${os}-${arch}`)
  33. const name = `${pkg.name}-${os}-${arch}`
  34. await $`mkdir -p dist/${name}/bin`
  35. 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(
  36. "../tui",
  37. )
  38. 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`
  39. await $`rm -rf ./dist/${name}/bin/tui`
  40. await Bun.file(`dist/${name}/package.json`).write(
  41. JSON.stringify(
  42. {
  43. name,
  44. version,
  45. os: [os === "windows" ? "win32" : os],
  46. cpu: [arch],
  47. },
  48. null,
  49. 2,
  50. ),
  51. )
  52. if (!dry)
  53. await $`cd dist/${name} && npm publish --access public --tag ${npmTag}`
  54. optionalDependencies[name] = version
  55. }
  56. await $`mkdir -p ./dist/${pkg.name}`
  57. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  58. await $`cp ./script/postinstall.js ./dist/${pkg.name}/postinstall.js`
  59. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  60. JSON.stringify(
  61. {
  62. name: pkg.name + "-ai",
  63. bin: {
  64. [pkg.name]: `./bin/${pkg.name}`,
  65. },
  66. scripts: {
  67. postinstall: "node ./postinstall.js",
  68. },
  69. version,
  70. optionalDependencies,
  71. },
  72. null,
  73. 2,
  74. ),
  75. )
  76. if (!dry)
  77. await $`cd ./dist/${pkg.name} && npm publish --access public --tag ${npmTag}`
  78. for (const key of Object.keys(optionalDependencies)) {
  79. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  80. }
  81. // Upload to GitHub releases
  82. const files = Object.keys(optionalDependencies)
  83. .map((key) => `dist/${key}.zip`)
  84. .join(" ")
  85. await $`gh release create v${version} ${files} --title "Release v${version}" --generate-notes`