From 5bf0c15d3e96e8a42d9d653c13b1342ea72abd92 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sat, 17 Feb 2018 15:15:30 +0100 Subject: [PATCH] =?UTF-8?q?Implement=20version=20check=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/index.js | 9 ++++++++- src/menu.js | 15 ++++++++++++++- src/update-checker.js | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/update-checker.js diff --git a/package.json b/package.json index c226a734..8f6d6284 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "electron-is-dev": "^0.1.2", "execa": "^0.5.0", "file-type": "^4.1.0", + "github-api": "^3.0.0", "jquery": "^3.1.1", "keyboardjs": "^2.3.3", "lodash": "^4.16.4", diff --git a/src/index.js b/src/index.js index 2011c6d4..6383322b 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,8 @@ const url = require('url'); const menu = require('./menu'); +const { checkNewVersion } = require('./update-checker'); + const app = electron.app; const BrowserWindow = electron.BrowserWindow; @@ -37,9 +39,14 @@ function createWindow() { // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. -app.on('ready', () => { +app.on('ready', async () => { createWindow(); menu(app, mainWindow); + + const newVersion = await checkNewVersion(); + if (newVersion) { + menu(app, mainWindow, newVersion); + } }); // Quit when all windows are closed. diff --git a/src/menu.js b/src/menu.js index da8b4fa5..3fee38d1 100644 --- a/src/menu.js +++ b/src/menu.js @@ -5,8 +5,9 @@ const Menu = electron.Menu; const dialog = electron.dialog; const homepage = 'https://github.com/mifi/lossless-cut'; +const releasesPage = 'https://github.com/mifi/lossless-cut/releases'; -module.exports = (app, mainWindow) => { +module.exports = (app, mainWindow, newVersion) => { const menu = defaultMenu(app, electron.shell); const editMenuIndex = menu.findIndex(item => item.Label === 'Edit'); @@ -40,5 +41,17 @@ module.exports = (app, mainWindow) => { }); } + if (newVersion) { + menu.push({ + label: 'New version!', + submenu: [ + { + label: `Download ${newVersion}`, + click() { electron.shell.openExternal(releasesPage); }, + }, + ], + }); + } + Menu.setApplicationMenu(Menu.buildFromTemplate(menu)); }; diff --git a/src/update-checker.js b/src/update-checker.js new file mode 100644 index 00000000..5c689eaa --- /dev/null +++ b/src/update-checker.js @@ -0,0 +1,26 @@ +const GitHub = require('github-api'); +const electron = require('electron'); + +const app = electron.app; + +const gh = new GitHub(); +const repo = gh.getRepo('mifi', 'lossless-cut'); + +async function checkNewVersion() { + try { + const res = (await repo.getRelease('latest')).data; + const tagName = res.tag_name; + console.log(tagName); + + const currentVersion = app.getVersion(); + // const currentVersion = '1.8.0'; + + if (tagName !== `v${currentVersion}`) return tagName; + return undefined; + } catch (e) { + console.error('Failed to check github version'); + return undefined; + } +} + +module.exports = { checkNewVersion };