@relay-core/cli 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/install.js +88 -1
- package/package.json +1 -1
package/install.js
CHANGED
|
@@ -1,2 +1,89 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const { execSync } = require("child_process");
|
|
5
|
+
const { existsSync, mkdirSync, readFileSync, createWriteStream } = require("fs");
|
|
6
|
+
const { pipeline } = require("stream/promises");
|
|
7
|
+
const { join } = require("path");
|
|
8
|
+
const https = require("https");
|
|
9
|
+
|
|
10
|
+
const PKG_DIR = join(__dirname, "..");
|
|
11
|
+
const BINARY_NAME = "relay-core-cli";
|
|
12
|
+
|
|
13
|
+
function getVersion() {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(readFileSync(join(PKG_DIR, "package.json"), "utf-8")).version;
|
|
16
|
+
} catch { return "0.3.0"; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getTarget() {
|
|
20
|
+
const map = {
|
|
21
|
+
"darwin-x64": "x86_64-apple-darwin",
|
|
22
|
+
"darwin-arm64": "aarch64-apple-darwin",
|
|
23
|
+
"linux-x64": "x86_64-unknown-linux-gnu",
|
|
24
|
+
"linux-arm64": "aarch64-unknown-linux-gnu",
|
|
25
|
+
"win32-x64": "x86_64-pc-windows-msvc",
|
|
26
|
+
};
|
|
27
|
+
const target = map[`${process.platform}-${process.arch}`];
|
|
28
|
+
if (!target) {
|
|
29
|
+
console.error(`Unsupported platform: ${process.platform}-${process.arch}. macOS/Linux/Windows (x64/arm64) supported.`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
return target;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function download(url, dest) {
|
|
36
|
+
console.log(`Downloading ${url} → ${dest}`);
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const file = createWriteStream(dest);
|
|
39
|
+
https.get(url, (response) => {
|
|
40
|
+
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
41
|
+
https.get(response.headers.location, (rr) => pipeline(rr, file).then(resolve, reject));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (response.statusCode !== 200) return reject(new Error(`HTTP ${response.statusCode}`));
|
|
45
|
+
pipeline(response, file).then(resolve, reject);
|
|
46
|
+
}).on("error", reject);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
(async () => {
|
|
51
|
+
try {
|
|
52
|
+
const binDir = join(PKG_DIR, "bin");
|
|
53
|
+
mkdirSync(binDir, { recursive: true });
|
|
54
|
+
|
|
55
|
+
const ext = process.platform === "win32" ? ".exe" : "";
|
|
56
|
+
const binaryPath = join(binDir, BINARY_NAME + ext);
|
|
57
|
+
const version = getVersion();
|
|
58
|
+
|
|
59
|
+
if (existsSync(binaryPath)) {
|
|
60
|
+
try {
|
|
61
|
+
const out = execSync(`"${binaryPath}" --version`, { encoding: "utf-8", timeout: 5000 }).trim();
|
|
62
|
+
if (out.includes(version)) {
|
|
63
|
+
console.log(`${BINARY_NAME} v${version} already installed.`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
console.log(`Installed ${out}, updating to v${version}...`);
|
|
67
|
+
} catch { console.log("Re-downloading..."); }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const target = getTarget();
|
|
71
|
+
const archiveExt = process.platform === "win32" ? "zip" : "tar.gz";
|
|
72
|
+
const url = `https://github.com/relaycraft/relay-core/releases/download/v${version}/${BINARY_NAME}-${target}.${archiveExt}`;
|
|
73
|
+
const archivePath = join(binDir, `${BINARY_NAME}.${archiveExt}`);
|
|
74
|
+
|
|
75
|
+
await download(url, archivePath);
|
|
76
|
+
console.log("Extracting...");
|
|
77
|
+
|
|
78
|
+
if (process.platform === "win32") {
|
|
79
|
+
execSync(`powershell -Command "Expand-Archive -Path '${archivePath}' -DestinationPath '${binDir}' -Force"`, { stdio: "inherit" });
|
|
80
|
+
} else {
|
|
81
|
+
execSync(`tar xzf ${archivePath} -C ${binDir}`, { stdio: "inherit" });
|
|
82
|
+
execSync(`chmod +x ${binaryPath}`, { stdio: "inherit" });
|
|
83
|
+
}
|
|
84
|
+
console.log(`${BINARY_NAME} v${version} installed.`);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error("Install failed:", err.message);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
})();
|