-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
57 lines (47 loc) · 1.51 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
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";
import path from "path";
async function install() {
const version = await (
await fetch("https://get-latest.deno.dev/cli/cli?no-v=true")
).text();
// Fetch Static Config
let { name: binName, path: binPath, url } = CONFIG;
url = url.replace(/{{platform}}/g, PLATFORM_MAPPING[process.platform]);
url = url.replace(/{{arch}}/g, ARCH_MAPPING[process.arch]);
url = url.replace(/{{version}}/g, version);
url = url.replace(/{{bin_name}}/g, binName);
console.log(url);
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);
await fs.copyFile(
path.join(
"bin",
`${binName}_${version}_${PLATFORM_MAPPING[process.platform]}_${
ARCH_MAPPING[process.arch]
}`,
"bin",
process.platform === "win32" ? "gh.exe" : "gh"
),
path.join("bin", process.platform === "win32" ? "gh.exe" : "gh")
);
}
install()
.then(async () => {
process.exit(0);
})
.catch(async (err) => {
console.error(err);
process.exit(1);
});