All files / src/scripts fetchDependencies.ts

0% Statements 0/35
0% Branches 0/13
0% Functions 0/5
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                                                                                                                                                                                     
import { remove, writeFile } from "fs-extra";
import Path from "path";
import AdmZip from "adm-zip";
import spawn from "../../src/lib/helpers/cli/spawn";
 
const buildToolsRoot = Path.join(
  Path.normalize(`${__dirname}/../../`),
  "buildTools"
);
 
const dependencies = {
  "darwin-arm64": {
    gbdk: {
      url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.3.0/gbdk-macos-arm64.tar.gz",
      type: "targz",
    },
  },
  "darwin-x64": {
    gbdk: {
      url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.3.0/gbdk-macos.tar.gz",
      type: "targz",
    },
  },
  "linux-x64": {
    gbdk: {
      url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.3.0/gbdk-linux64.tar.gz",
      type: "targz",
    },
  },
  "win32-ia32": {
    gbdk: {
      url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.3.0/gbdk-win32.zip",
      type: "zip",
    },
  },
  "win32-x64": {
    gbdk: {
      url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.3.0/gbdk-win64.zip",
      type: "zip",
    },
  },
} as const;
 
type Arch = keyof typeof dependencies;
 
const archs = Object.keys(dependencies) as Array<Arch>;
const localArch = `${process.platform}-${process.arch}`;
 
const fetchAll = process.argv.includes("--all");
const fetchArch =
  process.argv
    .find((arg) => arg.startsWith("--arch="))
    ?.replace("--arch=", "") ?? localArch;
 
const extractTarGz = async (
  archivePath: string,
  outputDir: string
): Promise<void> => {
  const res = spawn("tar", ["-zxf", archivePath, "-C", outputDir], {}, {});
  await res.completed;
};
 
const extractZip = async (
  archivePath: string,
  outputDir: string
): Promise<void> => {
  const zip = new AdmZip(archivePath);
  await zip.extractAllTo(outputDir, true);
};
 
export const fetchGBDKDependency = async (arch: Arch) => {
  const { url, type } = dependencies[arch].gbdk;
  const response = await fetch(url);
  const buffer = await response.arrayBuffer(); // Get a Buffer from the response
  const data = Buffer.from(buffer);
  const tmpPath = Path.join(buildToolsRoot, "tmp.data");
  await writeFile(tmpPath, data);
 
  const gbdkArchPath = Path.join(buildToolsRoot, arch);
 
  if (type === "targz") {
    await extractTarGz(tmpPath, gbdkArchPath);
  } else {
    await extractZip(tmpPath, gbdkArchPath);
  }
 
  await remove(tmpPath);
};
 
const main = async () => {
  for (const arch of archs) {
    Iif (fetchAll || arch === fetchArch) {
      await fetchGBDKDependency(arch);
    }
  }
};
 
main();