-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
73 lines (62 loc) · 1.91 KB
/
install.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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const https = require("https");
// Windows binaries end with .exe so we need to special case them.
const binaryName = "jstzd-bin";
// Compute the path we want to emit the fallback binary to
const fallbackBinaryPath = path.join(__dirname, binaryName);
function makeRequest(url) {
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
if (response.statusCode >= 200 && response.statusCode < 300) {
const chunks = [];
response.on("data", (chunk) => chunks.push(chunk));
response.on("end", () => {
resolve(Buffer.concat(chunks));
});
} else if (
response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location
) {
// Follow redirects
makeRequest(response.headers.location).then(resolve, reject);
} else {
reject(
new Error(
`npm responded with status code ${response.statusCode} when downloading the package!`
)
);
}
})
.on("error", (error) => {
reject(error);
});
});
}
async function downloadBinaryFromNpm() {
// Download the tarball of the right binary distribution package
const downloadBuffer = await makeRequest(
`https://github.com/huancheng-trili/jstzd-bin/raw/6f11c7842f4c7d7c01ba7e40db84fe2b78854a49/jstzd`
);
// Extract binary from package and write to disk
fs.writeFileSync(
fallbackBinaryPath,
downloadBuffer
);
// Make binary executable
fs.chmodSync(fallbackBinaryPath, "755");
}
function isPlatformSpecificPackageInstalled() {
try {
// Resolving will fail if the optionalDependency was not installed
require.resolve(`${binaryName}`);
return true;
} catch (e) {
return false;
}
}
downloadBinaryFromNpm();
console.log('hi');