aarch.mjs 3.4 KB

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