-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbinary.js
87 lines (74 loc) · 2.24 KB
/
binary.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
// this example uses the binary generated by the rust project in the parent directory
// said project is released on GitHub, and the correct URL is constructed based on
// the target operating system and the version in package.json
// your binary could be downloaded from any URL and could use any logic you want
// to construct said URL. You could even A/B test two different binary distribution
// solutions!
const { Binary } = require("@cloudflare/binary-install");
const os = require("os");
const { join } = require("path");
const cTable = require("console.table");
const error = msg => {
console.error(msg);
process.exit(1);
};
const { version, name, repository } = require("./package.json");
const supportedPlatforms = [
{
TYPE: "Windows_NT",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-pc-windows-msvc"
},
{
TYPE: "Linux",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-unknown-linux-musl"
},
{
TYPE: "Darwin",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-apple-darwin"
}
];
const getPlatform = () => {
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.RUST_TARGET;
}
}
error(
`Platform with type "${type}" and architecture "${architecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${cTable.getTable(
supportedPlatforms
)}`
);
};
const getBinary = () => {
const platform = getPlatform();
// the url for this binary is constructed from values in `package.json`
// https://github.com/cloudflare/binary-install/releases/download/v1.0.0/binary-install-example-v1.0.0-x86_64-apple-darwin.tar.gz
const url = `${repository.url}/releases/download/v${version}/${name}-v${version}-${platform}.tar.gz`;
return new Binary(url, { name });
};
const run = () => {
const binary = getBinary();
binary.run();
};
const install = () => {
const binary = getBinary();
binary.install();
};
const uninstall = () => {
const binary = getBinary();
binary.uninstall();
};
module.exports = {
install,
run,
uninstall
};