forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostinstall.js
47 lines (37 loc) · 1.31 KB
/
postinstall.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
import { createWriteStream } from "fs";
import * as fs from "fs/promises";
import fetch from "node-fetch";
import { pipeline } from "stream/promises";
import tar from "tar";
import { ARCH_MAPPING, CONFIG, PLATFORM_MAPPING } from "./config.js";
async function install() {
const packageJson = await fs.readFile("package.json").then(JSON.parse);
let version = packageJson.version;
if (typeof version !== "string") {
throw new Error("Missing version in package.json");
}
if (version[0] === "v") version = version.slice(1);
// Fetch Static Config
let { name: binName, path: binPath, url } = CONFIG;
url = url.replace(/{{arch}}/g, ARCH_MAPPING[process.arch]);
url = url.replace(/{{platform}}/g, PLATFORM_MAPPING[process.platform]);
url = url.replace(/{{version}}/g, version);
url = url.replace(/{{bin_name}}/g, binName);
const response = await fetch(url);
if (!response.ok) {
throw new Error("Failed fetching the binary: " + response.statusText);
}
const tarFile = "downloaded.tar.gz";
await fs.mkdir(binPath, { recursive: true });
await pipeline(response.body, createWriteStream(tarFile));
await tar.x({ file: tarFile, cwd: binPath });
await fs.rm(tarFile);
}
install()
.then(async () => {
process.exit(0);
})
.catch(async (err) => {
console.error(err);
process.exit(1);
});