Browse Source

添加检查更新功能(在“帮助”菜单中)。 #99

oldj 8 years ago
parent
commit
ae8e20001e
10 changed files with 134 additions and 19 deletions
  1. 96 0
      app/bg/check_for_update.js
  2. 5 12
      app/build/bundle.js
  3. 4 0
      app/common/lang/cn.js
  4. 4 0
      app/common/lang/en.js
  5. 1 0
      app/main.js
  6. 2 1
      app/package.json
  7. 10 3
      app/src/lang.js
  8. 10 1
      app/src/modules/mainMenu.js
  9. 1 1
      app/version.js
  10. 1 1
      package.json

+ 96 - 0
app/bg/check_for_update.js

@@ -0,0 +1,96 @@
+/**
+ * @author oldj
+ * @blog http://oldj.net
+ */
+
+'use strict';
+
+const request = require('request');
+const cheerio = require('cheerio');
+const {shell, dialog} = require('electron');
+const release_url = 'https://github.com/oldj/SwitchHosts/releases';
+const current_version = require('../version').version;
+const m_lang = require('../src/lang');
+const lang = m_lang.getLang(global.user_language);
+
+function convertStrVersion(v) {
+    let a = v.match(/\d+/g);
+    return a.map(i => parseInt(i));
+}
+
+function compareVersion(a, b) {
+    if (typeof a === 'string') {
+        a = convertStrVersion(a);
+    }
+    if (typeof b === 'string') {
+        b = convertStrVersion(b);
+    }
+
+    let len = Math.max(a.length, b.length);
+    for (let i = 0; i < len; i++) {
+        let ai = a[i];
+        let bi = b[i];
+
+        if (typeof ai === 'number' && typeof bi === 'number') {
+            if (ai === bi) {
+                continue;
+            }
+
+            return ai - bi;
+        }
+
+        if (typeof ai === 'number' && typeof bi !== 'number') {
+            return 1;
+        }
+        if (typeof ai !== 'number' && typeof bi === 'number') {
+            return -1;
+        }
+        return 0;
+    }
+}
+
+exports.check = () => {
+    console.log('start check updates..');
+    request(release_url, (err, res, body) => {
+        if (err) {
+            console.log(err);
+            dialog.showMessageBox({
+                type: 'error',
+                message: lang.check_update_err
+            });
+            return;
+        }
+
+        let $ = cheerio.load(body);
+        let a = $('.release-meta .css-truncate-target');
+        if (a.length <= 0) {
+            console.log('not found versios!');
+            return;
+        }
+        let last_v = $(a[0]).text();
+        // Array.from(a).map(i => {
+        //     console.log($(i).text());
+        // });
+
+        let cmp = compareVersion(current_version, last_v);
+        console.log('cmp', cmp);
+        let message;
+        let buttons = [lang.ok];
+        if (cmp >= 0) {
+            message = m_lang.fill(lang.check_update_nofound);
+        } else {
+            message = m_lang.fill(lang.check_update_found, last_v);
+            buttons.unshift(lang.cancel);
+        }
+
+        dialog.showMessageBox({
+            type: 'info',
+            message,
+            buttons
+        }, (res) => {
+            if (cmp < 0 && res === 1) {
+                shell.openExternal(release_url);
+            }
+        });
+    });
+};

File diff suppressed because it is too large
+ 5 - 12
app/build/bundle.js


+ 4 - 0
app/src/lang/cn.js → app/common/lang/cn.js

@@ -13,6 +13,10 @@ exports.content = {
     , auto_refresh: '自动更新'
     , bad_url: 'URL 地址有误。'
     , cancel: '取消'
+    , check_update: '检查更新'
+    , check_update_err: '检查更新出错,请稍后再试。:-('
+    , check_update_found: '发现新版本 ${0},前往下载?'
+    , check_update_nofound: '当前版本已是最新版本。'
     , comment: '注释'
     , confirm_del: '确定要删除此 host 吗?'
     , confirm_import: '确定要导入吗?原方案列表将被覆盖,此操作不可撤销。'

+ 4 - 0
app/src/lang/en.js → app/common/lang/en.js

@@ -13,6 +13,10 @@ exports.content = {
     , auto_refresh: 'Auto refresh'
     , bad_url: 'URL is not valid.'
     , cancel: 'Cancel'
+    , check_update: 'Check for updates'
+    , check_update_err: 'Something went wrong while checking updates, please try again later. :-('
+    , check_update_found: 'New version ${0} is avaliable, download it now?'
+    , check_update_nofound: 'Current version is the latest version.'
     , comment: 'Comment'
     , confirm_del: 'Are you sure you want to delete this host?'
     , confirm_import: 'You sure you want to import it? The original rules will be overwriten, this operation can not be undone.'

+ 1 - 0
app/main.js

@@ -20,6 +20,7 @@ const BrowserWindow = electron.BrowserWindow;
 const tray = require('./src/modules/tray');
 const pref = require('./src/libs/pref');
 let user_language = pref.get('user_language') || (app.getLocale() || '').split('-')[0].toLowerCase() || 'en';
+global.user_language = user_language;
 
 // Keep a global reference of the window object, if you don't, the window will
 // be closed automatically when the JavaScript object is garbage collected.

+ 2 - 1
app/package.json

@@ -16,6 +16,7 @@
   },
   "homepage": "https://oldj.github.io/SwitchHosts/",
   "dependencies": {
+    "cheerio": "^0.22.0",
     "classnames": "^2.2.5",
     "codemirror": "^5.17.0",
     "moment": "^2.14.1",
@@ -23,7 +24,7 @@
     "react": "^15.3.1",
     "react-addons-update": "^15.3.1",
     "react-dom": "^15.3.1",
-    "request": "^2.74.0",
+    "request": "^2.79.0",
     "yargs": "^6.6.0"
   },
   "devDependencies": {}

+ 10 - 3
app/src/lang.js

@@ -6,8 +6,8 @@
 "use strict";
 
 const languages = {
-    'en': require('./lang/en').content,
-    'cn': require('./lang/cn').content
+    'en': require('../common/lang/en').content,
+    'cn': require('../common/lang/cn').content
 };
 
 module.exports = {
@@ -24,7 +24,7 @@ module.exports = {
         }
         return list;
     })(),
-    getLang: function (lang) {
+    getLang: (lang) => {
         lang = lang.toLowerCase();
         if (lang == 'cn' || lang == 'zh-cn') {
             lang = 'cn';
@@ -32,5 +32,12 @@ module.exports = {
             lang = 'en';
         }
         return languages[lang] || languages['en'];
+    },
+    fill: (tpl, ...vals) => {
+        vals.map((v, idx) => {
+            let r = new RegExp('\\$\\{' + idx + '\\}', 'g');
+            tpl = tpl.replace(r, v);
+        });
+        return tpl;
     }
 };

+ 10 - 1
app/src/modules/mainMenu.js

@@ -156,6 +156,14 @@ exports.init = function (app, sys_lang = 'en') {
             label: lang.help,
             role: 'help',
             submenu: [{
+
+                label: lang.check_update,
+                click () {
+                    require('../../bg/check_for_update').check();
+                }
+            }, {
+                type: 'separator'
+            }, {
                 label: lang.feedback,
                 click () {
                     shell.openExternal('https://github.com/oldj/SwitchHosts/issues');
@@ -293,4 +301,5 @@ exports.init = function (app, sys_lang = 'en') {
 
     const menu = Menu.buildFromTemplate(template);
     Menu.setApplicationMenu(menu);
-};
+}
+;

+ 1 - 1
app/version.js

@@ -1 +1 @@
-exports.version = [3,2,2,4247];
+exports.version = [3,2,2,4250];

+ 1 - 1
package.json

@@ -44,4 +44,4 @@
     "webpack": "^1.14.0",
     "yargs": "^6.5.0"
   }
-}
+}

Some files were not shown because too many files changed in this diff