130 lines · 3.3 KB · JavaScript
RawDownload
#!/usr/bin/env node import fs from "fs"import path from "path"import os from "os"import { fileURLToPath } from "url"import { createRequire } from "module" const __dirname = path.dirname(fileURLToPath(import.meta.url))const require = createRequire(import.meta.url) function detectPlatformAndArch() {  // Map platform names  let platform  switch (os.platform()) {    case "darwin":      platform = "darwin"      break    case "linux":      platform = "linux"      break    case "win32":      platform = "windows"      break    default:      platform = os.platform()      break  }   // Map architecture names  let arch  switch (os.arch()) {    case "x64":      arch = "x64"      break    case "arm64":      arch = "arm64"      break    case "arm":      arch = "arm"      break    default:      arch = os.arch()      break  }   return { platform, arch }} function findBinary() {  const { platform, arch } = detectPlatformAndArch()  const binaryName = platform === "windows" ? "openscience.exe" : "openscience"   // Try scoped package first (@synsci/openscience-darwin-arm64), then unscoped (openscience-darwin-arm64)  const packageNames = [`@synsci/openscience-${platform}-${arch}`, `openscience-${platform}-${arch}`]   for (const packageName of packageNames) {    try {      const packageJsonPath = require.resolve(`${packageName}/package.json`)      const packageDir = path.dirname(packageJsonPath)      const binaryPath = path.join(packageDir, "bin", binaryName)       if (!fs.existsSync(binaryPath)) {        continue      }       return { binaryPath, binaryName }    } catch {      continue    }  }   throw new Error(`Could not find platform binary package for ${platform}-${arch}`)} function prepareBinDirectory(binaryName) {  const binDir = path.join(__dirname, "bin")  const targetPath = path.join(binDir, binaryName)   // Ensure bin directory exists  if (!fs.existsSync(binDir)) {    fs.mkdirSync(binDir, { recursive: true })  }   // Remove existing binary/symlink if it exists  if (fs.existsSync(targetPath)) {    fs.unlinkSync(targetPath)  }   return { binDir, targetPath }} function symlinkBinary(sourcePath, binaryName) {  const { targetPath } = prepareBinDirectory(binaryName)   fs.symlinkSync(sourcePath, targetPath)  console.log(`openscience binary symlinked: ${targetPath} -> ${sourcePath}`)   // Verify the file exists after operation  if (!fs.existsSync(targetPath)) {    throw new Error(`Failed to symlink binary to ${targetPath}`)  }} async function main() {  try {    if (os.platform() === "win32") {      // On Windows, the .exe is already included in the package and bin field points to it      // No postinstall setup needed      console.log("Windows detected: binary setup not needed (using packaged .exe)")      return    }     // On non-Windows platforms, just verify the binary package exists    // Don't replace the wrapper script - it handles binary execution    const { binaryPath } = findBinary()    console.log(`Platform binary verified at: ${binaryPath}`)    console.log("Wrapper script will handle binary execution")  } catch (error) {    console.error("Failed to setup openscience binary:", error.message)    process.exit(1)  }} try {  main()} catch (error) {  console.error("Postinstall script error:", error.message)  process.exit(0)}