-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathinstall-uv.js
More file actions
207 lines (185 loc) · 5.84 KB
/
install-uv.js
File metadata and controls
207 lines (185 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// @ts-check
import fs from "fs";
import path from "path";
import os from "os";
import { execSync } from "child_process";
import * as tar from "tar";
import AdmZip from "adm-zip";
import { downloadWithRedirects } from "./download.js";
// Base URL for downloading uv binaries
const UV_RELEASE_BASE_URL = "https://github.com/astral-sh/uv/releases/download";
const DEFAULT_UV_VERSION = "0.6.14";
// Mapping of platform+arch to binary package name
const UV_PACKAGES = {
"darwin-arm64": "uv-aarch64-apple-darwin.tar.gz",
"darwin-x64": "uv-x86_64-apple-darwin.tar.gz",
"win32-arm64": "uv-aarch64-pc-windows-msvc.zip",
"win32-ia32": "uv-i686-pc-windows-msvc.zip",
"win32-x64": "uv-x86_64-pc-windows-msvc.zip",
"linux-arm64": "uv-aarch64-unknown-linux-gnu.tar.gz",
"linux-ia32": "uv-i686-unknown-linux-gnu.tar.gz",
"linux-ppc64": "uv-powerpc64-unknown-linux-gnu.tar.gz",
"linux-ppc64le": "uv-powerpc64le-unknown-linux-gnu.tar.gz",
"linux-s390x": "uv-s390x-unknown-linux-gnu.tar.gz",
"linux-x64": "uv-x86_64-unknown-linux-gnu.tar.gz",
"linux-armv7l": "uv-armv7-unknown-linux-gnueabihf.tar.gz",
// MUSL variants
"linux-musl-arm64": "uv-aarch64-unknown-linux-musl.tar.gz",
"linux-musl-ia32": "uv-i686-unknown-linux-musl.tar.gz",
"linux-musl-x64": "uv-x86_64-unknown-linux-musl.tar.gz",
"linux-musl-armv6l": "uv-arm-unknown-linux-musleabihf.tar.gz",
"linux-musl-armv7l": "uv-armv7-unknown-linux-musleabihf.tar.gz",
};
/**
* Downloads and extracts the uv binary for the specified platform and architecture
* @param {string} platform Platform to download for (e.g., 'darwin', 'win32', 'linux')
* @param {string} arch Architecture to download for (e.g., 'x64', 'arm64')
* @param {string} version Version of uv to download
* @param {boolean} isMusl Whether to use MUSL variant for Linux
*/
async function downloadUvBinary(
uv_download_url,
platform,
arch,
version = DEFAULT_UV_VERSION,
isMusl = false
) {
console.log('[START] downloadUvBinary:', uv_download_url);
const platformKey = isMusl
? `${platform}-musl-${arch}`
: `${platform}-${arch}`;
const packageName = UV_PACKAGES[platformKey];
if (!packageName) {
console.error(`No binary available for ${platformKey}`);
return false;
}
// Create output directory structure
const binDir = path.join(os.homedir(), ".eigent", "bin");
// Ensure directories exist
fs.mkdirSync(binDir, { recursive: true });
// Download URL for the specific binary
const downloadUrl = `${uv_download_url}/${version}/${packageName}`;
const tempdir = os.tmpdir();
// Use unique temp filename to avoid race conditions when multiple processes install simultaneously
const uniqueSuffix = `${process.pid}-${Date.now()}`;
const tempFilename = path.join(tempdir, `${uniqueSuffix}-${packageName}`);
try {
console.log(`Downloading uv ${version} for ${platformKey}...`);
console.log(`URL: ${downloadUrl}`);
if (fs.existsSync(tempFilename)) fs.unlinkSync(tempFilename)
await downloadWithRedirects(downloadUrl, tempFilename);
console.log(`Extracting ${packageName} to ${binDir}...`);
if (packageName.endsWith(".zip")) {
// use adm-zip to handle zip file
const zip = new AdmZip(tempFilename);
zip.extractAllTo(binDir, true);
fs.unlinkSync(tempFilename);
console.log(
`Successfully installed uv ${version} for ${platform}-${arch}`
);
return true;
} else {
// handle tar.gz file
await tar.x({
file: tempFilename,
cwd: binDir,
strip: 1,
z: true,
filter: (path) => {
// Only extract the uv binary
return path.endsWith('uv') || path.endsWith('uv.exe');
}
});
// Set executable permissions for non-Windows platforms
if (platform !== "win32") {
const uvPath = path.join(binDir, "uv");
if (fs.existsSync(uvPath)) {
try {
fs.chmodSync(uvPath, "755");
} catch (error) {
console.warn(
`Warning: Failed to set executable permissions: ${error.message}`
);
}
}
}
// Clean up
fs.unlinkSync(tempFilename);
}
console.log(`Successfully installed uv ${version} for ${platform}-${arch}`);
return true;
} catch (error) {
console.error(`Error installing uv for ${platformKey}: ${error.message}`);
if (fs.existsSync(tempFilename)) {
fs.unlinkSync(tempFilename);
}
// Check if binDir is empty and remove it if so
try {
const files = fs.readdirSync(binDir);
if (files.length === 0) {
fs.rmSync(binDir, { recursive: true });
console.log(`Removed empty directory: ${binDir}`);
}
} catch (cleanupError) {
console.warn(
`Warning: Failed to clean up directory: ${cleanupError.message}`
);
}
return false;
}
}
/**
* Detects current platform and architecture
*/
function detectPlatformAndArch() {
const platform = os.platform();
const arch = os.arch();
const isMusl = platform === "linux" && detectIsMusl();
return { platform, arch, isMusl };
}
/**
* Attempts to detect if running on MUSL libc
*/
function detectIsMusl() {
try {
// Simple check for Alpine Linux which uses MUSL
const output = fs.readFileSync("/etc/os-release", "utf8");
return output.toLowerCase().includes("alpine");
} catch (error) {
return false;
}
}
/**
* Main function to install uv
*/
async function installUv() {
// Get the latest version if no specific version is provided
const version = DEFAULT_UV_VERSION;
console.log(`Using uv version: ${version}`);
const { platform, arch, isMusl } = detectPlatformAndArch();
console.log(
`Installing uv ${version} for ${platform}-${arch}${
isMusl ? " (MUSL)" : ""
}...`
);
let isInstalled = await downloadUvBinary(
UV_RELEASE_BASE_URL,
platform,
arch,
version,
isMusl
);
if (!isInstalled) {
throw new Error(`Failed to download uv ${version} from default source`);
}
}
// Run the installation
installUv()
.then(() => {
console.log("Installation successful");
process.exit(0);
})
.catch((error) => {
console.error("Installation failed:", error);
process.exit(1);
});