-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
128 lines (108 loc) · 3.36 KB
/
run.js
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
#!/usr/bin/env node
const child = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const cTable = require("console.table");
const axios = require("axios");
const rimraf = require("rimraf");
const tar = require("tar");
const { version, repository } = require("./package.json");
const binDir = path.join(__dirname, "bin");
const appName = "kontrolio";
// Determine the install directory by version so that we can detect when we need
// to upgrade to a new version.
const installDir = path.join(binDir, version, appName);
const supportedPlatforms = [
{
TYPE: "Linux",
ARCHITECTURE: "x64",
GOLANG_TARGET: "linux_amd64",
BINARY_NAME: "kontrolio",
},
{
TYPE: "Darwin",
ARCHITECTURE: "x64",
GOLANG_TARGET: "darwin_amd64",
BINARY_NAME: "kontrolio",
},
];
const platform = (() => {
const type = os.type();
const architecture = os.arch();
for (let index in supportedPlatforms) {
let supportedPlatform = supportedPlatforms[index];
if (
type === supportedPlatform.TYPE &&
architecture === supportedPlatform.ARCHITECTURE
) {
return supportedPlatform;
}
}
error(
`Platform with type "${type}" and architecture "${architecture}" is not supported by ${appName}.\nYour system must be one of the following:\n\n${cTable.getTable(
supportedPlatforms
)}`
);
})();
const binName = platform.TYPE === "Windows" ? ".exe" : "";
const binPath = path.join(installDir, binName);
const install = async () => {
const { GOLANG_TARGET } = platform;
const url = `${repository.url}/releases/download/v${version}/kontrolio_${version}_${GOLANG_TARGET}.tar.gz`;
fs.mkdirSync(installDir, { recursive: true });
try {
const response = await axios({ url, responseType: "stream" });
// Strip the outer directory when extracting. Just get the binary.
const tarWriter = tar.extract({ strip: 1, cwd: installDir });
response.data.pipe(tarWriter);
// Need to return a promise with the writer to ensure we can await for it to complete.
return new Promise((resolve, reject) => {
tarWriter.on("finish", resolve);
tarWriter.on("error", reject);
});
} catch (err) {
throw new Error(`Download archive ${url}: ${err.message}`);
}
};
const run = async () => {
if (!fs.existsSync(binPath)) {
// Remove any existing binaries before installing the new one.
rimraf.sync(binDir);
console.log(`Installing kontrolio ${version}...`);
try {
await install();
} catch (err) {
console.error(`Failed to install: ${err.message}`);
process.exit(1);
}
console.log(`
kkkkkkk
k:::::k
k:::::k
k:::::k
kkkkkkk k:::::k
k:::::k k:::::k
k:::::k k:::::k
k:::::k k:::::k
k:::::k k:::::k
k::::::k:::::k
k:::::::::::k
k:::::::::::k
k::::::k:::::k
k:::::k k:::::k
k:::::k k:::::k
k:::::k k:::::k
kkkkkkk kkkkkkk
Kontrolio CLI successfully installed`);
}
const [, , ...args] = process.argv;
const options = { cwd: process.cwd(), stdio: "inherit" };
const { status, error } = child.spawnSync(binPath, args, options);
if (error) {
console.error(error);
process.exit(1);
}
process.exit(status);
};
run();