chk.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. "use strict";
  6. const http = require('http');
  7. const config = require('../config');
  8. const lang = require('./lang').getLang('en');
  9. function compareVersion(v1, v2) {
  10. if (v1 == v2) return 0;
  11. let a1 = v1.split('.');
  12. let a2 = v2.split('.');
  13. let i;
  14. let l = Math.min(a1.length, a2.length);
  15. let c1;
  16. let c2;
  17. for (i = 0; i < l; i ++) {
  18. c1 = parseInt(a1[i]);
  19. c2 = parseInt(a2[i]);
  20. if (isNaN(c2) || c1 > c2) {
  21. return 1;
  22. } else if (c1 < c2) {
  23. return -1;
  24. }
  25. }
  26. return 0;
  27. }
  28. function chkUpdate(current_version, win) {
  29. const dialog = require('electron').dialog;
  30. http.get(config.url_chk_version, function (res) {
  31. let s = '';
  32. res.on('data', (c) => {
  33. s += c;
  34. });
  35. res.on('end', () => {
  36. //console.log(s);
  37. let new_version = s.replace(/^\s+|\s+$/g, '');
  38. if (compareVersion(current_version, new_version) < 0) {
  39. // new version available
  40. dialog.showMessageBox(win, {
  41. type: 'info'
  42. , buttons: ['cancel', 'YES']
  43. , title: 'New version found!'
  44. , message: lang.new_version_available + '\n\nv: ' + new_version
  45. }, function (c) {
  46. if (c == 1) {
  47. require('electron').shell.openExternal(config.url_homepage);
  48. }
  49. });
  50. } else {
  51. dialog.showMessageBox(win, {
  52. type: 'info'
  53. , buttons: ['OK']
  54. , title: 'You are up to date!'
  55. , message: lang.is_updated
  56. });
  57. }
  58. });
  59. }).on('error', function (e) {
  60. dialog.showMessageBox(win, {
  61. type: 'error'
  62. , buttons: ['OK']
  63. , title: 'Error'
  64. , message: e.message
  65. });
  66. });
  67. }
  68. exports.chkUpdate = chkUpdate;