github.com/anycable/anycable-go@v1.5.1/packaging/npm/install.mjs (about)

     1  import { spawnSync } from "child_process";
     2  import { chmodSync } from "fs";
     3  
     4  const iswin = ["win32", "cygwin"].includes(process.platform);
     5  
     6  export async function install() {
     7    if (process.env.CI) {
     8      return;
     9    }
    10    const exePath = await downloadBinary();
    11    if (!iswin) {
    12      chmodSync(exePath, "755");
    13    }
    14  
    15    // verify installation
    16    spawnSync(exePath, ["-v"], {
    17      stdio: "inherit",
    18    });
    19  }
    20  
    21  const baseReleaseUrl =
    22    "https://github.com/anycable/anycable-go/releases/download/";
    23  
    24  import packageJson from "./package.json" assert { type: "json" };
    25  const packageVersion = packageJson.version;
    26  const version = packageVersion.replace(/-patch.*$/, "");
    27  
    28  function getDownloadURL() {
    29    // Detect OS
    30    // https://nodejs.org/api/process.html#process_process_platform
    31    let downloadOS = process.platform;
    32    let extension = "";
    33    if (iswin) {
    34      downloadOS = "win";
    35      extension = ".exe";
    36    }
    37  
    38    // Detect architecture
    39    // https://nodejs.org/api/process.html#process_process_arch
    40    let arch = process.arch;
    41  
    42    // Based on https://github.com/anycable/anycable-rails/blob/master/lib/generators/anycable/with_os_helpers.rb#L20
    43    switch (process.arch) {
    44      case "x64": {
    45        arch = "amd64";
    46        break;
    47      }
    48    }
    49  
    50    return `${baseReleaseUrl}/v${version}/anycable-go-${downloadOS}-${arch}${extension}`;
    51  }
    52  
    53  import { DownloaderHelper } from "node-downloader-helper";
    54  import * as path from "path";
    55  import { fileURLToPath } from "url";
    56  
    57  const __dirname = path.dirname(fileURLToPath(import.meta.url));
    58  
    59  async function downloadBinary() {
    60    // TODO zip the binaries to reduce the download size
    61    const downloadURL = getDownloadURL();
    62    const extension = iswin ? ".exe" : "";
    63    const fileName = `anycable-go${extension}`;
    64    const binDir = path.join(__dirname, "bin");
    65    const dl = new DownloaderHelper(downloadURL, binDir, {
    66      fileName,
    67      retry: { maxRetries: 5, delay: 50 },
    68    });
    69  
    70    console.log("Downloading anycable-go binary from " + downloadURL + "...");
    71    dl.on("end", () => console.log("anycable-go binary was downloaded"));
    72    try {
    73      await dl.start();
    74    } catch (e) {
    75      const message = `Failed to download ${fileName}: ${e.message} while fetching ${downloadURL}`;
    76      console.error(message);
    77      throw new Error(message);
    78    }
    79    return path.join(binDir, fileName);
    80  }