aarch.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Build and upload assets for macOS(aarch)
  3. */
  4. import fs from "fs-extra";
  5. import path from "path";
  6. import { exit } from "process";
  7. import { createRequire } from "module";
  8. import { getOctokit, context } from "@actions/github";
  9. const require = createRequire(import.meta.url);
  10. async function resolve() {
  11. if (!process.env.GITHUB_TOKEN) {
  12. throw new Error("GITHUB_TOKEN is required");
  13. }
  14. if (!process.env.GITHUB_REPOSITORY) {
  15. throw new Error("GITHUB_REPOSITORY is required");
  16. }
  17. if (!process.env.TAURI_PRIVATE_KEY) {
  18. throw new Error("TAURI_PRIVATE_KEY is required");
  19. }
  20. if (!process.env.TAURI_KEY_PASSWORD) {
  21. throw new Error("TAURI_KEY_PASSWORD is required");
  22. }
  23. const { version } = require("../package.json");
  24. const cwd = process.cwd();
  25. const bundlePath = path.join(cwd, "src-tauri/target/release/bundle");
  26. const join = (p) => path.join(bundlePath, p);
  27. const appPathList = [
  28. join("macos/Clash Verge.aarch64.app.tar.gz"),
  29. join("macos/Clash Verge.aarch64.app.tar.gz.sig"),
  30. ];
  31. for (const appPath of appPathList) {
  32. if (fs.pathExistsSync(appPath)) {
  33. fs.removeSync(appPath);
  34. }
  35. }
  36. fs.copyFileSync(join("macos/Clash Verge.app.tar.gz"), appPathList[0]);
  37. fs.copyFileSync(join("macos/Clash Verge.app.tar.gz.sig"), appPathList[1]);
  38. const options = { owner: context.repo.owner, repo: context.repo.repo };
  39. const github = getOctokit(process.env.GITHUB_TOKEN);
  40. const { data: release } = await github.rest.repos.getReleaseByTag({
  41. ...options,
  42. tag: `v${version}`,
  43. });
  44. if (!release.id) throw new Error("failed to find the release");
  45. await uploadAssets(release.id, [
  46. join(`dmg/Clash Verge_${version}_aarch64.dmg`),
  47. ...appPathList,
  48. ]);
  49. }
  50. // From tauri-apps/tauri-action
  51. // https://github.com/tauri-apps/tauri-action/blob/dev/packages/action/src/upload-release-assets.ts
  52. async function uploadAssets(releaseId, assets) {
  53. const github = getOctokit(process.env.GITHUB_TOKEN);
  54. // Determine content-length for header to upload asset
  55. const contentLength = (filePath) => fs.statSync(filePath).size;
  56. for (const assetPath of assets) {
  57. const headers = {
  58. "content-type": "application/zip",
  59. "content-length": contentLength(assetPath),
  60. };
  61. const ext = path.extname(assetPath);
  62. const filename = path.basename(assetPath).replace(ext, "");
  63. const assetName = path.dirname(assetPath).includes(`target${path.sep}debug`)
  64. ? `${filename}-debug${ext}`
  65. : `${filename}${ext}`;
  66. console.log(`[INFO]: Uploading ${assetName}...`);
  67. try {
  68. await github.rest.repos.uploadReleaseAsset({
  69. headers,
  70. name: assetName,
  71. data: fs.readFileSync(assetPath),
  72. owner: context.repo.owner,
  73. repo: context.repo.repo,
  74. release_id: releaseId,
  75. });
  76. } catch (error) {
  77. console.log(error.message);
  78. }
  79. }
  80. }
  81. if (process.platform === "darwin" && process.arch === "arm64") {
  82. resolve();
  83. } else {
  84. console.error("invalid");
  85. exit(1);
  86. }