Repository navigation
Expand file tree
/
Copy pathbin.js
More file actions
121 lines (100 loc) · 3.28 KB
/
Copy pathbin.js
File metadata and controls
121 lines (100 loc) · 3.28 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
const { arch, type } = require("os");
const { join } = require("path");
const { existsSync, mkdirSync, renameSync, rmSync } = require("fs");
const { spawnSync } = require("child_process");
const axios = require("axios");
const tar = require("tar");
const version = require(join(__dirname, "package.json")).version.split("-")[0];
const osType = type();
const osArch = arch();
const goos = {
Windows_NT: "windows",
Linux: "linux",
Darwin: "darwin",
}[osType];
const goarch = {
arm64: "arm64",
x64: "amd64",
}[osArch];
if (goos === undefined || goarch === undefined) {
throw new Error(`Unsupported OS: ${osType} ${osArch}`);
}
/*! Copyright (c) 2019 Avery Harnish - MIT License */
class Binary {
constructor(name, url, installDirectory) {
this.url = url;
this.name = name;
this.installDirectory = installDirectory;
if (!existsSync(this.installDirectory)) {
mkdirSync(this.installDirectory, { recursive: true });
}
const binaryName = goos === "windows" ? `${this.name}.exe` : this.name;
this.binaryPath = join(this.installDirectory, binaryName);
}
install(fetchOptions, suppressLogs = false) {
if (existsSync(this.binaryPath)) {
if (!suppressLogs) {
console.info(
`${this.name} is already installed, skipping installation.`,
);
}
return Promise.resolve();
}
if (existsSync(this.installDirectory)) {
rmSync(this.installDirectory, { recursive: true });
}
mkdirSync(this.installDirectory, { recursive: true });
if (!suppressLogs) {
console.info(`Downloading release from ${this.url}`);
}
return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
.then((res) => {
return new Promise((resolve, reject) => {
const sink = res.data.pipe(
tar.x({ strip: 1, C: this.installDirectory }),
);
sink.on("finish", () => resolve());
sink.on("error", (err) => reject(err));
});
})
.then(() => {
const extractedName = goos === "windows" ? `${this.name}.exe` : this.name;
const extractedPath = join(this.installDirectory, extractedName);
// Only rename if the extracted file is in a different location
if (extractedPath !== this.binaryPath) {
renameSync(extractedPath, this.binaryPath);
}
if (!suppressLogs) {
console.info(`${this.name} has been installed!`);
}
})
.catch((e) => {
console.error(`Error fetching release: ${e.message}`);
});
}
run(fetchOptions) {
const promise = !existsSync(this.binaryPath)
? this.install(fetchOptions, true)
: Promise.resolve();
promise
.then(() => {
const [, , ...args] = process.argv;
const options = { cwd: process.cwd(), stdio: "inherit" };
const result = spawnSync(this.binaryPath, args, options);
if (result.error) {
console.error(result.error);
}
process.exit(result.status);
})
.catch((e) => {
console.error(e.message);
process.exit(1);
});
}
}
const binary = new Binary(
"xmit",
`https://github.com/xmit-co/xmit/releases/download/v${version}/xmit_${version}_${goos}_${goarch}.tgz`,
join(__dirname, `${goos}-${goarch}`),
);
module.exports = binary;