updater-fixed-webview2.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. },
  37. };
  38. const promises = latestRelease.assets.map(async (asset) => {
  39. const { name, browser_download_url } = asset;
  40. // win64 url
  41. if (name.endsWith("x64_fixed_webview2-setup.nsis.zip")) {
  42. updateData.platforms["windows-x86_64"].url = browser_download_url;
  43. }
  44. // win64 signature
  45. if (name.endsWith("x64_fixed_webview2-setup.nsis.zip.sig")) {
  46. const sig = await getSignature(browser_download_url);
  47. updateData.platforms["windows-x86_64"].signature = sig;
  48. }
  49. // win32 url
  50. if (name.endsWith("x86_fixed_webview2-setup.nsis.zip")) {
  51. updateData.platforms["windows-x86"].url = browser_download_url;
  52. }
  53. // win32 signature
  54. if (name.endsWith("x86_fixed_webview2-setup.nsis.zip.sig")) {
  55. const sig = await getSignature(browser_download_url);
  56. updateData.platforms["windows-x86"].signature = sig;
  57. }
  58. // win arm url
  59. if (name.endsWith("arm64_fixed_webview2-setup.nsis.zip")) {
  60. updateData.platforms["windows-aarch64"].url = browser_download_url;
  61. }
  62. // win arm signature
  63. if (name.endsWith("arm64_fixed_webview2-setup.nsis.zip.sig")) {
  64. const sig = await getSignature(browser_download_url);
  65. updateData.platforms["windows-aarch64"].signature = sig;
  66. }
  67. });
  68. await Promise.allSettled(promises);
  69. console.log(updateData);
  70. // maybe should test the signature as well
  71. // delete the null field
  72. Object.entries(updateData.platforms).forEach(([key, value]) => {
  73. if (!value.url) {
  74. console.log(`[Error]: failed to parse release for "${key}"`);
  75. delete updateData.platforms[key];
  76. }
  77. });
  78. // 生成一个代理github的更新文件
  79. // 使用 https://hub.fastgit.xyz/ 做github资源的加速
  80. const updateDataNew = JSON.parse(JSON.stringify(updateData));
  81. Object.entries(updateDataNew.platforms).forEach(([key, value]) => {
  82. if (value.url) {
  83. updateDataNew.platforms[key].url =
  84. "https://mirror.ghproxy.com/" + value.url;
  85. } else {
  86. console.log(`[Error]: updateDataNew.platforms.${key} is null`);
  87. }
  88. });
  89. // update the update.json
  90. const { data: updateRelease } = await github.rest.repos.getReleaseByTag({
  91. ...options,
  92. tag: UPDATE_TAG_NAME,
  93. });
  94. // delete the old assets
  95. for (let asset of updateRelease.assets) {
  96. if (asset.name === UPDATE_JSON_FILE) {
  97. await github.rest.repos.deleteReleaseAsset({
  98. ...options,
  99. asset_id: asset.id,
  100. });
  101. }
  102. if (asset.name === UPDATE_JSON_PROXY) {
  103. await github.rest.repos
  104. .deleteReleaseAsset({ ...options, asset_id: asset.id })
  105. .catch(console.error); // do not break the pipeline
  106. }
  107. }
  108. // upload new assets
  109. await github.rest.repos.uploadReleaseAsset({
  110. ...options,
  111. release_id: updateRelease.id,
  112. name: UPDATE_JSON_FILE,
  113. data: JSON.stringify(updateData, null, 2),
  114. });
  115. await github.rest.repos.uploadReleaseAsset({
  116. ...options,
  117. release_id: updateRelease.id,
  118. name: UPDATE_JSON_PROXY,
  119. data: JSON.stringify(updateDataNew, null, 2),
  120. });
  121. }
  122. // get the signature file content
  123. async function getSignature(url) {
  124. const response = await fetch(url, {
  125. method: "GET",
  126. headers: { "Content-Type": "application/octet-stream" },
  127. });
  128. return response.text();
  129. }
  130. resolveUpdater().catch(console.error);