|
1 | 1 | const fs = require('node:fs'); |
2 | | -const os = require('node:os'); |
3 | 2 | const path = require('node:path'); |
4 | 3 | const https = require('node:https'); |
| 4 | +const { execSync } = require('node:child_process'); |
5 | 5 |
|
6 | 6 | const pkg = require('../package.json'); |
7 | 7 |
|
@@ -80,22 +80,52 @@ async function main() { |
80 | 80 | } |
81 | 81 |
|
82 | 82 | const isWindows = process.platform === 'win32'; |
83 | | - const assetName = isWindows |
84 | | - ? `tu-${VERSION_TAG}-${target}.exe` |
85 | | - : `tu-${VERSION_TAG}-${target}`; |
| 83 | + const vendorDir = path.join(__dirname, '..', 'vendor'); |
| 84 | + const outFile = path.join(vendorDir, isWindows ? 'tu.exe' : 'tu'); |
| 85 | + |
| 86 | + if (isWindows) { |
| 87 | + // Windows: download bare .exe directly. |
| 88 | + const assetName = `tu-${VERSION_TAG}-${target}.exe`; |
| 89 | + const url = `https://github.com/${OWNER}/${REPO}/releases/download/${VERSION_TAG}/${assetName}`; |
| 90 | + |
| 91 | + try { |
| 92 | + await downloadWithRedirect(url, outFile); |
| 93 | + console.log(`[tokenusage] Installed ${assetName}`); |
| 94 | + } catch (err) { |
| 95 | + console.warn(`[tokenusage] Failed to download release binary: ${err.message}`); |
| 96 | + console.warn('[tokenusage] You can still build/install via cargo install tokenusage --bin tu'); |
| 97 | + } |
| 98 | + } else { |
| 99 | + // macOS / Linux: download tar.gz archive and extract the binary. |
| 100 | + const assetName = `tu-${VERSION_TAG}-${target}.tar.gz`; |
| 101 | + const url = `https://github.com/${OWNER}/${REPO}/releases/download/${VERSION_TAG}/${assetName}`; |
| 102 | + const archivePath = path.join(vendorDir, assetName); |
| 103 | + |
| 104 | + try { |
| 105 | + await downloadWithRedirect(url, archivePath); |
| 106 | + |
| 107 | + // Extract: archive contains <dir>/tu — extract just the binary. |
| 108 | + const innerDir = `tu-${VERSION_TAG}-${target}`; |
| 109 | + execSync(`tar -xzf "${archivePath}" -C "${vendorDir}" "${innerDir}/tu"`, { |
| 110 | + stdio: 'pipe', |
| 111 | + }); |
| 112 | + |
| 113 | + // Move the binary from the extracted subdirectory to vendor/. |
| 114 | + const extractedBin = path.join(vendorDir, innerDir, 'tu'); |
| 115 | + await fs.promises.rename(extractedBin, outFile); |
| 116 | + await fs.promises.chmod(outFile, 0o755); |
86 | 117 |
|
87 | | - const url = `https://github.com/${OWNER}/${REPO}/releases/download/${VERSION_TAG}/${assetName}`; |
88 | | - const outFile = path.join(__dirname, '..', 'vendor', isWindows ? 'tu.exe' : 'tu'); |
| 118 | + // Clean up archive and extracted directory. |
| 119 | + await fs.promises.rm(archivePath, { force: true }); |
| 120 | + await fs.promises.rm(path.join(vendorDir, innerDir), { recursive: true, force: true }); |
89 | 121 |
|
90 | | - try { |
91 | | - await downloadWithRedirect(url, outFile); |
92 | | - if (!isWindows) { |
93 | | - await fs.promises.chmod(outFile, 0o755); |
| 122 | + console.log(`[tokenusage] Installed tu from ${assetName}`); |
| 123 | + } catch (err) { |
| 124 | + // Clean up on failure. |
| 125 | + try { await fs.promises.rm(archivePath, { force: true }); } catch {} |
| 126 | + console.warn(`[tokenusage] Failed to download release binary: ${err.message}`); |
| 127 | + console.warn('[tokenusage] You can still build/install via cargo install tokenusage --bin tu'); |
94 | 128 | } |
95 | | - console.log(`[tokenusage] Installed ${assetName}`); |
96 | | - } catch (err) { |
97 | | - console.warn(`[tokenusage] Failed to download release binary: ${err.message}`); |
98 | | - console.warn('[tokenusage] You can still build/install via cargo install tokenusage --bin tu'); |
99 | 129 | } |
100 | 130 | } |
101 | 131 |
|
|
0 commit comments