|
| 1 | +#!/usr/bin/env -S bun run -- |
| 2 | + |
| 3 | +/** |
| 4 | + * Note: this file does not use any run-time dependencies, only `bun` built-ins. |
| 5 | + * This is less ergonomic, but it allows running the script directly, which in |
| 6 | + * turn means fewer opportunities for things to go wrong. |
| 7 | + */ |
| 8 | + |
| 9 | +import assert from "node:assert"; |
| 10 | +import { mkdir, mkdtemp, readFile, rename } from "node:fs/promises"; |
| 11 | +import { arch, homedir, platform, tmpdir } from "node:os"; |
| 12 | +import { dirname, join } from "node:path"; |
| 13 | +import { env, exit } from "node:process"; |
| 14 | +import { $, write } from "bun"; |
| 15 | + |
| 16 | +const BIN_INSTALLATION_DIR = join( |
| 17 | + homedir(), |
| 18 | + "./.local/share/install-fish/bin/fish", |
| 19 | +); |
| 20 | +const BIN_INSTALLATION_PATH = join(BIN_INSTALLATION_DIR, "./fish"); |
| 21 | + |
| 22 | +console.log("PATH", env["PATH"]); |
| 23 | + |
| 24 | +const metadata: { [version: string]: { url: string; hash: string } } = { |
| 25 | + "4.3.3": { |
| 26 | + url: "https://github.com/fish-shell/fish-shell/releases/download/4.3.3/fish-4.3.3-linux-x86_64.tar.xz", |
| 27 | + hash: "3759c788d7941c5d0ca7713b259612211f4efb9041426ea415aaa8e4022d3392", |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +async function installFish(version: string = "4.3.3"): Promise<void> { |
| 32 | + assert(metadata[version]); |
| 33 | + const { url, hash } = metadata[version]; |
| 34 | + |
| 35 | + assert.equal(platform(), "linux"); |
| 36 | + assert.equal(arch(), "x64"); |
| 37 | + |
| 38 | + try { |
| 39 | + await $`fish --version`; |
| 40 | + console.log("Able to execute `fish`. Skipping installation."); |
| 41 | + exit(0); |
| 42 | + } catch { |
| 43 | + console.log("Unable to execute `fish`. Proceeding with installation."); |
| 44 | + } |
| 45 | + |
| 46 | + const tempDir = await mkdtemp(join(tmpdir(), "install-fish-")); |
| 47 | + const tempArchive = join(tempDir, "./fish.tar.xz"); |
| 48 | + await write(tempArchive, await fetch(url)); |
| 49 | + assert.equal( |
| 50 | + new Uint8Array( |
| 51 | + await globalThis.crypto.subtle.digest( |
| 52 | + "SHA256", |
| 53 | + // The `new Uint8Array(…)` is not necessary, but it makes the type checker happy. |
| 54 | + new Uint8Array(await readFile(tempArchive)), |
| 55 | + ), |
| 56 | + ).toHex(), |
| 57 | + hash, |
| 58 | + ); |
| 59 | + |
| 60 | + await $`cd ${tempDir} && tar -xJvf ${tempArchive}`; |
| 61 | + await mkdir(dirname(BIN_INSTALLATION_PATH), { recursive: true }); |
| 62 | + await rename(join(tempDir, "fish"), BIN_INSTALLATION_PATH); |
| 63 | + |
| 64 | + console.log("Installed fish version:"); |
| 65 | + await $`fish --version`; |
| 66 | +} |
| 67 | + |
| 68 | +await installFish(); |
0 commit comments