-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (110 loc) · 3.19 KB
/
index.js
File metadata and controls
132 lines (110 loc) · 3.19 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const axios = require("axios");
const ProgressBar = require("progress");
const { spawn } = require("child_process");
const tar = require("tar");
const packageJson = require("./package.json");
const binaryDir = path.join(__dirname, "bin");
const binaryPath = path.join(binaryDir, process.platform === "win32" ? "tau.exe" : "tau");
const versionFilePath = path.join(binaryDir, "version.txt");
const packageVersion = packageJson.version;
function binaryExists() {
return fs.existsSync(binaryPath);
}
function versionMatches() {
if (!fs.existsSync(versionFilePath)) {
return false;
}
const installedVersion = fs.readFileSync(versionFilePath, "utf-8").trim();
return installedVersion === packageVersion;
}
function parseAssetName() {
let os, arch;
if (process.platform === "darwin") {
os = "darwin";
} else if (process.platform === "linux") {
os = "linux";
} else if (process.platform === "win32") {
os = "windows";
} else {
os = null;
}
if (process.arch === "x64") {
arch = "amd64";
} else if (process.arch === "arm64") {
arch = "arm64";
} else {
arch = null;
}
return { os, arch };
}
async function downloadAndExtractBinary() {
if (binaryExists() && versionMatches()) {
return;
}
const version = packageVersion;
const { os: currentOs, arch: currentArch } = parseAssetName();
if (!currentOs || !currentArch) {
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
}
const assetName = `tau-cli_${version}_${currentOs}_${currentArch}.tar.gz`;
const assetUrl = `https://github.com/taubyte/tau-cli/releases/download/v${version}/${assetName}`;
console.log(`Downloading tau-cli v${version}...`);
const { data, headers } = await axios({
url: assetUrl,
method: "GET",
responseType: "stream",
});
const totalLength = headers["content-length"];
const progressBar = new ProgressBar("-> downloading [:bar] :percent :etas", {
width: 40,
complete: "=",
incomplete: " ",
renderThrottle: 1,
total: parseInt(totalLength, 10) || 0,
});
if (!fs.existsSync(binaryDir)) {
fs.mkdirSync(binaryDir);
}
const tarPath = path.join(binaryDir, assetName);
const writer = fs.createWriteStream(tarPath);
data.on("data", (chunk) => progressBar.tick(chunk.length));
data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", async () => {
console.log(`Extracting tau-cli v${version}...`);
await tar.x({
file: tarPath,
C: binaryDir,
});
fs.unlinkSync(tarPath);
fs.writeFileSync(versionFilePath, version);
resolve();
});
writer.on("error", reject);
});
}
function executeBinary() {
if (!binaryExists()) {
console.error("Binary not found. Please run the install script.");
return;
}
const args = process.argv.slice(2);
const child = spawn(binaryPath, args, {
stdio: "inherit",
});
child.on("error", (err) => {
console.error("Failed to start binary:", err);
});
}
async function main() {
try {
await downloadAndExtractBinary();
executeBinary();
} catch (err) {
console.error(err.message);
}
}
main();