|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import logSymbols from "log-symbols"; |
| 3 | +import os from "os"; |
| 4 | +import axios from "axios"; |
| 5 | +import fs from "fs"; |
| 6 | +import { createGunzip } from "zlib"; |
| 7 | +import tar from "tar-fs"; |
| 8 | +import { promisify } from "util"; |
| 9 | +import { pipeline } from "stream"; |
| 10 | +import path from "path"; |
| 11 | +export function sourceShellConfig() { |
| 12 | + const shell = execSync("echo $SHELL", { encoding: "utf-8" }).trim(); |
| 13 | + if (shell.includes("bash")) { |
| 14 | + process.env.PATH = execSync("echo $PATH", { encoding: "utf-8" }).trim(); |
| 15 | + } |
| 16 | + else if (shell.includes("zsh")) { |
| 17 | + process.env.PATH = execSync('zsh -c "echo $PATH"', { |
| 18 | + encoding: "utf-8", |
| 19 | + }).trim(); |
| 20 | + } |
| 21 | + else if (shell.includes("fish")) { |
| 22 | + process.env.PATH = execSync('fish -c "echo $PATH"', { |
| 23 | + encoding: "utf-8", |
| 24 | + }).trim(); |
| 25 | + } |
| 26 | +} |
| 27 | +export function exec(cmd, options = {}) { |
| 28 | + return execSync(cmd, { |
| 29 | + encoding: "utf-8", |
| 30 | + stdio: "pipe", |
| 31 | + ...options, |
| 32 | + }); |
| 33 | +} |
| 34 | +export async function installBB(version, spinner) { |
| 35 | + let architecture = os.arch(); |
| 36 | + if (architecture === "arm64") { |
| 37 | + architecture = "aarch64"; |
| 38 | + } |
| 39 | + else if (architecture === "x64") { |
| 40 | + architecture = "x86_64"; |
| 41 | + } |
| 42 | + let platform = os.platform(); |
| 43 | + if (platform === "darwin") { |
| 44 | + platform = "apple-darwin"; |
| 45 | + } |
| 46 | + else if (platform === "linux") { |
| 47 | + platform = "linux-gnu"; |
| 48 | + } |
| 49 | + const home = os.homedir(); |
| 50 | + const bbPath = path.join(home, ".bb"); |
| 51 | + spinner.start(`Installing to ${bbPath}`); |
| 52 | + const tempTarPath = path.join(fs.mkdtempSync("bb-"), "temp.tar.gz"); |
| 53 | + if (!["x86_64", "aarch64"].includes(architecture) || |
| 54 | + !["linux-gnu", "apple-darwin"].includes(platform)) { |
| 55 | + throw new Error(`Unsupported architecture ${architecture} and platform ${platform}`); |
| 56 | + } |
| 57 | + const releaseUrl = `https://github.com/AztecProtocol/aztec-packages/releases/download/aztec-packages-v${version}`; |
| 58 | + const binaryUrl = `${releaseUrl}/barretenberg-${architecture}-${platform}.tar.gz`; |
| 59 | + const response = await axios.get(binaryUrl, { responseType: "stream" }); |
| 60 | + const pipelineAsync = promisify(pipeline); |
| 61 | + await pipelineAsync(response.data, fs.createWriteStream(tempTarPath)); |
| 62 | + await pipelineAsync(fs.createReadStream(tempTarPath), createGunzip(), tar.extract(bbPath)); |
| 63 | + fs.rmSync(path.dirname(tempTarPath), { recursive: true }); |
| 64 | + spinner.stopAndPersist({ |
| 65 | + text: `Installed barretenberg to ${bbPath}`, |
| 66 | + symbol: logSymbols.success, |
| 67 | + }); |
| 68 | +} |
0 commit comments