Skip to content

Commit

Permalink
Added a function to return the platform type/version
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemal Ruparelia committed Nov 9, 2017
1 parent 3aa7e6c commit fe1759c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 81 deletions.
134 changes: 65 additions & 69 deletions appUpdater.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,76 @@
"use strict";
const fs = require("fs");
const { app, dialog } = require("electron");
const { autoUpdater } = require("electron-updater");
const isDev = require("electron-is-dev");
const log = require("electron-log");
'use strict';
const fs = require('fs');
const { app, dialog } = require('electron');
const { autoUpdater } = require('electron-updater');
const isDev = require('electron-is-dev');
const log = require('electron-log');

function appUpdater(mainWindow) {
// Don't initiate auto-updates in development and on Linux system
// since autoUpdater doesn't work on Linux
if (isDev || process.platform === "linux") {
return;
}
// Don't initiate auto-updates in development and on Linux system
// since autoUpdater doesn't work on Linux
if (isDev || process.platform === 'linux') {
return;
}

// Log whats happening
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = "info";
log.info("App starting...");
// Log whats happening
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');

autoUpdater.logger = log;
autoUpdater.logger = log;

function sendStatusToWindow(text) {
log.info(text);
mainWindow.send("Updater", text);
}
function sendStatusToWindow(text) {
log.info(text);
if (mainWindow) {
mainWindow.send('Updater', text);
}
}

autoUpdater.on("checking-for-update", () => {
sendStatusToWindow("Checking for update...");
});
autoUpdater.on("update-available", info => {
sendStatusToWindow("Update available.");
});
autoUpdater.on("update-not-available", info => {
sendStatusToWindow("Update not available.");
});
autoUpdater.on("error", err => {
log.info(err);
sendStatusToWindow(`Error in auto-updater.`);
});
autoUpdater.on("download-progress", progressObj => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + " - Downloaded " + progressObj.percent + "%";
log_message =
log_message +
" (" +
progressObj.transferred +
"/" +
progressObj.total +
")";
sendStatusToWindow(log_message);
});
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...');
});
autoUpdater.on('update-available', info => {
sendStatusToWindow('Update available.');
});
autoUpdater.on('update-not-available', info => {
sendStatusToWindow('Update not available.');
});
autoUpdater.on('error', err => {
log.info(err);
sendStatusToWindow(`Error in auto-updater.`);
});
autoUpdater.on('download-progress', progressObj => {
let log_message = 'Download speed: ' + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + '/' + progressObj.total + ')';
sendStatusToWindow(log_message);
});

// Ask the user to restart if an update is available
autoUpdater.on("update-downloaded", event => {
dialog.showMessageBox(
{
type: "question",
buttons: ["Install and Relaunch", "Install Later"],
defaultId: 0,
message: `A new update ${event.version} has been downloaded`,
detail: "It will be installed the next time you restart the application"
},
response => {
if (response === 0) {
setTimeout(() => {
autoUpdater.quitAndInstall();
// force app to quit. This is just a workaround, ideally autoUpdater.quitAndInstall() should relaunch the app.
app.quit();
}, 1000);
}
}
);
});
// Init for updates
autoUpdater.checkForUpdates();
// Ask the user to restart if an update is available
autoUpdater.on('update-downloaded', event => {
dialog.showMessageBox(
{
type: 'question',
buttons: ['Install and Relaunch', 'Install Later'],
defaultId: 0,
message: `A new update ${event.version} has been downloaded`,
detail: 'It will be installed the next time you restart the application',
},
response => {
if (response === 0) {
setTimeout(() => {
autoUpdater.quitAndInstall();
// force app to quit. This is just a workaround, ideally autoUpdater.quitAndInstall() should relaunch the app.
app.quit();
}, 1000);
}
}
);
});
// Init for updates
autoUpdater.checkForUpdates();
}

module.exports = {
appUpdater
appUpdater,
};
12 changes: 8 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const mime = require('mime');
const path = require('path');
const log = require('electron-log');
const platform = require('./platform');

let mainWindow = null;

Expand Down Expand Up @@ -46,19 +47,22 @@ app.on('ready', () => {
},
});

const filePath = process.argv[2];
if (filePath != undefined) {
handleFilePath(filePath);
if (platform.isWindows) {
const filePath = process.argv[2];
if (filePath != undefined) {
handleFilePath(filePath);
}
}

mainWindow.maximize();
mainWindow.loadURL('https://dev5.mediref.com.au/new');
mainWindow.loadURL('https://dev5.mediref.com.au/');

appUpdater(mainWindow);

setTimeout(() => {
let version = app.getVersion();
mainWindow.send('Updater', `You are running v${version}`);
mainWindow.send('Updater', `Your OS: ${platform.name}`);
}, 5000);

mainWindow.webContents.openDevTools();
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mediref-desktop",
"version": "0.1.16",
"version": "0.1.17",
"repository": {
"type": "git",
"url": "https://github.com/HemalR/mediref-desktop.git"
Expand Down Expand Up @@ -47,13 +47,13 @@
}
]
},
"win": {
"target": "nsis"
},
"nsis": {
"oneClick": true,
"runAfterFinish": false
}
"win": {
"target": "nsis"
},
"nsis": {
"oneClick": true,
"runAfterFinish": false
}
},
"devDependencies": {
"electron": "^1.7.9",
Expand Down
61 changes: 61 additions & 0 deletions platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Check platform. Code from:https://github.com/adam-lynch/developing-an-electron-edge/

const os = require('os');

var platform = {
isLinux: false,
isMac: false,
isWindows: false,
isWindows8: false,
version: os.release(),
};

/**
* Checks if the current platform version is greater than or equal to the desired minimum version given
*
* @param minimumVersion {string} E.g. 10.0.0.
* See [the Darwin operating system Wikipedia entry](http://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history) for Mac - Darwin versions.
* Also, Windows 8 >= 6.2.9200
*
* @returns {boolean}
*/
var isOfMinimumVersion = minimumVersion => {
var actualVersionPieces = platform.version.split('.'),
pieces = minimumVersion.split('.'),
numberOfPieces = pieces.length;

for (var i = 0; i < numberOfPieces; i++) {
var piece = parseInt(pieces[i], 10),
actualPiece = parseInt(actualVersionPieces[i], 10);

if (typeof actualPiece === 'undefined') {
break; // e.g. 13.1 passed and actual is 13.1.0
} else if (actualPiece > piece) {
break; // doesn't matter what the next bits are, the major version (or whatever) is larger
} else if (actualPiece === piece) {
continue; // to check next version piece
} else {
return false;
}
}

return true; // all was ok
};

var name = os.platform();

if (name === 'darwin') {
platform.name = 'mac';
platform.isMac = true;
} else if (name === 'linux') {
platform.name = 'linux';
platform.isLinux = true;
} else {
platform.name = 'windows';
platform.isWindows = true;
platform.isWindows8 = isOfMinimumVersion('6.2.9200');
}

platform.is64Bit = os.arch() === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');

module.exports = platform;

0 comments on commit fe1759c

Please sign in to comment.