-
Notifications
You must be signed in to change notification settings - Fork 360
/
run.js
executable file
·88 lines (71 loc) · 2.56 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
#!/usr/bin/env node
const child = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const axios = require("axios");
const rimraf = require("rimraf");
const tar = require("tar");
const { version } = require("./package.json");
// Strip any v4.0.0-1 style suffix (but not -rc1) so that the correct package is
// downloaded if there is a mistake in the NPM publish and we need to release a
// -1, etc. (because NPM packages are immutable and can't be fixed after a mistake).
const strippedVersion = version.replace(/-[0-9]+/i, "");
const binDir = path.join(__dirname, "install");
// 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, strippedVersion);
const platform = () => {
switch (os.type()) {
case "Windows_NT":
return "windows";
case "Linux":
return "linux";
case "Darwin":
return "macos";
default:
throw new Error(`platform ${os.type} unrecognized`);
}
};
const binName = platform() === "windows" ? "fleetctl.exe" : "fleetctl";
const binPath = path.join(installDir, binName);
const install = async () => {
const url = `https://github.com/fleetdm/fleet/releases/download/${strippedVersion}/fleetctl_${strippedVersion}_${platform()}.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 fleetctl ${version}...`);
try {
await install();
} catch (err) {
console.error(`Failed to install: ${err.message}`);
process.exit(1);
}
console.log("Install completed.");
}
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();