forked from kt3k/deno-bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
61 lines (57 loc) · 1.62 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
// @ts-check
const fs = require("fs");
const path = require("path");
const pkg = require("./package.json");
const AdmZip = require("adm-zip");
function filename() {
switch (process.platform) {
case "win32": {
return "deno-x86_64-pc-windows-msvc.zip";
}
case "darwin": {
if (process.arch === "x64") {
return "deno-x86_64-apple-darwin.zip";
} else if (process.arch === "arm64") {
return "deno-aarch64-apple-darwin.zip";
}
throw new Error(`Not supported architecture: ${process.arch}`);
}
case "linux": {
return "deno-x86_64-unknown-linux-gnu.zip";
}
default: {
throw new Error(`Not a supported platform: ${process.platform}`);
}
}
}
function executableFilename() {
switch (process.platform) {
case "win32": {
return "deno.exe";
}
default: {
return "deno";
}
}
}
async function main() {
const dlUrl =
`https://github.com/denoland/deno/releases/download/v${pkg.version}/${filename()}`;
const binPath = path.join(__dirname, "bin");
const denoBin = path.join(binPath, executableFilename());
try {
await fs.promises.access(denoBin);
} catch {
// 1. Downloads Deno binary zip from github release page
// TODO: handle errors
const response = await fetch(dlUrl);
// 2. Stores it in memory
const zip = Buffer.from(await response.arrayBuffer());
// 3. Extracts `deno` entry to bin path.
new AdmZip(zip).extractEntryTo(executableFilename(), binPath, true, true);
// 4. Changes the file permission
await fs.promises.chmod(denoBin, 0o755);
}
return denoBin;
}
module.exports = main();