lightweight-ai 1.2.19 → 1.2.20

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 +1 -1
  2. package/postinstall.mjs +112 -80
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lightweight-ai",
3
- "version": "1.2.19",
3
+ "version": "1.2.20",
4
4
  "bin": {
5
5
  "lightweight": "./bin/lightweight"
6
6
  },
package/postinstall.mjs CHANGED
@@ -1,99 +1,131 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // Downloads the lightweight CLI binary from GitHub Releases.
4
- // This avoids publishing 11 x 57MB platform packages to npm.
5
-
6
- import { existsSync, mkdirSync, createWriteStream, unlinkSync, chmodSync, readFileSync } from "fs"
7
- import { join, dirname } from "path"
3
+ import fs from "fs"
4
+ import path from "path"
5
+ import os from "os"
8
6
  import { fileURLToPath } from "url"
9
- import { execSync } from "child_process"
10
- import https from "https"
11
- import http from "http"
12
-
13
- const root = dirname(fileURLToPath(import.meta.url))
14
- const platforms = { darwin: "darwin", linux: "linux", win32: "windows" }
15
- const archs = { x64: "x64", arm64: "arm64" }
16
- const platform = platforms[process.platform]
17
- const arch = archs[process.arch]
18
-
19
- if (!platform || !arch) {
20
- console.log(`lightweight: unsupported platform ${process.platform}-${process.arch}`)
21
- process.exit(0)
22
- }
7
+ import { createRequire } from "module"
23
8
 
24
- const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"))
25
- const isWindows = process.platform === "win32"
26
- const binary = isWindows ? "lightweight.exe" : "lightweight"
27
- const cliDir = join(root, "cli")
28
- const binaryPath = join(cliDir, binary)
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
10
+ const require = createRequire(import.meta.url)
29
11
 
30
- if (existsSync(binaryPath)) {
31
- console.log("lightweight: binary already installed")
32
- process.exit(0)
12
+ function detectPlatformAndArch() {
13
+ // Map platform names
14
+ let platform
15
+ switch (os.platform()) {
16
+ case "darwin":
17
+ platform = "darwin"
18
+ break
19
+ case "linux":
20
+ platform = "linux"
21
+ break
22
+ case "win32":
23
+ platform = "windows"
24
+ break
25
+ default:
26
+ platform = os.platform()
27
+ break
28
+ }
29
+
30
+ // Map architecture names
31
+ let arch
32
+ switch (os.arch()) {
33
+ case "x64":
34
+ arch = "x64"
35
+ break
36
+ case "arm64":
37
+ arch = "arm64"
38
+ break
39
+ case "arm":
40
+ arch = "arm"
41
+ break
42
+ default:
43
+ arch = os.arch()
44
+ break
45
+ }
46
+
47
+ return { platform, arch }
33
48
  }
34
49
 
35
- const ext = isWindows ? "zip" : "tar.gz"
36
- const url = `https://github.com/templarsco/lightweight/releases/download/v${pkg.version}/lightweight-${platform}-${arch}.${ext}`
37
- const archive = join(root, `lightweight.${ext}`)
38
-
39
- console.log(`lightweight: downloading ${platform}-${arch} binary...`)
40
- mkdirSync(cliDir, { recursive: true })
41
-
42
- function download(url, dest) {
43
- return new Promise((resolve, reject) => {
44
- const follow = (url) => {
45
- const client = url.startsWith("https") ? https : http
46
- client
47
- .get(url, (res) => {
48
- if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
49
- follow(res.headers.location)
50
- return
51
- }
52
- if (res.statusCode !== 200) {
53
- reject(new Error(`HTTP ${res.statusCode}`))
54
- return
55
- }
56
- const stream = createWriteStream(dest)
57
- res.pipe(stream)
58
- stream.on("finish", () => {
59
- stream.close()
60
- resolve()
61
- })
62
- stream.on("error", reject)
63
- })
64
- .on("error", reject)
50
+ function findBinary() {
51
+ const { platform, arch } = detectPlatformAndArch()
52
+ const packageName = `opencode-${platform}-${arch}`
53
+ const binaryName = platform === "windows" ? "opencode.exe" : "opencode"
54
+
55
+ try {
56
+ // Use require.resolve to find the package
57
+ const packageJsonPath = require.resolve(`${packageName}/package.json`)
58
+ const packageDir = path.dirname(packageJsonPath)
59
+ const binaryPath = path.join(packageDir, "bin", binaryName)
60
+
61
+ if (!fs.existsSync(binaryPath)) {
62
+ throw new Error(`Binary not found at ${binaryPath}`)
65
63
  }
66
- follow(url)
67
- })
64
+
65
+ return { binaryPath, binaryName }
66
+ } catch (error) {
67
+ throw new Error(`Could not find package ${packageName}: ${error.message}`)
68
+ }
68
69
  }
69
70
 
70
- try {
71
- await download(url, archive)
72
-
73
- if (isWindows) {
74
- execSync(`powershell -NoProfile -Command "Expand-Archive -Path '${archive}' -DestinationPath '${cliDir}' -Force"`, {
75
- stdio: "inherit",
76
- })
77
- } else {
78
- execSync(`tar -xzf "${archive}" -C "${cliDir}"`, { stdio: "inherit" })
71
+ function prepareBinDirectory(binaryName) {
72
+ const binDir = path.join(__dirname, "bin")
73
+ const targetPath = path.join(binDir, binaryName)
74
+
75
+ // Ensure bin directory exists
76
+ if (!fs.existsSync(binDir)) {
77
+ fs.mkdirSync(binDir, { recursive: true })
79
78
  }
80
79
 
81
- if (existsSync(archive)) unlinkSync(archive)
80
+ // Remove existing binary/symlink if it exists
81
+ if (fs.existsSync(targetPath)) {
82
+ fs.unlinkSync(targetPath)
83
+ }
84
+
85
+ return { binDir, targetPath }
86
+ }
87
+
88
+ function symlinkBinary(sourcePath, binaryName) {
89
+ const { targetPath } = prepareBinDirectory(binaryName)
82
90
 
83
- if (!isWindows && existsSync(binaryPath)) {
84
- chmodSync(binaryPath, 0o755)
91
+ fs.symlinkSync(sourcePath, targetPath)
92
+ console.log(`opencode binary symlinked: ${targetPath} -> ${sourcePath}`)
93
+
94
+ // Verify the file exists after operation
95
+ if (!fs.existsSync(targetPath)) {
96
+ throw new Error(`Failed to symlink binary to ${targetPath}`)
85
97
  }
98
+ }
86
99
 
87
- if (existsSync(binaryPath)) {
88
- console.log("lightweight: installed successfully")
89
- } else {
90
- console.error("lightweight: binary not found after extraction")
91
- console.error(`lightweight: download manually from ${url}`)
100
+ async function main() {
101
+ try {
102
+ if (os.platform() === "win32") {
103
+ // On Windows, the .exe is already included in the package and bin field points to it
104
+ // No postinstall setup needed
105
+ console.log("Windows detected: binary setup not needed (using packaged .exe)")
106
+ return
107
+ }
108
+
109
+ // On non-Windows platforms, just verify the binary package exists
110
+ // Don't replace the wrapper script - it handles binary execution
111
+ const { binaryPath } = findBinary()
112
+ const target = path.join(__dirname, "bin", ".opencode")
113
+ if (fs.existsSync(target)) fs.unlinkSync(target)
114
+ try {
115
+ fs.linkSync(binaryPath, target)
116
+ } catch {
117
+ fs.copyFileSync(binaryPath, target)
118
+ }
119
+ fs.chmodSync(target, 0o755)
120
+ } catch (error) {
121
+ console.error("Failed to setup opencode binary:", error.message)
92
122
  process.exit(1)
93
123
  }
124
+ }
125
+
126
+ try {
127
+ main()
94
128
  } catch (error) {
95
- console.error(`lightweight: download failed: ${error.message}`)
96
- console.error(`lightweight: download manually from ${url}`)
97
- if (existsSync(archive)) unlinkSync(archive)
98
- process.exit(1)
129
+ console.error("Postinstall script error:", error.message)
130
+ process.exit(0)
99
131
  }