Repository navigation
Expand file tree
/
Copy pathinstall.js
More file actions
93 lines (79 loc) · 2.71 KB
/
Copy pathinstall.js
File metadata and controls
93 lines (79 loc) · 2.71 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
const https = require("https");
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const os = require("os");
const REPO = "KimMaru10/bl-cli";
const BIN_DIR = path.join(__dirname, "bin");
const BIN_PATH = path.join(BIN_DIR, "bl-binary");
function getPlatform() {
const platform = os.platform();
if (platform === "darwin") return "darwin";
if (platform === "linux") return "linux";
throw new Error(`Unsupported platform: ${platform}`);
}
function getArch() {
const arch = os.arch();
if (arch === "x64") return "amd64";
if (arch === "arm64") return "arm64";
throw new Error(`Unsupported architecture: ${arch}`);
}
function getDownloadUrl(version, platform, arch) {
const ext = platform === "darwin" ? "zip" : "tar.gz";
const tag = version.startsWith("v") ? version : `v${version}`;
const ver = tag.replace(/^v/, "");
return `https://github.com/${REPO}/releases/download/${tag}/bl-cli_${ver}_${platform}_${arch}.${ext}`;
}
function httpsGet(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
httpsGet(res.headers.location).then(resolve).catch(reject);
return;
}
if (res.statusCode !== 200) {
reject(new Error(`HTTP ${res.statusCode} for ${url}`));
return;
}
const chunks = [];
res.on("data", (chunk) => chunks.push(chunk));
res.on("end", () => resolve(Buffer.concat(chunks)));
res.on("error", reject);
}).on("error", reject);
});
}
function getVersion() {
const pkg = require("./package.json");
return pkg.version;
}
async function install() {
const platform = getPlatform();
const arch = getArch();
const version = getVersion();
const url = getDownloadUrl(version, platform, arch);
const ext = platform === "darwin" ? "zip" : "tar.gz";
console.log(`Downloading bl ${version} for ${platform}/${arch}...`);
const data = await httpsGet(url);
if (!fs.existsSync(BIN_DIR)) {
fs.mkdirSync(BIN_DIR, { recursive: true });
}
const tmpFile = path.join(os.tmpdir(), `bl-cli.${ext}`);
fs.writeFileSync(tmpFile, data);
try {
if (ext === "zip") {
execSync(`unzip -o -j "${tmpFile}" bl -d "${BIN_DIR}"`, { stdio: "pipe" });
fs.renameSync(path.join(BIN_DIR, "bl"), BIN_PATH);
} else {
execSync(`tar -xzf "${tmpFile}" -C "${BIN_DIR}" bl`, { stdio: "pipe" });
fs.renameSync(path.join(BIN_DIR, "bl"), BIN_PATH);
}
fs.chmodSync(BIN_PATH, 0o755);
console.log("bl installed successfully.");
} finally {
fs.unlinkSync(tmpFile);
}
}
install().catch((err) => {
console.error("Failed to install bl:", err.message);
process.exit(1);
});