postinstall.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env node
  2. import fs from "fs"
  3. import path from "path"
  4. import os from "os"
  5. import { fileURLToPath } from "url"
  6. import { createRequire } from "module"
  7. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  8. const require = createRequire(import.meta.url)
  9. function detectPlatformAndArch() {
  10. // Map platform names
  11. let platform
  12. switch (os.platform()) {
  13. case "darwin":
  14. platform = "darwin"
  15. break
  16. case "linux":
  17. platform = "linux"
  18. break
  19. case "win32":
  20. platform = "windows"
  21. break
  22. default:
  23. platform = os.platform()
  24. break
  25. }
  26. // Map architecture names
  27. let arch
  28. switch (os.arch()) {
  29. case "x64":
  30. arch = "x64"
  31. break
  32. case "arm64":
  33. arch = "arm64"
  34. break
  35. case "arm":
  36. arch = "arm"
  37. break
  38. default:
  39. arch = os.arch()
  40. break
  41. }
  42. return { platform, arch }
  43. }
  44. function findBinary() {
  45. const { platform, arch } = detectPlatformAndArch()
  46. const packageName = `opencode-${platform}-${arch}`
  47. const binaryName = platform === "windows" ? "opencode.exe" : "opencode"
  48. try {
  49. // Use require.resolve to find the package
  50. const packageJsonPath = require.resolve(`${packageName}/package.json`)
  51. const packageDir = path.dirname(packageJsonPath)
  52. const binaryPath = path.join(packageDir, "bin", binaryName)
  53. if (!fs.existsSync(binaryPath)) {
  54. throw new Error(`Binary not found at ${binaryPath}`)
  55. }
  56. return { binaryPath, binaryName }
  57. } catch (error) {
  58. throw new Error(`Could not find package ${packageName}: ${error.message}`, { cause: error })
  59. }
  60. }
  61. async function main() {
  62. try {
  63. if (os.platform() === "win32") {
  64. // On Windows, the .exe is already included in the package and bin field points to it
  65. // No postinstall setup needed
  66. console.log("Windows detected: binary setup not needed (using packaged .exe)")
  67. return
  68. }
  69. // On non-Windows platforms, just verify the binary package exists
  70. // Don't replace the wrapper script - it handles binary execution
  71. const { binaryPath } = findBinary()
  72. const target = path.join(__dirname, "bin", ".opencode")
  73. if (fs.existsSync(target)) fs.unlinkSync(target)
  74. try {
  75. fs.linkSync(binaryPath, target)
  76. } catch {
  77. fs.copyFileSync(binaryPath, target)
  78. }
  79. fs.chmodSync(target, 0o755)
  80. } catch (error) {
  81. console.error("Failed to setup opencode binary:", error.message)
  82. process.exit(1)
  83. }
  84. }
  85. try {
  86. void main()
  87. } catch (error) {
  88. console.error("Postinstall script error:", error.message)
  89. process.exit(0)
  90. }