lightweight-ai 1.2.12 → 1.2.15

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.
package/bin/lightweight CHANGED
@@ -160,6 +160,10 @@ function findBinary(startDir) {
160
160
  }
161
161
  }
162
162
 
163
+ // Check for binary downloaded by postinstall (in cli/ directory)
164
+ const localBinary = path.join(scriptDir, "..", "cli", binary)
165
+ if (fs.existsSync(localBinary)) run(localBinary)
166
+
163
167
  const resolved = findBinary(scriptDir)
164
168
  if (!resolved) {
165
169
  console.error(
package/package.json CHANGED
@@ -1,24 +1,11 @@
1
1
  {
2
2
  "name": "lightweight-ai",
3
- "version": "1.2.12",
3
+ "version": "1.2.15",
4
4
  "bin": {
5
5
  "lightweight": "./bin/lightweight"
6
6
  },
7
7
  "scripts": {
8
8
  "postinstall": "node postinstall.mjs"
9
9
  },
10
- "optionalDependencies": {
11
- "lightweight-ai-darwin-arm64": "1.2.12",
12
- "lightweight-ai-darwin-x64-baseline": "1.2.12",
13
- "lightweight-ai-darwin-x64": "1.2.12",
14
- "lightweight-ai-linux-arm64-musl": "1.2.12",
15
- "lightweight-ai-linux-arm64": "1.2.12",
16
- "lightweight-ai-linux-x64-baseline-musl": "1.2.12",
17
- "lightweight-ai-linux-x64-baseline": "1.2.12",
18
- "lightweight-ai-linux-x64-musl": "1.2.12",
19
- "lightweight-ai-linux-x64": "1.2.12",
20
- "lightweight-ai-windows-x64-baseline": "1.2.12",
21
- "lightweight-ai-windows-x64": "1.2.12"
22
- },
23
10
  "license": "MIT"
24
11
  }
package/postinstall.mjs CHANGED
@@ -1,125 +1,99 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import fs from "fs"
4
- import path from "path"
5
- import os from "os"
6
- import { fileURLToPath } from "url"
7
- import { createRequire } from "module"
8
-
9
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
10
- const require = createRequire(import.meta.url)
11
-
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
- }
3
+ // Downloads the lightweight CLI binary from GitHub Releases.
4
+ // This avoids publishing 11 x 57MB platform packages to npm.
29
5
 
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 }
6
+ import { existsSync, mkdirSync, createWriteStream, unlinkSync, chmodSync, readFileSync } from "fs"
7
+ import { join, dirname } from "path"
8
+ 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)
48
22
  }
49
23
 
50
- function findBinary() {
51
- const { platform, arch } = detectPlatformAndArch()
52
- const packageName = `lightweight-ai-${platform}-${arch}`
53
- const binaryName = platform === "windows" ? "lightweight.exe" : "lightweight"
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)
54
29
 
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)
30
+ if (existsSync(binaryPath)) {
31
+ console.log("lightweight: binary already installed")
32
+ process.exit(0)
33
+ }
60
34
 
61
- if (!fs.existsSync(binaryPath)) {
62
- throw new Error(`Binary not found at ${binaryPath}`)
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)
63
65
  }
64
-
65
- return { binaryPath, binaryName }
66
- } catch (error) {
67
- throw new Error(`Could not find package ${packageName}: ${error.message}`)
68
- }
66
+ follow(url)
67
+ })
69
68
  }
70
69
 
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 })
78
- }
79
-
80
- // Remove existing binary/symlink if it exists
81
- if (fs.existsSync(targetPath)) {
82
- fs.unlinkSync(targetPath)
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" })
83
79
  }
84
80
 
85
- return { binDir, targetPath }
86
- }
81
+ if (existsSync(archive)) unlinkSync(archive)
87
82
 
88
- function symlinkBinary(sourcePath, binaryName) {
89
- const { targetPath } = prepareBinDirectory(binaryName)
90
-
91
- fs.symlinkSync(sourcePath, targetPath)
92
- console.log(`lightweight 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}`)
83
+ if (!isWindows && existsSync(binaryPath)) {
84
+ chmodSync(binaryPath, 0o755)
97
85
  }
98
- }
99
86
 
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
- console.log(`Platform binary verified at: ${binaryPath}`)
113
- console.log("Wrapper script will handle binary execution")
114
- } catch (error) {
115
- console.error("Failed to setup lightweight binary:", error.message)
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}`)
116
92
  process.exit(1)
117
93
  }
118
- }
119
-
120
- try {
121
- main()
122
94
  } catch (error) {
123
- console.error("Postinstall script error:", error.message)
124
- process.exit(0)
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)
125
99
  }