updater.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const fetch = require('./node-fetch');
  2. /**
  3. * 更新器
  4. */
  5. const Updater = {
  6. /**
  7. * 远端地址
  8. * @type {string}
  9. */
  10. remoteUrl: 'https://gitee.com/mcv/simple-code-describe',
  11. /**
  12. * 获取远端的 package.json
  13. * @returns {Promise<object>}
  14. */
  15. async getRemotePackageJson() {
  16. const packageJsonUrl = `${this.remoteUrl}/raw/master/package.json`;
  17. // 发起网络请求
  18. const response = await fetch(packageJsonUrl, {
  19. method: 'GET',
  20. cache: 'no-cache',
  21. mode: 'no-cors',
  22. });
  23. // 请求结果
  24. if (response.status !== 200) {
  25. return null;
  26. }
  27. // 读取 json
  28. const json = response.json();
  29. return json;
  30. },
  31. async getRemoteDesc() {
  32. const descUrl = `${this.remoteUrl}/raw/master/README.md`;
  33. // 发起网络请求
  34. const response = await fetch(descUrl, {
  35. method: 'GET',
  36. cache: 'no-cache',
  37. mode: 'no-cors',
  38. });
  39. // 请求结果
  40. if (response.status !== 200) {
  41. return '';
  42. }
  43. const text = response.text();
  44. return text;
  45. },
  46. /**
  47. * 获取远端版本号
  48. * @returns {Promise<string>}
  49. */
  50. async getRemoteVersion() {
  51. const package = await this.getRemotePackageJson();
  52. if (!package) {
  53. return null;
  54. }
  55. const version = package.version || null;
  56. return version;
  57. },
  58. /**
  59. * 获取本地版本号
  60. * @returns {string}
  61. */
  62. getLocalVersion() {
  63. return require('../package.json').version;
  64. },
  65. /**
  66. * 拆分版本号
  67. * @param {string} version 版本号文本
  68. * @returns {number[]}
  69. * @example
  70. * splitVersionString('1.2.0'); // [1, 2, 0]
  71. */
  72. splitVersionString(version) {
  73. return (
  74. version.replace(/-/g, '.')
  75. .split('.')
  76. .map(v => (parseInt(v) || 0))
  77. );
  78. },
  79. /**
  80. * 对比版本号
  81. * @param {string} a 版本 a
  82. * @param {string} b 版本 b
  83. * @returns {-1 | 0 | 1}
  84. * @example
  85. * compareVersion('1.0.0', '1.0.1'); // -1
  86. * compareVersion('1.1.0', '1.1.0'); // 0
  87. * compareVersion('1.2.1', '1.2.0'); // 1
  88. * compareVersion('1.2.0.1', '1.2.0'); // 1
  89. */
  90. compareVersion(a, b) {
  91. const acs = this.splitVersionString(a),
  92. bcs = this.splitVersionString(b);
  93. const count = Math.max(acs.length, bcs.length);
  94. for (let i = 0; i < count; i++) {
  95. const ac = acs[i],
  96. bc = bcs[i];
  97. // 前者缺少分量或前者小于后者
  98. if (ac == null || ac < bc) {
  99. return -1;
  100. }
  101. // 后者缺少分量或前者大于后者
  102. if (bc == null || ac > bc) {
  103. return 1;
  104. }
  105. }
  106. return 0;
  107. },
  108. /**
  109. * 检查远端是否有新版本
  110. * @returns {Promise<boolean>}
  111. */
  112. async check() {
  113. // 远端版本号
  114. const remoteVersion = await this.getRemoteVersion();
  115. if (!remoteVersion) {
  116. return '';
  117. }
  118. // 本地版本号
  119. const localVersion = this.getLocalVersion();
  120. // 对比版本号
  121. const result = this.compareVersion(localVersion, remoteVersion);
  122. const remoteDesc = await this.getRemoteDesc() || '';
  123. return (result < 0) ? remoteDesc : '';
  124. },
  125. };
  126. module.exports = Updater;