check.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import fs from "fs-extra";
  2. import zlib from "zlib";
  3. import path from "path";
  4. import AdmZip from "adm-zip";
  5. import fetch from "node-fetch";
  6. import { execSync } from "child_process";
  7. const cwd = process.cwd();
  8. const FORCE = process.argv.includes("--force");
  9. const CLASH_URL_PREFIX =
  10. "https://github.com/Dreamacro/clash/releases/download/premium/";
  11. const CLASH_LATEST_DATE = "2022.01.27";
  12. /**
  13. * get the correct clash release infomation
  14. */
  15. function resolveClash() {
  16. const { platform, arch } = process;
  17. // todo
  18. const map = {
  19. "win32-x64": "clash-windows-amd64",
  20. "darwin-x64": "clash-darwin-amd64",
  21. "darwin-arm64": "clash-darwin-arm64",
  22. "linux-x64": "clash-linux-amd64",
  23. };
  24. const name = map[`${platform}-${arch}`];
  25. if (!name) {
  26. throw new Error(`unsupport platform "${platform}-${arch}"`);
  27. }
  28. const isWin = platform === "win32";
  29. const zip = isWin ? "zip" : "gz";
  30. const url = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${zip}`;
  31. const exefile = `${name}${isWin ? ".exe" : ""}`;
  32. const zipfile = `${name}.${zip}`;
  33. return { url, zip, exefile, zipfile };
  34. }
  35. /**
  36. * get the sidecar bin
  37. */
  38. async function resolveSidecar() {
  39. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  40. const host = execSync("rustc -vV | grep host").toString().slice(6).trim();
  41. const ext = process.platform === "win32" ? ".exe" : "";
  42. const sidecarFile = `clash-${host}${ext}`;
  43. const sidecarPath = path.join(sidecarDir, sidecarFile);
  44. await fs.mkdirp(sidecarDir);
  45. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  46. // download sidecar
  47. const binInfo = resolveClash();
  48. const tempDir = path.join(cwd, "pre-dev-temp");
  49. const tempZip = path.join(tempDir, binInfo.zipfile);
  50. const tempExe = path.join(tempDir, binInfo.exefile);
  51. await fs.mkdirp(tempDir);
  52. if (!(await fs.pathExists(tempZip))) await downloadFile(binInfo.url, tempZip);
  53. if (binInfo.zip === "zip") {
  54. const zip = new AdmZip(tempZip);
  55. zip.getEntries().forEach((entry) => {
  56. console.log("[INFO]: entry name", entry.entryName);
  57. });
  58. zip.extractAllTo(tempDir, true);
  59. // save as sidecar
  60. await fs.rename(tempExe, sidecarPath);
  61. console.log(`[INFO]: unzip finished`);
  62. } else {
  63. // gz
  64. const readStream = fs.createReadStream(tempZip);
  65. const writeStream = fs.createWriteStream(sidecarPath);
  66. readStream
  67. .pipe(zlib.createGunzip())
  68. .pipe(writeStream)
  69. .on("finish", () => {
  70. console.log(`[INFO]: gunzip finished`);
  71. execSync(`chmod 755 ${sidecarPath}`);
  72. console.log(`[INFO]: chmod binary finished`);
  73. });
  74. }
  75. // delete temp dir
  76. await fs.remove(tempDir);
  77. }
  78. /**
  79. * get the Country.mmdb (not required)
  80. */
  81. async function resolveMmdb() {
  82. const url =
  83. "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb";
  84. const resDir = path.join(cwd, "src-tauri", "resources");
  85. const resPath = path.join(resDir, "Country.mmdb");
  86. if (!FORCE && (await fs.pathExists(resPath))) return;
  87. await fs.mkdirp(resDir);
  88. await downloadFile(url, resPath);
  89. }
  90. /**
  91. * download file and save to `path`
  92. */
  93. async function downloadFile(url, path) {
  94. console.log(`[INFO]: downloading from "${url}"`);
  95. const response = await fetch(url, {
  96. method: "GET",
  97. headers: { "Content-Type": "application/octet-stream" },
  98. });
  99. const buffer = await response.arrayBuffer();
  100. await fs.writeFile(path, new Uint8Array(buffer));
  101. console.log(`[INFO]: download finished "${url}"`);
  102. }
  103. /// main
  104. resolveSidecar().catch(console.error);
  105. resolveMmdb().catch(console.error);