Skip to content

Commit

Permalink
handle updates in main process, fix unnecessary update download bug
Browse files Browse the repository at this point in the history
  • Loading branch information
danenania committed Nov 9, 2017
1 parent 3b269ee commit 71f2b60
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 148 deletions.
4 changes: 2 additions & 2 deletions buildmac.sh
Expand Up @@ -11,8 +11,8 @@ echo "Copying assets to envkey-electron"
cp -r envkey-assets/build/fonts envkey-assets/build/images envkey-assets/build/stylesheets envkey-electron/assets/
cp envkey-assets/build/javascripts/* envkey-assets/build/*.js envkey-electron/assets/

echo "Building dmg only without signing"
export CSC_IDENTITY_AUTO_DISCOVERY=false
# echo "Building dmg only without signing"
# export CSC_IDENTITY_AUTO_DISCOVERY=false
(cd envkey-electron && npm run distmac)

echo "Clearing middleman build and electron assets"
Expand Down
21 changes: 21 additions & 0 deletions buildmac_debug.sh
@@ -0,0 +1,21 @@
#!/bin/bash
echo "Building middleman assets for production"
(export ASSET_HOST=assets DEBUG_BUILD=true && ./build_prod.sh)

echo "Clearing envkey-electron/assets and envkey-electron/dist"
rm -rf envkey-electron/assets/*
rm -rf envkey-electron/dist/*
rm envkey-assets/source/javascripts/*

echo "Copying assets to envkey-electron"
cp -r envkey-assets/build/fonts envkey-assets/build/images envkey-assets/build/stylesheets envkey-electron/assets/
cp envkey-assets/build/javascripts/* envkey-assets/build/*.js envkey-electron/assets/

# echo "Building dmg only without signing"
# export CSC_IDENTITY_AUTO_DISCOVERY=false
(cd envkey-electron && npm run distmac)

echo "Clearing middleman build and electron assets"
rm -rf envkey-assets/build/*
rm -rf envkey-electron/assets/*
rm envkey-assets/source/javascripts/*
57 changes: 57 additions & 0 deletions envkey-electron/main-process/updates.js
@@ -0,0 +1,57 @@
const updater = require('electron-simple-updater'),
{dialog, BrowserWindow} = require('electron'),
logger = require('electron-log')

let versionAvailable,
versionDownloaded,
checkingUpdates = false

logger.transports.file.level = 'info'

// Auto-update
updater.init({logger, autoDownload: false, checkUpdateOnStart: false})

const
onUpdateDownloaded = ()=>{
logger.info("Update downloaded.")
versionDownloaded = versionAvailable
promptRestart()
},

onUpdateAvailable = meta =>{
logger.info("Update available.")
if(meta.version == versionAvailable)return
versionAvailable = meta.version
updater.downloadUpdate()
},

promptRestart = ()=> {
let choice = dialog.showMessageBox({
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: `EnvKey has auto-updated to v${versionDownloaded}. Do you want to restart with the latest version?`
})

if(choice === 0){
updater.quitAndInstall()
}
}

module.exports = {

listenUpdater: ()=>{
if(checkingUpdates)return

updater.on('update-available', onUpdateAvailable)
updater.on('update-downloaded', onUpdateDownloaded)
updater.on('error', err => logger.error(err))

// Check for updates every 10 minutes
setInterval(()=> updater.checkForUpdates(), 1000 * 60 * 10)

updater.checkForUpdates()

checkingUpdates = true
}
}
10 changes: 4 additions & 6 deletions envkey-electron/main.js
Expand Up @@ -4,18 +4,16 @@ const
url = require('url'),
isDev = require('electron-is-dev'),
createMenu = require('./main-process/create_menu'),
logger = require("electron-log"),
updater = require('electron-simple-updater'),
jsonfile = require('jsonfile'),
{listenUpdater} = require('./main-process/updates'),
{app, BrowserWindow, ipcMain} = electron

// Start auto-update listener
listenUpdater()

// 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.
let win, stripeWin

// Auto-update
updater.init({logger, autoDownload: false})

function createWindow () {
// Create the browser window.
const {width: screenW, height: screenH} = electron.screen.getPrimaryDisplay().workAreaSize
Expand Down
13 changes: 3 additions & 10 deletions envkey-electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions envkey-electron/package.json
@@ -1,6 +1,6 @@
{
"name": "EnvKey",
"version": "1.1.2",
"version": "1.1.3",
"description": "EnvKey's cross-platform native app",
"author": "Dane Schneider <dane@envkey.com> (https://www.envkey.com)",
"homepage": "https://www.envkey.com",
Expand Down Expand Up @@ -37,8 +37,7 @@
"dependencies": {
"electron-is-dev": "^0.3.0",
"electron-log": "^2.2.9",
"electron-simple-updater": "^1.2.1",
"jsonfile": "^4.0.0"
"electron-simple-updater": "^1.2.1"
},
"devDependencies": {
"ajv": "^5.0.0",
Expand Down
8 changes: 1 addition & 7 deletions envkey-electron/preload.js
@@ -1,16 +1,10 @@
const electron = require("electron"),
{clipboard, remote, ipcRenderer, shell} = electron,
updater = remote.require("electron-simple-updater")
{clipboard, remote, ipcRenderer, shell} = electron

window.copy = s => {
clipboard.writeText(s)
return true
}

window.ipc = ipcRenderer
window.updater = updater
window.shell = shell

window.installUpdate = ()=> {
updater.quitAndInstall()
}
24 changes: 12 additions & 12 deletions envkey-electron/updates.json
Expand Up @@ -5,23 +5,23 @@
"version": "1.1.3"
},
"linux-x64-prod": {
"update": "https://github.com/envkey/envkey-app/releases/download/linux-x64-prod-v1.1.2/EnvKey-1.1.2-x86_64.AppImage",
"install": "https://github.com/envkey/envkey-app/releases/download/linux-x64-prod-v1.1.2/EnvKey-1.1.2-x86_64.AppImage",
"version": "1.1.2"
"update": "https://github.com/envkey/envkey-app/releases/download/linux-x64-prod-v1.1.3/EnvKey-1.1.3-x86_64.AppImage",
"install": "https://github.com/envkey/envkey-app/releases/download/linux-x64-prod-v1.1.3/EnvKey-1.1.3-x86_64.AppImage",
"version": "1.1.3"
},
"win32-x64-prod": {
"update": "https://github.com/envkey/envkey-app/releases/download/win32-x64-prod-v1.1.2",
"install": "https://github.com/envkey/envkey-app/releases/download/win32-x64-prod-v1.1.2/EnvKey Setup 1.1.2.exe",
"version": "1.1.2"
"update": "https://github.com/envkey/envkey-app/releases/download/win32-x64-prod-v1.1.3",
"install": "https://github.com/envkey/envkey-app/releases/download/win32-x64-prod-v1.1.3/EnvKey Setup 1.1.3.exe",
"version": "1.1.3"
},
"win32-ia32-prod": {
"update": "https://github.com/envkey/envkey-app/releases/download/win32-ia32-prod-v1.1.2",
"install": "https://github.com/envkey/envkey-app/releases/download/win32-ia32-prod-v1.1.2/EnvKey Setup 1.1.2.exe",
"version": "1.1.2"
"update": "https://github.com/envkey/envkey-app/releases/download/win32-ia32-prod-v1.1.3",
"install": "https://github.com/envkey/envkey-app/releases/download/win32-ia32-prod-v1.1.3/EnvKey Setup 1.1.3.exe",
"version": "1.1.3"
},
"darwin-x64-prod": {
"update": "https://github.com/envkey/envkey-app/releases/download/darwin-x64-prod-v1.1.2/release.json",
"install": "https://github.com/envkey/envkey-app/releases/download/darwin-x64-prod-v1.1.2/EnvKey-1.1.2.dmg",
"version": "1.1.2"
"update": "https://github.com/envkey/envkey-app/releases/download/darwin-x64-prod-v1.1.3/release.json",
"install": "https://github.com/envkey/envkey-app/releases/download/darwin-x64-prod-v1.1.3/EnvKey-1.1.3.dmg",
"version": "1.1.3"
}
}
3 changes: 0 additions & 3 deletions envkey-react/src/index.js
Expand Up @@ -5,7 +5,6 @@ import Routes from 'routes'
import R from 'ramda'
import * as crypto from 'lib/crypto'
import {startConnectionWatcher, startReactivatedWatcher, startWindowFocusWatcher} from 'lib/status'
import {listenUpdater} from 'lib/updates'

ReactDOM.render(<Routes />, document.getElementById('wrap'))

Expand All @@ -15,5 +14,3 @@ document.body.className += " react-loaded"

startConnectionWatcher()
startReactivatedWatcher()
listenUpdater()
// startWindowFocusWatcher()
6 changes: 3 additions & 3 deletions envkey-react/src/init_redux.js
Expand Up @@ -25,14 +25,14 @@ const

loggerOpts = {
level: {
prevState: devMode ? "info" : false,
nextState: devMode ? "info" : false,
prevState: (devMode || debugBuild) ? "info" : false,
nextState: (devMode || debugBuild) ? "info" : false,
action: "info",
error: "info"
}
}

if (!devMode){
if (!devMode && !debugBuild){
loggerOpts.actionTransformer = action => {
const props = ["type", "error"]
if (action.error) props.push("payload")
Expand Down
30 changes: 0 additions & 30 deletions envkey-react/src/lib/status/index.js
@@ -1,6 +1,5 @@
import {store} from 'init_redux'
import {DISCONNECTED, REACTIVATED_BRIEF, REACTIVATED_LONG} from 'actions'
import {promptRestartIfUpdateDownloaded} from 'lib/updates'

let isCheckingConnection = false,
isCheckingActive = false,
Expand Down Expand Up @@ -72,26 +71,6 @@ const

lastActiveAt = time
setTimeout(checkReactivated, 4000)
},

onUserAction = ()=> {
lastUserActionAt = Date.now()
},

onWindowFocus = ()=>{
// If it's been at least a minute since any user action and an update is available, prompt for restart
const time = Date.now(),
sinceUserAction = time - lastUserActionAt

console.log("Window focused")
console.log("Since user action: ", sinceUserAction)

if (sinceUserAction > (1000 * 60)){
console.log("prompting for restart if update downloaded")
promptRestartIfUpdateDownloaded()
}

lastUserActionAt = Date.now()
}

export const
Expand All @@ -104,16 +83,7 @@ export const
lastActiveAt = Date.now()
checkReactivated()
}
},

startWindowFocusWatcher = ()=>{
document.body.addEventListener('click', onUserAction)
document.body.addEventListener('mousemove', onUserAction)
document.body.addEventListener('keydown', onUserAction)

window.addEventListener('focus', onWindowFocus)
}




56 changes: 0 additions & 56 deletions envkey-react/src/lib/updates/index.js

This file was deleted.

17 changes: 6 additions & 11 deletions envkey-react/src/sagas/crypto_sagas.js
Expand Up @@ -368,20 +368,15 @@ function *onDecryptAll(action){
const skipVerify = R.path(["meta", "skipVerifyCurrentUser"], action)
if (!skipVerify) verifyRes = yield call(verifyCurrentUser, background)

let decryptErr
if((skipVerify && !verifyRes) || !verifyRes.error){
try {
const hasEnvParents = yield call(decryptAllEnvParents, firstTarget, background)
if (!hasEnvParents){
yield put({type: DECRYPT_ALL_SUCCESS})
}
} catch (err){
decryptErr = err
const hasEnvParents = yield call(decryptAllEnvParents, firstTarget, background)
if (!hasEnvParents){
yield put({type: DECRYPT_ALL_SUCCESS})
}
}

if ((verifyRes && verifyRes.error) || decryptErr){
yield put({type: DECRYPT_ALL_FAILED, error: true, payload: (verifyRes ? verifyRes.payload : decryptErr)})
if (verifyRes && verifyRes.error){
yield put({type: DECRYPT_ALL_FAILED, error: true, payload: verifyRes.payload})
}
}
}
Expand All @@ -397,7 +392,7 @@ function *onDecryptEnvs(action){
yield put({type: DECRYPT_ENVS_SUCCESS, payload: decryptedParent, meta: action.meta})
} catch (err){
yield put({type: DECRYPT_ENVS_FAILED, error: true, payload: err, meta: action.meta})
throw(err)
yield put({type: DECRYPT_ALL_FAILED, error: true, payload: err})
}
}
}
Expand Down

0 comments on commit 71f2b60

Please sign in to comment.