Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Implement update checking
Browse files Browse the repository at this point in the history
  • Loading branch information
HR committed Jun 29, 2019
1 parent 5fd05a8 commit a7ddddf
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 6,068 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.9.0
12.0.0
1 change: 1 addition & 0 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const VIEWS_BASE_URI = `file://${__dirname}/static`
module.exports = {
REPO: {
URL: 'https://github.com/HR/Crypter/',
RELEASES_API_URL: 'https://api.github.com/repos/HR/Crypter/releases/latest',
FORK: 'https://github.com/HR/Crypter/fork',
DOCS: 'https://github.com/HR/Crypter/blob/master/readme.md',
REPORT_ISSUE: 'https://github.com/HR/Crypter/issues/new'
Expand Down
26 changes: 22 additions & 4 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
******************************/
// Electron

const {app, dialog} = require('electron')
const { app, dialog } = require('electron')
// declare global constants
// MasterPass credentials global
global.creds = {}
Expand All @@ -20,19 +20,20 @@ global.paths = {
}

const logger = require('./script/logger')

const { checkUpdate } = require('./script/utils')
// Core
const Db = require('./core/Db')
// Windows
const crypter = require('./src/crypter')
const masterPassPrompt = require('./src/masterPassPrompt')
const setup = require('./src/setup')
const settings = require('./src/settings')
const {ERRORS} = require('./config')
const { ERRORS } = require('./config')
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')()

// change exec path
logger.info(`Crypter v${app.getVersion()}`)
logger.info(`AppPath: ${app.getAppPath()}`)
logger.info(`UseData Path: ${app.getPath('userData')}`)
process.chdir(app.getAppPath())
Expand Down Expand Up @@ -68,6 +69,9 @@ const initMain = function () {

// Main event handler
app.on('ready', function () {
// Check for updates
checkUpdate()
.catch((err) => { logger.warn(err) })
// Check synchronously whether paths exist
init()
.then((mainRun) => {
Expand Down Expand Up @@ -179,9 +183,23 @@ app.on('app:open-settings', () => {
}
})

app.on('app:check-updates', () => {
app.on('app:check-update', () => {
logger.verbose('APP: app:check-updates event emitted')
// Check for updates
checkUpdate()
.then((updateAvailable) => {
if (!updateAvailable) {
dialog.showMessageBox({
type: 'info',
message: 'No update available.',
detail: `You have the latest version Crypter ${app.getVersion()} :)`
})
}
})
.catch((err) => {
dialog.showErrorBox('Failed to check for update',
`An error occured while checking for update:\n ${err.message}`)
})
})

app.on('app:relaunch', () => {
Expand Down
54 changes: 54 additions & 0 deletions app/script/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const https = require('https')
const { app, dialog, shell } = require('electron')
const { REPO } = require('../config')
const USER_AGENT = 'Crypter/x Wubba Lubba Dub Dub'
const VERSION_REGEX = /[\.v]+/g
const VERSION = parseV(app.getVersion())

function parseV(str) {
return parseInt(str.replace(VERSION_REGEX, ''))
}

module.exports = {
isRenderer: function () {
// running in a web browser
Expand All @@ -10,5 +21,48 @@ module.exports = {
if (!process.type) return false

return process.type === 'renderer'
},
checkUpdate: function () {
return new Promise((resolve, reject) => {
https.get(REPO.RELEASES_API_URL, {
headers: { 'User-Agent': USER_AGENT }
}, (res) => {
let data = ''

res.on('data', (chunk) => {
data += chunk
})

res.on('end', () => {
try {
release = JSON.parse(data.toString('utf8'))
const LATEST_VERSION = parseV(release.tag_name)
if (VERSION < LATEST_VERSION) {
dialog.showMessageBox({
type: 'info',
message: `Update is available.`,
detail: `A new version Crypter ${release.tag_name} is available.\nDo you want to get it?`,
buttons: ['Get update', 'Later'],
defaultId: 0,
cancelId: 1,
icon: null
}, (response) => {
if (response === 0) {
// Update button pressed
shell.openExternal(release.html_url)
}
})
resolve(true)
} else {
resolve(false)
}
} catch (err) {
reject(err)
}
})
})
.on('error', (err) => reject(err))
})
}

}
2 changes: 1 addition & 1 deletion app/src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ if (process.platform === 'darwin') {
menu.unshift({
label: 'Crypter',
submenu: [
// { label: 'About Crypter', click() { app.emit('app:about') } },
{ label: 'About Crypter', role: 'about' },
{ label: `Version ${app.getVersion()}`, enabled: false },
{ label: 'Check for Update', click() { app.emit('app:check-update') } },
{ type: 'separator' },
{ label: 'Preferences…', click() { app.emit('app:open-settings') } },
{ type: 'separator' },
Expand Down
Loading

0 comments on commit a7ddddf

Please sign in to comment.