Skip to content

Commit

Permalink
working on update module
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrvel committed Dec 14, 2018
1 parent 6ecd14b commit 623722e
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ package-lock.json

app/certs/*.pem
*electron-builder.yml
*dev-app-update.yml
config/production/electron-builder.yml
config/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Author: [Ganesh Rathinavel](https://www.linkedin.com/in/ganeshrvel 'Ganesh Rathi

Requirements: node.js v8 or higher, yarn

Version: 0.8.3
Version: 0.08.05

yarn
cd ./app && yarn
3 changes: 3 additions & 0 deletions app-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
owner: ganeshrvel
repo: openmtp
provider: github
193 changes: 193 additions & 0 deletions app/classes/AppUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
'use strict';

import { dialog, BrowserWindow, remote } from 'electron';
import { autoUpdater } from 'electron-updater';
import { isConnected } from '../utils/isOnline';
import ElectronProgressbar from 'electron-progressbar';
import { log } from '../utils/log';
import { IS_DEV } from '../constants/env';
import { PATHS } from '../utils/paths';

export default class AppUpdate {
constructor() {
this.autoUpdater = autoUpdater;
this.autoUpdater.updateConfigPath = PATHS.appUpdateFile;
this.autoUpdater.autoDownload = false;

this.forceCheckProgress = null;
this.downloadProgress = null;
this.updateInitFlag = false;
this.updateForceCheckFlag = false;
}

init() {
if (this.updateInitFlag) {
return;
}

this.autoUpdater.on('error', error => {
const errorMsg =
error == null ? 'unknown' : (error.stack || error).toString();
this.forceCheckProgress.setCompleted();

dialog.showErrorBox(`Error: ${errorMsg}`);
log.doLog(error);
});

this.autoUpdater.on('update-available', () => {
this.forceCheckProgress.setCompleted();

dialog.showMessageBox(
{
type: 'info',
title: 'Updates Found',
message: 'New update found. Update now?',
buttons: ['Yes', 'No']
},
buttonIndex => {
switch (buttonIndex) {
case 0:
this.autoUpdater.downloadUpdate();
break;
default:
case 1:
this.setDownloadUpdatesProgress();
return;
break;
}
}
);
});

this.autoUpdater.on('download-progress', (ev, progress) => {
if (this.downloadProgress.isCompleted()) {
return null;
}

this.downloadProgress.value = progress.percent;
});

this.autoUpdater.on('update-downloaded', () => {
this.downloadProgress.setCompleted();

dialog.showMessageBox(
{
title: 'Install Updates',
message: 'Updates downloaded. Application will be quit for update...',
buttons: ['Install']
},
buttonIndex => {
switch (buttonIndex) {
case 0:
default:
autoUpdater.quitAndInstall();
break;
}
}
);
});

this.updateInitFlag = true;

try {
} catch (e) {
log.error(e, `AppUpdate -> init`);
}
}

checkForUpdates() {
this.autoUpdater.checkForUpdates();
}

forceCheck() {
if (!this.updateForceCheckFlag) {
this.autoUpdater.on('checking-for-update', () => {
this.setCheckUpdatesProgress();
});

this.autoUpdater.on('update-not-available', () => {
this.forceCheckProgress.setCompleted();
dialog.showMessageBox(
{
title: 'No Updates Found',
message: 'You have the latest version installed.',
buttons: ['Close']
},
buttonIndex => {
switch (buttonIndex) {
case 0:
default:
break;
}
}
);
});
}

this.autoUpdater.checkForUpdates();
this.updateForceCheckFlag = true;
try {
} catch (e) {
log.error(e, `AppUpdate -> forceCheck`);
}
}

setCheckUpdatesProgress() {
isConnected().then(connected => {
if (!connected) {
dialog.showMessageBox(
{
title: 'Checking For Updates',
message: 'Internet connection is unavailable.',
buttons: ['Close']
},
buttonIndex => {
switch (buttonIndex) {
case 0:
default:
return;
break;
}
}
);
return null;
}

this.forceCheckProgress = new ElectronProgressbar({
title: 'Checking For Updates',
text: 'Please wait...',
detail: ''
});
});
}

setDownloadUpdatesProgress() {
isConnected().then(connected => {
if (!connected) {
dialog.showMessageBox(
{
title: 'Downloading Updates',
message: 'Internet connection is unavailable.',
buttons: ['Close']
},
buttonIndex => {
switch (buttonIndex) {
case 0:
default:
return;
break;
}
}
);
return null;
}

this.downloadProgress = new ElectronProgressbar({
indeterminate: false,
title: 'Downloading Updates',
text: 'Please wait...',
detail: ''
});
});
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion app/classes/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { logFile, settingsFile, logFolder } = PATHS;
const deviceType = deviceTypeConst.local;
const logFileRotationCleanUpThreshold = 60; //days

export default class boot {
export default class Boot {
constructor() {
this.verifyDirList = [logFolder];
this.verifyFileList = [settingsFile, logFile];
Expand Down
2 changes: 1 addition & 1 deletion app/containers/App/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Typography from '@material-ui/core/Typography';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { withStyles } from '@material-ui/core/styles';
import Routes from '../../routing';
import bootApp from '../../classes/boot';
import bootApp from '../../classes/Boot';
import { settingsStorage } from '../../utils/storageHelper';
import SettingsDialog from '../Settings';
import { withReducer } from '../../store/reducers/withReducer';
Expand Down
22 changes: 6 additions & 16 deletions app/main.dev.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
'use strict';

/**
* This module executes inside of electron's main process. You can start
* electron renderer process from here and communicate with the other processes
* through IPC.
*
* When running `yarn build` or `yarn build-main`, this file is compiled to
* `./app/main.prod.js` using webpack. This gives us some performance wins.
*
*/
import { app, BrowserWindow } from 'electron';
import { app, BrowserWindow, ipcMain } from 'electron';
import MenuBuilder from './menu';
import { log } from './utils/log';
import { IS_PROD, DEBUG_PROD, IS_DEV } from './constants/env';
import { DEBUG_PROD, IS_DEV } from './constants/env';
import AppUpdate from './classes/AppUpdate';

let mainWindow = null;
const isSingleInstance = app.requestSingleInstanceLock();
const autoAppUpdate = new AppUpdate();

if (IS_PROD) {
const sourceMapSupport = require('source-map-support');
sourceMapSupport.install();
}
/*autoAppUpdate.init();*/

if (IS_DEV || DEBUG_PROD) {
require('electron-debug')();
Expand Down Expand Up @@ -99,7 +89,7 @@ const createWindow = async () => {
mainWindow = null;
});

const menuBuilder = new MenuBuilder(mainWindow);
const menuBuilder = new MenuBuilder({ mainWindow, autoAppUpdate });
menuBuilder.buildMenu();
} catch (e) {
log.error(e, `main.dev -> createWindow`);
Expand Down
17 changes: 16 additions & 1 deletion app/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ const fireReportBugs = () => {
};

export default class MenuBuilder {
constructor(mainWindow) {
constructor({ mainWindow, autoAppUpdate }) {
this.mainWindow = mainWindow;
this.autoAppUpdate = autoAppUpdate;
}

buildMenu() {
this.setupDevelopmentEnvironment();
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
Expand Down Expand Up @@ -108,6 +110,13 @@ export default class MenuBuilder {
selector: 'orderFrontStandardAboutPanel:'
},
{ type: 'separator' },
{
label: 'Check For Updates',
click: () => {
this.autoAppUpdate.forceCheck();
}
},
{ type: 'separator' },
{
label: 'Hide the OpenMTP',
accelerator: 'Command+H',
Expand Down Expand Up @@ -302,6 +311,12 @@ export default class MenuBuilder {
{
label: 'Help',
submenu: [
{
label: 'Check For Updates',
click: () => {
this.autoAppUpdate.forceCheck();
}
},
{
label: 'Send Error Logs',
click: () => {
Expand Down
19 changes: 19 additions & 0 deletions app/utils/isOnline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
import Promise from 'bluebird';
import { log } from '../utils/log';

export const isConnected = () => {
try {
return new Promise((resolve, reject) => {
require('dns').lookup('github.com', function(err) {
if (err && err.code === 'ENOTFOUND') {
resolve(false);
return null;
}
resolve(true);
});
});
} catch (e) {
log.error(e, `isOnline -> isConnected`);
}
};
4 changes: 3 additions & 1 deletion app/utils/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const logFileName = IS_DEV
const logFolder = path.join(profileFolder, `./logs`);
const logFile = path.join(logFolder, `./${productName}-${logFileName}`);
const settingsFile = path.join(profileFolder, `./settings.json`);
const appUpdateFile = path.join(root, `./app-update.yml`);

export const PATHS = {
root: path.resolve(root),
Expand All @@ -26,7 +27,8 @@ export const PATHS = {
profileFolder: path.resolve(profileFolder),
logFolder: path.resolve(logFolder),
logFile: path.resolve(logFile),
settingsFile: path.resolve(settingsFile)
settingsFile: path.resolve(settingsFile),
appUpdateFile: path.resolve(appUpdateFile)
};

export const pathUp = filePath => {
Expand Down
2 changes: 1 addition & 1 deletion app/utils/storageHelper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { PATHS } from './paths';
import Storage from '../classes/storage';
import Storage from '../classes/Storage';

const { settingsFile } = PATHS;

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"start": "cross-env NODE_ENV=production electron ./app/",
"start-main-dev": "cross-env HOT=1 NODE_ENV=development electron -r babel-register ./app/main.dev.js",
"start-renderer-dev": "cross-env NODE_ENV=development node --trace-warnings -r babel-register ./node_modules/webpack-dev-server/bin/webpack-dev-server --config webpack/config.renderer.dev.js",
"publish:mac": "yarn build && electron-builder build --mac --config=config/deploy/electron-builder.yml --publish always"
"publish-mac": "yarn build && electron-builder build --mac --config=config/deploy/electron-builder.yml --publish always"
},
"browserslist": "electron 3.0",
"build": {
Expand Down Expand Up @@ -163,6 +163,8 @@
"classnames": "^2.2.6",
"devtron": "^1.4.0",
"electron-debug": "^2.0.0",
"electron-progressbar": "^1.1.0",
"electron-updater": "^4.0.6",
"history": "^4.7.2",
"immutable": "^3.8.2",
"junk": "^2.1.0",
Expand Down

0 comments on commit 623722e

Please sign in to comment.