postinstall.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 binary = 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", binary)
  53. if (!fs.existsSync(binaryPath)) {
  54. throw new Error(`Binary not found at ${binaryPath}`)
  55. }
  56. return binaryPath
  57. } catch (error) {
  58. throw new Error(`Could not find package ${packageName}: ${error.message}`)
  59. }
  60. }
  61. async function regenerateWindowsCmdWrappers() {
  62. console.log("Windows + npm detected: Forcing npm to rebuild bin links")
  63. try {
  64. const { execSync } = require("child_process")
  65. const pkgPath = path.join(__dirname, "..")
  66. // npm_config_global is string | undefined
  67. // if it exists, the value is true
  68. const isGlobal = process.env.npm_config_global === "true" || pkgPath.includes(path.join("npm", "node_modules"))
  69. // The npm rebuild command does 2 things - Execute lifecycle scripts and rebuild bin links
  70. // We want to skip lifecycle scripts to avoid infinite loops, so we use --ignore-scripts
  71. const cmd = `npm rebuild opencode-ai --ignore-scripts${isGlobal ? " -g" : ""}`
  72. const opts = {
  73. stdio: "inherit",
  74. shell: true,
  75. ...(isGlobal ? {} : { cwd: path.join(pkgPath, "..", "..") }), // For local, run from project root
  76. }
  77. console.log(`Running: ${cmd}`)
  78. execSync(cmd, opts)
  79. console.log("Successfully rebuilt npm bin links")
  80. } catch (error) {
  81. console.error("Error rebuilding npm links:", error.message)
  82. console.error("npm rebuild failed. You may need to manually run: npm rebuild opencode-ai --ignore-scripts")
  83. }
  84. }
  85. async function main() {
  86. try {
  87. if (os.platform() === "win32") {
  88. // NPM eg format - npm/11.4.2 node/v24.4.1 win32 x64
  89. // Bun eg format - bun/1.2.19 npm/? node/v24.3.0 win32 x64
  90. if (process.env.npm_config_user_agent.startsWith("npm")) {
  91. await regenerateWindowsCmdWrappers()
  92. } else {
  93. console.log("Windows detected but not npm, skipping postinstall")
  94. }
  95. return
  96. }
  97. const binaryPath = findBinary()
  98. const binScript = path.join(__dirname, "bin", "opencode")
  99. // Remove existing bin script if it exists
  100. if (fs.existsSync(binScript)) {
  101. fs.unlinkSync(binScript)
  102. }
  103. // Create symlink to the actual binary
  104. fs.symlinkSync(binaryPath, binScript)
  105. console.log(`opencode binary symlinked: ${binScript} -> ${binaryPath}`)
  106. } catch (error) {
  107. console.error("Failed to create opencode binary symlink:", error.message)
  108. process.exit(1)
  109. }
  110. }
  111. try {
  112. main()
  113. } catch (error) {
  114. console.error("Postinstall script error:", error.message)
  115. process.exit(0)
  116. }