updater-fixed-webview2.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import fetch from "node-fetch";
  2. import { getOctokit, context } from "@actions/github";
  3. import { resolveUpdateLog } from "./updatelog.mjs";
  4. const UPDATE_TAG_NAME = "updater";
  5. const UPDATE_JSON_FILE = "update-fixed-webview2.json";
  6. const UPDATE_JSON_PROXY = "update-fixed-webview2-proxy.json";
  7. /// generate update.json
  8. /// upload to update tag's release asset
  9. async function resolveUpdater() {
  10. if (process.env.GITHUB_TOKEN === undefined) {
  11. throw new Error("GITHUB_TOKEN is required");
  12. }
  13. const options = { owner: context.repo.owner, repo: context.repo.repo };
  14. const github = getOctokit(process.env.GITHUB_TOKEN);
  15. const { data: tags } = await github.rest.repos.listTags({
  16. ...options,
  17. per_page: 10,
  18. page: 1,
  19. });
  20. // get the latest publish tag
  21. const tag = tags.find((t) => t.name.startsWith("v"));
  22. console.log(tag);
  23. console.log();
  24. const { data: latestRelease } = await github.rest.repos.getReleaseByTag({
  25. ...options,
  26. tag: tag.name,
  27. });
  28. const updateData = {
  29. name: tag.name,
  30. notes: await resolveUpdateLog(tag.name), // use updatelog.md
  31. pub_date: new Date().toISOString(),
  32. platforms: {
  33. "windows-x86_64": { signature: "", url: "" },
  34. "windows-aarch64": { signature: "", url: "" },
  35. "windows-x86": { signature: "", url: "" },
  36. "windows-i686": { signature: "", url: "" },
  37. },
  38. };
  39. const promises = latestRelease.assets.map(async (asset) => {
  40. const { name, browser_download_url } = asset;
  41. // win64 url
  42. if (name.endsWith("x64_fixed_webview2-setup.nsis.zip")) {
  43. updateData.platforms["windows-x86_64"].url = browser_download_url;
  44. }
  45. // win64 signature
  46. if (name.endsWith("x64_fixed_webview2-setup.nsis.zip.sig")) {
  47. const sig = await getSignature(browser_download_url);
  48. updateData.platforms["windows-x86_64"].signature = sig;
  49. }
  50. // win32 url
  51. if (name.endsWith("x86_fixed_webview2-setup.nsis.zip")) {
  52. updateData.platforms["windows-x86"].url = browser_download_url;
  53. updateData.platforms["windows-i686"].url = browser_download_url;
  54. }
  55. // win32 signature
  56. if (name.endsWith("x86_fixed_webview2-setup.nsis.zip.sig")) {
  57. const sig = await getSignature(browser_download_url);
  58. updateData.platforms["windows-x86"].signature = sig;
  59. updateData.platforms["windows-i686"].signature = sig;
  60. }
  61. // win arm url
  62. if (name.endsWith("arm64_fixed_webview2-setup.nsis.zip")) {
  63. updateData.platforms["windows-aarch64"].url = browser_download_url;
  64. }
  65. // win arm signature
  66. if (name.endsWith("arm64_fixed_webview2-setup.nsis.zip.sig")) {
  67. const sig = await getSignature(browser_download_url);
  68. updateData.platforms["windows-aarch64"].signature = sig;
  69. }
  70. });
  71. await Promise.allSettled(promises);
  72. console.log(updateData);
  73. // maybe should test the signature as well
  74. // delete the null field
  75. Object.entries(updateData.platforms).forEach(([key, value]) => {
  76. if (!value.url) {
  77. console.log(`[Error]: failed to parse release for "${key}"`);
  78. delete updateData.platforms[key];
  79. }
  80. });
  81. // 生成一个代理github的更新文件
  82. // 使用 https://hub.fastgit.xyz/ 做github资源的加速
  83. const updateDataNew = JSON.parse(JSON.stringify(updateData));
  84. Object.entries(updateDataNew.platforms).forEach(([key, value]) => {
  85. if (value.url) {
  86. updateDataNew.platforms[key].url =
  87. "https://download.clashverge.dev/" + value.url;
  88. } else {
  89. console.log(`[Error]: updateDataNew.platforms.${key} is null`);
  90. }
  91. });
  92. // update the update.json
  93. const { data: updateRelease } = await github.rest.repos.getReleaseByTag({
  94. ...options,
  95. tag: UPDATE_TAG_NAME,
  96. });
  97. // delete the old assets
  98. for (let asset of updateRelease.assets) {
  99. if (asset.name === UPDATE_JSON_FILE) {
  100. await github.rest.repos.deleteReleaseAsset({
  101. ...options,
  102. asset_id: asset.id,
  103. });
  104. }
  105. if (asset.name === UPDATE_JSON_PROXY) {
  106. await github.rest.repos
  107. .deleteReleaseAsset({ ...options, asset_id: asset.id })
  108. .catch(console.error); // do not break the pipeline
  109. }
  110. }
  111. // upload new assets
  112. await github.rest.repos.uploadReleaseAsset({
  113. ...options,
  114. release_id: updateRelease.id,
  115. name: UPDATE_JSON_FILE,
  116. data: JSON.stringify(updateData, null, 2),
  117. });
  118. await github.rest.repos.uploadReleaseAsset({
  119. ...options,
  120. release_id: updateRelease.id,
  121. name: UPDATE_JSON_PROXY,
  122. data: JSON.stringify(updateDataNew, null, 2),
  123. });
  124. }
  125. // get the signature file content
  126. async function getSignature(url) {
  127. const response = await fetch(url, {
  128. method: "GET",
  129. headers: { "Content-Type": "application/octet-stream" },
  130. });
  131. return response.text();
  132. }
  133. resolveUpdater().catch(console.error);