postinstall.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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}`)
  59. }
  60. }
  61. function prepareBinDirectory(binaryName) {
  62. const binDir = path.join(__dirname, "bin")
  63. const targetPath = path.join(binDir, binaryName)
  64. // Ensure bin directory exists
  65. if (!fs.existsSync(binDir)) {
  66. fs.mkdirSync(binDir, { recursive: true })
  67. }
  68. // Remove existing binary/symlink if it exists
  69. if (fs.existsSync(targetPath)) {
  70. fs.unlinkSync(targetPath)
  71. }
  72. return { binDir, targetPath }
  73. }
  74. function symlinkBinary(sourcePath, binaryName) {
  75. const { targetPath } = prepareBinDirectory(binaryName)
  76. fs.symlinkSync(sourcePath, targetPath)
  77. console.log(`opencode binary symlinked: ${targetPath} -> ${sourcePath}`)
  78. // Verify the file exists after operation
  79. if (!fs.existsSync(targetPath)) {
  80. throw new Error(`Failed to symlink binary to ${targetPath}`)
  81. }
  82. }
  83. async function main() {
  84. try {
  85. if (os.platform() === "win32") {
  86. // On Windows, the .exe is already included in the package and bin field points to it
  87. // No postinstall setup needed
  88. console.log("Windows detected: binary setup not needed (using packaged .exe)")
  89. return
  90. }
  91. // On non-Windows platforms, just verify the binary package exists
  92. // Don't replace the wrapper script - it handles binary execution
  93. const { binaryPath } = findBinary()
  94. console.log(`Platform binary verified at: ${binaryPath}`)
  95. console.log("Wrapper script will handle binary execution")
  96. } catch (error) {
  97. console.error("Failed to setup opencode binary:", error.message)
  98. process.exit(1)
  99. }
  100. }
  101. try {
  102. main()
  103. } catch (error) {
  104. console.error("Postinstall script error:", error.message)
  105. process.exit(0)
  106. }