| 1 | const path = require("path"); |
| 2 | const os = require("os"); |
| 3 | |
| 4 | const CHECKSUM_MANIFEST = "codewhale-artifacts-sha256.txt"; |
| 5 | const BUNDLE_CHECKSUM_MANIFEST = "codewhale-bundles-sha256.txt"; |
| 6 | const WINDOWS_INSTALLER_ASSET = "CodeWhaleSetup.exe"; |
| 7 | |
| 8 | const CNB_BINARY_ASSET_NAMES = [ |
| 9 | "codewhale-linux-x64", |
| 10 | "codew-linux-x64", |
| 11 | "codewhale-tui-linux-x64", |
| 12 | ]; |
| 13 | const CNB_RELEASE_ASSET_NAMES = [ |
| 14 | ...CNB_BINARY_ASSET_NAMES, |
| 15 | CHECKSUM_MANIFEST, |
| 16 | ]; |
| 17 | |
| 18 | const BUNDLE_ASSET_NAMES = [ |
| 19 | "codewhale-linux-x64.tar.gz", |
| 20 | "codewhale-linux-arm64.tar.gz", |
| 21 | "codewhale-android-arm64.tar.gz", |
| 22 | "codewhale-macos-x64.tar.gz", |
| 23 | "codewhale-macos-arm64.tar.gz", |
| 24 | "codewhale-windows-x64.zip", |
| 25 | "codewhale-windows-x64-portable.zip", |
| 26 | "codewhale-windows-arm64.zip", |
| 27 | "codewhale-windows-arm64-portable.zip", |
| 28 | ]; |
| 29 | |
| 30 | const ASSET_MATRIX = { |
| 31 | linux: { |
| 32 | x64: ["codewhale-linux-x64", "codewhale-tui-linux-x64", "codew-linux-x64"], |
| 33 | arm64: ["codewhale-linux-arm64", "codewhale-tui-linux-arm64", "codew-linux-arm64"], |
| 34 | }, |
| 35 | android: { |
| 36 | arm64: ["codewhale-android-arm64", "codewhale-tui-android-arm64", "codew-android-arm64"], |
| 37 | }, |
| 38 | darwin: { |
| 39 | x64: ["codewhale-macos-x64", "codewhale-tui-macos-x64", "codew-macos-x64"], |
| 40 | arm64: ["codewhale-macos-arm64", "codewhale-tui-macos-arm64", "codew-macos-arm64"], |
| 41 | }, |
| 42 | win32: { |
| 43 | x64: ["codewhale-windows-x64.exe", "codewhale-tui-windows-x64.exe", "codew-windows-x64.exe", "codewhale.bat"], |
| 44 | arm64: ["codewhale-windows-arm64.exe", "codewhale-tui-windows-arm64.exe", "codew-windows-arm64.exe"], |
| 45 | }, |
| 46 | }; |
| 47 | |
| 48 | // HarmonyPC (openharmony) is an x86_64 Linux-compatible environment; map it to |
| 49 | // the linux binary family so npm install succeeds without a separate build target. |
| 50 | const PLATFORM_ALIASES = { |
| 51 | openharmony: "linux", |
| 52 | }; |
| 53 | |
| 54 | function detectBinaryNames() { |
| 55 | const rawPlatform = os.platform(); |
| 56 | const platform = PLATFORM_ALIASES[rawPlatform] || rawPlatform; |
| 57 | const arch = os.arch(); |
| 58 | const defaults = ASSET_MATRIX[platform]; |
| 59 | if (!defaults) { |
| 60 | const supported = Object.keys(ASSET_MATRIX).map(p => `'${p}'`).join(', '); |
| 61 | throw new Error( |
| 62 | `Unsupported platform: ${rawPlatform}. Supported platforms: ${supported}.\n\n` + |
| 63 | unsupportedBuildHint(), |
| 64 | ); |
| 65 | } |
| 66 | const pair = defaults[arch]; |
| 67 | if (!pair) { |
| 68 | const supported = Object.keys(defaults).map(a => `'${a}'`).join(', '); |
| 69 | const hint = platform === "linux" && arch === "riscv64" ? unsupportedRiscvHint() : unsupportedBuildHint(); |
| 70 | throw new Error( |
| 71 | `Unsupported architecture: ${arch} on platform ${platform}. ` + |
| 72 | `Supported architectures: ${supported}.\n\n` + |
| 73 | hint, |
| 74 | ); |
| 75 | } |
| 76 | return { |
| 77 | platform, |
| 78 | arch, |
| 79 | codewhale: pair[0], |
| 80 | tui: pair[1], |
| 81 | codew: pair[2], |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | function unsupportedBuildHint() { |
| 86 | return [ |
| 87 | "No prebuilt binary is available for this platform/architecture combo.", |
| 88 | "You can still run codewhale by building from source with Cargo:", |
| 89 | "", |
| 90 | " # Requires Rust 1.88+ (https://rustup.rs)", |
| 91 | " cargo install codewhale-cli --locked # provides `codewhale` and `codew`", |
| 92 | " cargo install codewhale-tui --locked # provides `codewhale-tui`", |
| 93 | "", |
| 94 | "Or build from a checkout:", |
| 95 | "", |
| 96 | " git clone https://github.com/Hmbown/CodeWhale.git", |
| 97 | " cd CodeWhale", |
| 98 | " cargo install --path crates/cli --locked", |
| 99 | " cargo install --path crates/tui --locked", |
| 100 | "", |
| 101 | "See https://github.com/Hmbown/CodeWhale/blob/main/docs/INSTALL.md", |
| 102 | "for cross-compilation, mirror, and Linux ARM64 specifics.", |
| 103 | ].join("\n"); |
| 104 | } |
| 105 | |
| 106 | function unsupportedRiscvHint() { |
| 107 | return [ |
| 108 | "Linux riscv64 prebuilt binaries are temporarily unavailable.", |
| 109 | "CodeWhale currently depends on rquickjs-sys, which does not ship", |
| 110 | "riscv64gc-unknown-linux-gnu bindings in the locked dependency set.", |
| 111 | "", |
| 112 | "Track the release notes and docs/INSTALL.md for the next RISC-V support update.", |
| 113 | ].join("\n"); |
| 114 | } |
| 115 | |
| 116 | function executableName(base, platform) { |
| 117 | return platform === "win32" ? `${base}.exe` : base; |
| 118 | } |
| 119 | |
| 120 | function releaseBaseUrl(version, repo = "Hmbown/CodeWhale") { |
| 121 | // CODEWHALE_RELEASE_BASE_URL is the canonical override. |
| 122 | // DEEPSEEK_TUI_RELEASE_BASE_URL / DEEPSEEK_RELEASE_BASE_URL are legacy aliases. |
| 123 | const override = |
| 124 | process.env.CODEWHALE_RELEASE_BASE_URL || |
| 125 | process.env.DEEPSEEK_TUI_RELEASE_BASE_URL || |
| 126 | process.env.DEEPSEEK_RELEASE_BASE_URL; |
| 127 | if (override) { |
| 128 | const trimmed = String(override).trim(); |
| 129 | return trimmed.endsWith("/") ? trimmed : `${trimmed}/`; |
| 130 | } |
| 131 | // When CODEWHALE_USE_CNB_MIRROR is set, use the CNB (China-friendly) |
| 132 | // mirror that already builds and publishes binary release assets. |
| 133 | if (usesCnbMirror()) { |
| 134 | assertCnbMirrorSupportedPlatform(); |
| 135 | return `https://cnb.cool/codewhale.net/codewhale/-/releases/v${version}/`; |
| 136 | } |
| 137 | return `https://github.com/${repo}/releases/download/v${version}/`; |
| 138 | } |
| 139 | |
| 140 | function usesCnbMirror(env = process.env) { |
| 141 | const hasExplicitBase = Boolean( |
| 142 | env.CODEWHALE_RELEASE_BASE_URL || |
| 143 | env.DEEPSEEK_TUI_RELEASE_BASE_URL || |
| 144 | env.DEEPSEEK_RELEASE_BASE_URL, |
| 145 | ); |
| 146 | return !hasExplicitBase && Boolean(env.CODEWHALE_USE_CNB_MIRROR); |
| 147 | } |
| 148 | |
| 149 | function assertCnbMirrorSupportedPlatform( |
| 150 | rawPlatform = os.platform(), |
| 151 | arch = os.arch(), |
| 152 | ) { |
| 153 | const platform = PLATFORM_ALIASES[rawPlatform] || rawPlatform; |
| 154 | if (platform === "linux" && arch === "x64") { |
| 155 | return; |
| 156 | } |
| 157 | throw new Error( |
| 158 | "CODEWHALE_USE_CNB_MIRROR=1 currently supports only Linux x64 " + |
| 159 | `(including OpenHarmony x64); detected ${rawPlatform} ${arch}. ` + |
| 160 | "Use the GitHub Release or set CODEWHALE_RELEASE_BASE_URL to a " + |
| 161 | "complete mirror for this platform.", |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | function releaseAssetUrl(baseName, version, repo = "Hmbown/CodeWhale") { |
| 166 | return new URL(baseName, releaseBaseUrl(version, repo)).toString(); |
| 167 | } |
| 168 | |
| 169 | function checksumManifestUrl(version, repo = "Hmbown/CodeWhale") { |
| 170 | return releaseAssetUrl(CHECKSUM_MANIFEST, version, repo); |
| 171 | } |
| 172 | |
| 173 | function releaseBinaryDirectory() { |
| 174 | return path.join(__dirname, "..", "bin", "downloads"); |
| 175 | } |
| 176 | |
| 177 | function allAssetNames() { |
| 178 | const names = []; |
| 179 | for (const platformAssets of Object.values(ASSET_MATRIX)) { |
| 180 | for (const assets of Object.values(platformAssets)) { |
| 181 | names.push(...assets); |
| 182 | } |
| 183 | } |
| 184 | return Array.from(new Set(names)); |
| 185 | } |
| 186 | |
| 187 | function allReleaseAssetNames() { |
| 188 | return [ |
| 189 | ...allAssetNames(), |
| 190 | ...BUNDLE_ASSET_NAMES, |
| 191 | WINDOWS_INSTALLER_ASSET, |
| 192 | BUNDLE_CHECKSUM_MANIFEST, |
| 193 | CHECKSUM_MANIFEST, |
| 194 | ]; |
| 195 | } |
| 196 | |
| 197 | function checksummedReleaseAssetNames() { |
| 198 | return allReleaseAssetNames().filter((name) => name !== CHECKSUM_MANIFEST); |
| 199 | } |
| 200 | |
| 201 | module.exports = { |
| 202 | allAssetNames, |
| 203 | allReleaseAssetNames, |
| 204 | assertCnbMirrorSupportedPlatform, |
| 205 | BUNDLE_ASSET_NAMES, |
| 206 | BUNDLE_CHECKSUM_MANIFEST, |
| 207 | CHECKSUM_MANIFEST, |
| 208 | checksummedReleaseAssetNames, |
| 209 | CNB_BINARY_ASSET_NAMES, |
| 210 | CNB_RELEASE_ASSET_NAMES, |
| 211 | checksumManifestUrl, |
| 212 | detectBinaryNames, |
| 213 | executableName, |
| 214 | releaseAssetUrl, |
| 215 | releaseBaseUrl, |
| 216 | releaseBinaryDirectory, |
| 217 | usesCnbMirror, |
| 218 | WINDOWS_INSTALLER_ASSET, |
| 219 | }; |
| 220 |