@onescience/onecode 1.14.50-202606300908 → 1.14.50-202607010646

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -5
  2. package/postinstall.mjs +94 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onescience/onecode",
3
- "version": "1.14.50-202606300908",
3
+ "version": "1.14.50-202607010646",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "OneScience AI coding agent for the terminal.",
@@ -8,9 +8,6 @@
8
8
  "onecode": "./bin/onecode"
9
9
  },
10
10
  "scripts": {
11
- "postinstall": "bun ./postinstall.mjs || node ./postinstall.mjs"
12
- },
13
- "optionalDependencies": {
14
- "onecode-linux-x64": "1.14.50-202606300908"
11
+ "postinstall": "node ./postinstall.mjs"
15
12
  }
16
13
  }
package/postinstall.mjs CHANGED
@@ -3,12 +3,13 @@
3
3
  import fs from "fs"
4
4
  import path from "path"
5
5
  import os from "os"
6
+ import https from "https"
6
7
  import { fileURLToPath } from "url"
7
- import { createRequire } from "module"
8
+ import { execSync } from "child_process"
8
9
 
9
10
  const __dirname = path.dirname(fileURLToPath(import.meta.url))
10
- const require = createRequire(import.meta.url)
11
11
  const product = "onecode"
12
+ const DEFAULT_TGZ_BASE = "https://218.90.133.98:4443/onecode_tgz"
12
13
 
13
14
  function detectPlatformAndArch() {
14
15
  let platform
@@ -40,30 +41,99 @@ function detectPlatformAndArch() {
40
41
  break
41
42
  default:
42
43
  arch = os.arch()
43
- break
44
44
  }
45
45
 
46
46
  return { platform, arch }
47
47
  }
48
48
 
49
- function findBinary() {
49
+ function ownVersion() {
50
+ return JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8")).version
51
+ }
52
+
53
+ function platformPackageName() {
50
54
  const { platform, arch } = detectPlatformAndArch()
51
- const packageName = `${product}-${platform}-${arch}`
52
- const binaryName = platform === "windows" ? `${product}.exe` : product
55
+ return `${product}-${platform}-${arch}`
56
+ }
53
57
 
54
- try {
55
- const packageJsonPath = require.resolve(`${packageName}/package.json`)
56
- const packageDir = path.dirname(packageJsonPath)
57
- const binaryPath = path.join(packageDir, "bin", binaryName)
58
+ function platformTgzUrl(version) {
59
+ const base = (process.env.ONECODE_TGZ_DOWNLOAD_BASE || DEFAULT_TGZ_BASE).replace(/\/$/, "")
60
+ const dir = encodeURIComponent(`onecode-${version}`)
61
+ const file = encodeURIComponent(`onecode-linux-x64-${version}.tgz`)
62
+ return `${base}/${dir}/${file}`
63
+ }
58
64
 
59
- if (!fs.existsSync(binaryPath)) {
60
- throw new Error(`Binary not found at ${binaryPath}`)
61
- }
65
+ function downloadFile(url, dest) {
66
+ return new Promise((resolve, reject) => {
67
+ const file = fs.createWriteStream(dest)
68
+ const req = https.get(url, { rejectUnauthorized: false }, (res) => {
69
+ if (res.statusCode === 301 || res.statusCode === 302) {
70
+ file.close()
71
+ fs.unlinkSync(dest)
72
+ downloadFile(res.headers.location, dest).then(resolve).catch(reject)
73
+ return
74
+ }
75
+ if (res.statusCode !== 200) {
76
+ file.close()
77
+ fs.unlinkSync(dest)
78
+ reject(new Error(`HTTP ${res.statusCode} for ${url}`))
79
+ return
80
+ }
81
+ res.pipe(file)
82
+ file.on("finish", () => file.close(resolve))
83
+ })
84
+ req.on("error", (err) => {
85
+ file.close()
86
+ if (fs.existsSync(dest)) fs.unlinkSync(dest)
87
+ reject(err)
88
+ })
89
+ })
90
+ }
62
91
 
63
- return { binaryPath, binaryName }
64
- } catch (error) {
65
- throw new Error(`Could not find package ${packageName}: ${error.message}`, { cause: error })
92
+ async function downloadAndExtractPlatformPackage() {
93
+ const packageName = platformPackageName()
94
+ const version = ownVersion()
95
+ const url = platformTgzUrl(version)
96
+ const nodeModulesDir = path.join(__dirname, "node_modules")
97
+ const targetDir = path.join(nodeModulesDir, packageName)
98
+ const tmpTgz = path.join(os.tmpdir(), `${packageName}-${version}.tgz`)
99
+ const tmpExtract = path.join(os.tmpdir(), `${packageName}-${version}-extract`)
100
+
101
+ console.log(`Downloading ${packageName} from ${url}`)
102
+ await downloadFile(url, tmpTgz)
103
+ console.log(`Downloaded ${path.basename(tmpTgz)} (${(fs.statSync(tmpTgz).size / 1024 / 1024).toFixed(1)} MB)`)
104
+
105
+ fs.rmSync(tmpExtract, { recursive: true, force: true })
106
+ fs.mkdirSync(tmpExtract, { recursive: true })
107
+ execSync(`tar -xzf "${tmpTgz}"`, { cwd: tmpExtract, stdio: "pipe" })
108
+ fs.unlinkSync(tmpTgz)
109
+
110
+ const extractedRoot = path.join(tmpExtract, "package")
111
+ if (!fs.existsSync(extractedRoot)) {
112
+ throw new Error(`Expected package/ inside tgz downloaded from ${url}`)
113
+ }
114
+
115
+ fs.rmSync(targetDir, { recursive: true, force: true })
116
+ fs.mkdirSync(nodeModulesDir, { recursive: true })
117
+ fs.renameSync(extractedRoot, targetDir)
118
+ fs.rmSync(tmpExtract, { recursive: true, force: true })
119
+
120
+ const binaryName = detectPlatformAndArch().platform === "windows" ? `${product}.exe` : product
121
+ const binaryPath = path.join(targetDir, "bin", binaryName)
122
+ if (!fs.existsSync(binaryPath)) {
123
+ throw new Error(`Binary not found at ${binaryPath} after extraction`)
66
124
  }
125
+ fs.chmodSync(binaryPath, 0o755)
126
+
127
+ return { binaryPath, binaryName }
128
+ }
129
+
130
+ function findExistingPlatformBinary() {
131
+ const packageName = platformPackageName()
132
+ const binaryName = detectPlatformAndArch().platform === "windows" ? `${product}.exe` : product
133
+ const targetDir = path.join(__dirname, "node_modules", packageName)
134
+ const binaryPath = path.join(targetDir, "bin", binaryName)
135
+ if (!fs.existsSync(binaryPath)) return null
136
+ return { binaryPath, binaryName }
67
137
  }
68
138
 
69
139
  async function main() {
@@ -73,15 +143,20 @@ async function main() {
73
143
  return
74
144
  }
75
145
 
76
- const { binaryPath } = findBinary()
146
+ let result = findExistingPlatformBinary()
147
+ if (!result) {
148
+ result = await downloadAndExtractPlatformPackage()
149
+ }
150
+
77
151
  const target = path.join(__dirname, "bin", `.${product}`)
78
152
  if (fs.existsSync(target)) fs.unlinkSync(target)
79
153
  try {
80
- fs.linkSync(binaryPath, target)
154
+ fs.linkSync(result.binaryPath, target)
81
155
  } catch {
82
- fs.copyFileSync(binaryPath, target)
156
+ fs.copyFileSync(result.binaryPath, target)
83
157
  }
84
158
  fs.chmodSync(target, 0o755)
159
+ console.log(`${product} binary ready`)
85
160
  } catch (error) {
86
161
  console.error(`Failed to setup ${product} binary:`, error.message)
87
162
  process.exit(1)