pre-dev.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import fs from "fs-extra";
  2. import path from "path";
  3. import AdmZip from "adm-zip";
  4. import fetch from "node-fetch";
  5. import { execSync } from "child_process";
  6. const cwd = process.cwd();
  7. const CLASH_URL_PREFIX =
  8. "https://github.com/Dreamacro/clash/releases/download/premium/";
  9. const CLASH_LATEST_DATE = "2021.12.07";
  10. /**
  11. * get the correct clash release infomation
  12. */
  13. function resolveClash() {
  14. const { platform, arch } = process;
  15. let name = "";
  16. // todo
  17. if (platform === "win32" && arch === "x64") {
  18. name = `clash-windows-386`;
  19. }
  20. if (!name) {
  21. throw new Error("todo");
  22. }
  23. const isWin = platform === "win32";
  24. const zip = isWin ? "zip" : "gz";
  25. const url = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${zip}`;
  26. const exefile = `${name}${isWin ? ".exe" : ""}`;
  27. const zipfile = `${name}.${zip}`;
  28. return { url, zip, exefile, zipfile };
  29. }
  30. /**
  31. * get the sidecar bin
  32. */
  33. async function resolveSidecar() {
  34. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  35. const host = execSync("rustc -vV | grep host").toString().slice(6).trim();
  36. const ext = process.platform === "win32" ? ".exe" : "";
  37. const sidecarFile = `clash-${host}${ext}`;
  38. const sidecarPath = path.join(sidecarDir, sidecarFile);
  39. if (!(await fs.pathExists(sidecarDir))) await fs.mkdir(sidecarDir);
  40. if (await fs.pathExists(sidecarPath)) return;
  41. // download sidecar
  42. const binInfo = resolveClash();
  43. const tempDir = path.join(cwd, "pre-dev-temp");
  44. const tempZip = path.join(tempDir, binInfo.zipfile);
  45. const tempExe = path.join(tempDir, binInfo.exefile);
  46. if (!(await fs.pathExists(tempDir))) await fs.mkdir(tempDir);
  47. if (!(await fs.pathExists(tempZip))) await downloadFile(binInfo.url, tempZip);
  48. // Todo: support gz
  49. const zip = new AdmZip(tempZip);
  50. zip.getEntries().forEach((entry) => {
  51. console.log("[INFO]: entry name", entry.entryName);
  52. });
  53. zip.extractAllTo(tempDir, true);
  54. // save as sidecar
  55. await fs.rename(tempExe, sidecarPath);
  56. // delete temp dir
  57. await fs.remove(tempDir);
  58. }
  59. /**
  60. * get the Country.mmdb (not required)
  61. */
  62. async function resolveMmdb() {
  63. const url =
  64. "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb";
  65. const resPath = path.join(cwd, "src-tauri", "resources", "Country.mmdb");
  66. if (await fs.pathExists(resPath)) return;
  67. await downloadFile(url, resPath);
  68. }
  69. /**
  70. * download file and save to `path`
  71. */
  72. async function downloadFile(url, path) {
  73. console.log(`[INFO]: downloading from "${url}"`);
  74. const response = await fetch(url, {
  75. method: "GET",
  76. headers: { "Content-Type": "application/octet-stream" },
  77. });
  78. const buffer = await response.arrayBuffer();
  79. await fs.writeFile(path, new Uint8Array(buffer));
  80. }
  81. /// main
  82. resolveSidecar();
  83. resolveMmdb();