voinznext 0.1.0 → 0.1.1
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 +29 -22
- package/package.json +1 -1
package/install.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const { createWriteStream, existsSync, mkdirSync, chmodSync } = require("fs");
|
|
3
|
-
const
|
|
3
|
+
const https = require("https");
|
|
4
4
|
const { join } = require("path");
|
|
5
5
|
const { platform, arch } = require("process");
|
|
6
6
|
|
|
@@ -29,24 +29,42 @@ function getBinaryName() {
|
|
|
29
29
|
return p === "windows" ? `${name}.exe` : name;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
function
|
|
32
|
+
function fetchJson(url) {
|
|
33
33
|
return new Promise((resolve, reject) => {
|
|
34
|
-
|
|
35
|
-
get(url, { headers: { "Accept": "application/vnd.github.v3+json", "User-Agent": "voinznext-installer" } }, (res) => {
|
|
34
|
+
https.get(url, { headers: { "Accept": "application/vnd.github.v3+json", "User-Agent": "voinznext-installer" } }, (res) => {
|
|
36
35
|
let data = "";
|
|
37
36
|
res.on("data", (chunk) => (data += chunk));
|
|
38
37
|
res.on("end", () => {
|
|
39
|
-
try {
|
|
40
|
-
|
|
41
|
-
resolve(json.tag_name);
|
|
42
|
-
} catch {
|
|
43
|
-
reject(new Error(`Failed to parse latest release response: ${data}`));
|
|
44
|
-
}
|
|
38
|
+
try { resolve(JSON.parse(data)); }
|
|
39
|
+
catch { reject(new Error(`Failed to parse response: ${data}`)); }
|
|
45
40
|
});
|
|
46
41
|
}).on("error", reject);
|
|
47
42
|
});
|
|
48
43
|
}
|
|
49
44
|
|
|
45
|
+
function getLatestVersion() {
|
|
46
|
+
return fetchJson(`https://api.github.com/repos/${REPO}/releases/latest`).then((json) => json.tag_name);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function download(url, dest) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
https.get(url, { headers: { "User-Agent": "voinznext-installer" } }, (res) => {
|
|
52
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
53
|
+
download(res.headers.location, dest).then(resolve).catch(reject);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (res.statusCode !== 200) {
|
|
57
|
+
reject(new Error(`Download failed with status ${res.statusCode}`));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const file = createWriteStream(dest);
|
|
61
|
+
res.pipe(file);
|
|
62
|
+
file.on("finish", () => file.close(resolve));
|
|
63
|
+
file.on("error", reject);
|
|
64
|
+
}).on("error", reject);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
50
68
|
async function install() {
|
|
51
69
|
const binDir = join(__dirname, "bin");
|
|
52
70
|
if (!existsSync(binDir)) mkdirSync(binDir, { recursive: true });
|
|
@@ -59,18 +77,7 @@ async function install() {
|
|
|
59
77
|
const version = await getLatestVersion();
|
|
60
78
|
const downloadUrl = `https://github.com/${REPO}/releases/download/${version}/${binaryName}`;
|
|
61
79
|
|
|
62
|
-
await
|
|
63
|
-
get(downloadUrl, { headers: { "User-Agent": "voinznext-installer" } }, (res) => {
|
|
64
|
-
if (res.statusCode !== 200) {
|
|
65
|
-
reject(new Error(`Download failed with status ${res.statusCode}`));
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const file = createWriteStream(targetPath);
|
|
69
|
-
res.pipe(file);
|
|
70
|
-
file.on("finish", () => file.close(resolve));
|
|
71
|
-
file.on("error", reject);
|
|
72
|
-
}).on("error", reject);
|
|
73
|
-
});
|
|
80
|
+
await download(downloadUrl, targetPath);
|
|
74
81
|
|
|
75
82
|
if (platform !== "win32") chmodSync(targetPath, 0o755);
|
|
76
83
|
console.log(` ✔ Installed to ${targetPath}`);
|