@ekravo/cli 0.0.0-stage → 0.1.113

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.
@@ -0,0 +1,237 @@
1
+ #!/usr/bin/env node
2
+ import { spawnSync } from "node:child_process"
3
+ import {
4
+ chmodSync,
5
+ createWriteStream,
6
+ existsSync,
7
+ mkdirSync,
8
+ mkdtempSync,
9
+ readFileSync,
10
+ rmSync as rmSync2,
11
+ writeFileSync,
12
+ } from "node:fs"
13
+ import { tmpdir } from "node:os"
14
+ import { dirname, join, resolve } from "node:path"
15
+ import { pipeline } from "node:stream/promises"
16
+ import { fileURLToPath } from "node:url"
17
+
18
+ const __dirname = dirname(fileURLToPath(import.meta.url))
19
+ const root = resolve(__dirname, "..")
20
+
21
+ // 1. Run existing dev postinstall steps (patch + copy) — best-effort, never fail install
22
+ try {
23
+ spawnSync(process.execPath, [join(__dirname, "patch-pi-ai-oauth.js")], { stdio: "inherit" })
24
+ } catch {}
25
+ try {
26
+ spawnSync(process.execPath, [join(__dirname, "copy-resources.js"), "--dev"], { stdio: "inherit" })
27
+ } catch {}
28
+
29
+ // 2. Skip download in CI / when binary already present / when explicitly skipped
30
+ if (process.env.EKRAVO_SKIP_POSTINSTALL === "1") {
31
+ console.log("[ekravo] EKRAVO_SKIP_POSTINSTALL=1, skipping binary download")
32
+ process.exit(0)
33
+ }
34
+
35
+ const distBin = join(root, "dist", "bin")
36
+ const isWin = process.platform === "win32"
37
+ const binName = isWin ? "ekravo.exe" : "ekravo"
38
+ const binPath = join(distBin, binName)
39
+
40
+ if (existsSync(binPath)) {
41
+ console.log("[ekravo] binary already present, skipping download")
42
+ process.exit(0)
43
+ }
44
+
45
+ if (process.env.CI === "true" || process.env.CI === "1") {
46
+ console.log("[ekravo] CI detected and no binary, skipping download (CI builds via `pnpm run build:binary`)")
47
+ process.exit(0)
48
+ }
49
+
50
+ // 3. Download prebuilt binary for npm installs (when dist/ not present)
51
+ const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf-8"))
52
+ const version = pkg.version && pkg.version !== "0.0.0" ? `v${pkg.version}` : "latest"
53
+ const repo = "EOSKILLZ/ACHECLI"
54
+
55
+ let os
56
+ if (process.platform === "linux") os = "linux"
57
+ else if (process.platform === "darwin") os = "darwin"
58
+ else if (isWin) os = "windows"
59
+ else {
60
+ console.warn(`[ekravo] unsupported platform ${process.platform}, skipping download`)
61
+ process.exit(0)
62
+ }
63
+
64
+ let arch
65
+ if (process.arch === "x64") arch = "amd64"
66
+ else if (process.arch === "arm64") arch = "arm64"
67
+ else {
68
+ console.warn(`[ekravo] unsupported arch ${process.arch}, skipping download`)
69
+ process.exit(0)
70
+ }
71
+
72
+ const ext = isWin ? "zip" : "tar.gz"
73
+ const asset = `ekravo_${os}_${arch}.${ext}`
74
+
75
+ // The GitHub repo is private, so unauthenticated `github.com/...` release
76
+ // URLs 404. Route through the ekravo.com release proxy (same one the CLI's own
77
+ // auto-update uses, see src/update/github.ts) which re-serves the release
78
+ // data publicly. The proxy does NOT serve `latest/download/<asset>` directly,
79
+ // so resolve the actual tag via its API first, then download the pinned URL.
80
+ const PROXY_API = "https://ekravo.com/cli-releases/api"
81
+ const PROXY_DL = "https://ekravo.com/cli-releases/dl"
82
+ const GITHUB_DL = "https://github.com"
83
+
84
+ async function resolveLatestTag() {
85
+ const url = `${PROXY_API}/repos/${repo}/releases/latest`
86
+ const res = await fetch(url, { headers: { Accept: "application/vnd.github+json" } })
87
+ if (!res.ok) throw new Error(`latest-release API returned HTTP ${res.status}`)
88
+ const json = await res.json()
89
+ if (typeof json.tag_name !== "string" || json.tag_name.length === 0) {
90
+ throw new Error("latest-release API response missing tag_name")
91
+ }
92
+ return json.tag_name
93
+ }
94
+
95
+ // Determine the release tag to download: the exact npm version's tag when it
96
+ // exists, otherwise the latest release (npm and GitHub tags often drift).
97
+ async function resolveTag() {
98
+ if (version === "latest") return resolveLatestTag()
99
+ // Probe whether the pinned tag's asset exists on the proxy.
100
+ const probe = `${PROXY_DL}/${repo}/releases/download/${version}/${asset}`
101
+ const res = await fetch(probe, { method: "HEAD" })
102
+ if (res.ok) return version
103
+ // Pinned tag has no asset (e.g. npm 0.1.98 but GitHub only cut v0.1.97).
104
+ console.warn(`[ekravo] no release asset for ${version}; using latest release`)
105
+ return resolveLatestTag()
106
+ }
107
+
108
+ console.log(`[ekravo] downloading ${asset} (${version}) for ${os}/${arch}`)
109
+
110
+ const tmp = mkdtempSync(join(tmpdir(), "ekravo-postinstall-"))
111
+ const archivePath = join(tmp, asset)
112
+
113
+ async function download(url) {
114
+ const res = await fetch(url, { redirect: "follow" })
115
+ if (!res.ok) throw new Error(`HTTP ${res.status} ${res.statusText} for ${url}`)
116
+ const fileStream = createWriteStream(archivePath)
117
+ await pipeline(res.body, fileStream)
118
+ return url
119
+ }
120
+
121
+ try {
122
+ // Resolve the concrete tag, then download its pinned asset via the proxy
123
+ // (falling back to GitHub directly). Both serve version-pinned assets.
124
+ const tag = await resolveTag()
125
+ let usedUrl
126
+ let lastErr
127
+ for (const base of [PROXY_DL, GITHUB_DL]) {
128
+ const url = `${base}/${repo}/releases/download/${tag}/${asset}`
129
+ try {
130
+ usedUrl = await download(url)
131
+ break
132
+ } catch (e) {
133
+ lastErr = e
134
+ console.warn(`[ekravo] ${url} failed (${e.message})`)
135
+ }
136
+ }
137
+ if (!usedUrl) {
138
+ throw new Error(`all download attempts failed; last: ${lastErr?.message}`)
139
+ }
140
+ console.log(`[ekravo] downloaded to ${archivePath} from ${usedUrl} (tag ${tag})`)
141
+
142
+ // Checksums come from the same base + tag we actually used.
143
+ const checksumBase = usedUrl.startsWith(PROXY_DL) ? PROXY_DL : GITHUB_DL
144
+ const checksumUrl = `${checksumBase}/${repo}/releases/download/${tag}/checksums.txt`
145
+
146
+ // Optional checksum verification (best-effort)
147
+ try {
148
+ const checksumRes = await fetch(checksumUrl)
149
+ if (checksumRes.ok) {
150
+ const text = await checksumRes.text()
151
+ const line = text.split("\n").find((l) => l.includes(asset))
152
+ if (line) {
153
+ const expected = line.split(/\s+/)[0]
154
+ const { createHash } = await import("node:crypto")
155
+ const data = readFileSync(archivePath)
156
+ const actual = createHash("sha256").update(data).digest("hex")
157
+ if (expected !== actual) throw new Error(`checksum mismatch for ${asset}: expected ${expected}, got ${actual}`)
158
+ console.log(`[ekravo] checksum verified ${expected.slice(0, 12)}…`)
159
+ }
160
+ }
161
+ } catch (e) {
162
+ console.warn(`[ekravo] checksum verify skipped: ${e.message}`)
163
+ }
164
+
165
+ mkdirSync(distBin, { recursive: true })
166
+ mkdirSync(join(root, "dist", "share", "ekravo"), { recursive: true })
167
+
168
+ if (isWin) {
169
+ // Use PowerShell Expand-Archive on Windows, tar on Unix (tar handles zip on modern Windows too)
170
+ const ps = spawnSync(
171
+ "powershell.exe",
172
+ ["-Command", `Expand-Archive -Path '${archivePath}' -DestinationPath '${tmp}' -Force`],
173
+ { stdio: "inherit" },
174
+ )
175
+ if (ps.status !== 0) {
176
+ // Fallback to tar
177
+ spawnSync("tar", ["-xzf", archivePath, "-C", tmp], { stdio: "inherit" })
178
+ }
179
+ } else {
180
+ const result = spawnSync("tar", ["-xzf", archivePath, "-C", tmp], { stdio: "inherit" })
181
+ if (result.status !== 0) throw new Error("tar extraction failed")
182
+ }
183
+
184
+ // The archive contains bin/ekravo + share/ekravo/
185
+ const binSrc = join(tmp, "bin", binName)
186
+ const binDest = binPath
187
+ if (existsSync(binSrc)) {
188
+ // Copy binary
189
+ const data = readFileSync(binSrc)
190
+ writeFileSync(binDest, data)
191
+ chmodSync(binDest, 0o755)
192
+ console.log(`[ekravo] installed binary to ${binDest}`)
193
+ } else {
194
+ throw new Error(`archive did not contain bin/${binName}`)
195
+ }
196
+
197
+ // Copy share/ekravo if present
198
+ const photonSrc = join(tmp, "bin", "photon_rs_bg.wasm")
199
+ if (existsSync(photonSrc)) {
200
+ writeFileSync(join(root, "dist", "bin", "photon_rs_bg.wasm"), readFileSync(photonSrc))
201
+ }
202
+ const shareSrc = join(tmp, "share", "ekravo")
203
+ const shareDest = join(root, "dist", "share", "ekravo")
204
+ if (existsSync(shareSrc)) {
205
+ // Use cp -r via spawn
206
+ if (isWin) {
207
+ spawnSync("powershell.exe", ["-Command", `Copy-Item -Recurse -Force '${shareSrc}' '${shareDest}'`], {
208
+ stdio: "inherit",
209
+ })
210
+ } else {
211
+ spawnSync("cp", ["-r", shareSrc, shareDest], { stdio: "inherit" })
212
+ }
213
+ console.log(`[ekravo] installed share to ${shareDest}`)
214
+ }
215
+
216
+ // Also copy proxy-helper if present
217
+ const proxySrc = join(tmp, "share", "ekravo", "bin", isWin ? "proxy-helper.exe" : "proxy-helper")
218
+ const proxyDest = join(root, "dist", "share", "ekravo", "bin", isWin ? "proxy-helper.exe" : "proxy-helper")
219
+ if (existsSync(proxySrc)) {
220
+ mkdirSync(dirname(proxyDest), { recursive: true })
221
+ writeFileSync(proxyDest, readFileSync(proxySrc))
222
+ if (!isWin) chmodSync(proxyDest, 0o755)
223
+ }
224
+
225
+ console.log("[ekravo] postinstall complete")
226
+ } catch (e) {
227
+ console.warn(`[ekravo] postinstall download failed: ${e.message}`)
228
+ console.warn(
229
+ `[ekravo] you can manually install via: curl -fsSL https://github.com/${repo}/releases/latest/download/install.sh | bash`,
230
+ )
231
+ // Don't fail npm install if download fails (e.g. offline, rate-limited)
232
+ process.exit(0)
233
+ } finally {
234
+ try {
235
+ rmSync2(tmp, { recursive: true, force: true })
236
+ } catch {}
237
+ }
@@ -0,0 +1,31 @@
1
+ // Patch the version field in package.json from a git tag ref.
2
+ //
3
+ // Usage: node scripts/set-version.js <git-ref>
4
+ // Example: node scripts/set-version.js v0.2.0
5
+ //
6
+ // Strips the leading "v" and validates basic semver before writing.
7
+
8
+ import { readFileSync, writeFileSync } from "node:fs"
9
+ import { dirname, join } from "node:path"
10
+ import { fileURLToPath } from "node:url"
11
+
12
+ const __dirname = dirname(fileURLToPath(import.meta.url))
13
+ const packageJsonPath = join(__dirname, "..", "package.json")
14
+
15
+ const ref = process.argv[2]
16
+ if (!ref) {
17
+ console.error("Usage: node scripts/set-version.js <git-ref>")
18
+ process.exit(1)
19
+ }
20
+
21
+ const version = ref.replace(/^v/, "")
22
+ if (!/^\d+\.\d+\.\d+/.test(version)) {
23
+ console.error(`Invalid semver version: "${version}" (from ref "${ref}")`)
24
+ process.exit(1)
25
+ }
26
+
27
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"))
28
+ pkg.version = version
29
+ writeFileSync(packageJsonPath, `${JSON.stringify(pkg, null, 2)}\n`)
30
+
31
+ console.log(`Set version to ${version}`)