-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.js
More file actions
65 lines (54 loc) · 2.06 KB
/
Copy pathinstall.js
File metadata and controls
65 lines (54 loc) · 2.06 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
import https from "https";
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
import { fileURLToPath } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REPO = "zigbang-smarthome/eac-cli";
const PLATFORMS = {
"darwin-x64": { artifact: "eac-darwin-x64", ext: ".tar.gz" },
"darwin-arm64": { artifact: "eac-darwin-arm64", ext: ".tar.gz" },
"linux-x64": { artifact: "eac-linux-x64", ext: ".tar.gz" },
"linux-arm64": { artifact: "eac-linux-arm64", ext: ".tar.gz" },
};
function download(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
if (res.statusCode === 302 || res.statusCode === 301) {
return download(res.headers.location).then(resolve).catch(reject);
}
if (res.statusCode !== 200) return reject(new Error(`HTTP ${res.statusCode}`));
const chunks = [];
res.on("data", (c) => chunks.push(c));
res.on("end", () => resolve(Buffer.concat(chunks)));
res.on("error", reject);
});
});
}
if (process.env.CI) process.exit(0);
const nativeDir = path.join(__dirname, "native");
const binPath = path.join(nativeDir, "eac");
if (!fs.existsSync(binPath)) {
const { version } = require("../package.json");
if (version) {
const platform = `${process.platform}-${process.arch}`;
const info = PLATFORMS[platform];
if (!info) {
console.error(`Unsupported platform: ${platform}`);
process.exit(1);
}
const { artifact, ext } = info;
const url = `https://github.com/${REPO}/releases/download/v${version}/${artifact}${ext}`;
console.info(`Downloading eac v${version} for ${platform}...`);
const data = await download(url);
fs.mkdirSync(nativeDir, { recursive: true });
const tmp = path.join(nativeDir, `tmp${ext}`);
fs.writeFileSync(tmp, data);
execSync(`tar xzf "${tmp}"`, { cwd: nativeDir });
fs.unlinkSync(tmp);
fs.chmodSync(binPath, 0o755);
console.info("Installed successfully.");
}
}