Skip to content

Commit

Permalink
Draft version, initialize an example
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Oct 14, 2016
1 parent c3fde17 commit 5e264f9
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 4 deletions.
5 changes: 5 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Simple Updater Example

This example uses
[electron-builder](https://github.com/electron-userland/electron-builder)
for building installer and update package.
61 changes: 61 additions & 0 deletions example/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of using electron-simple-updater</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody id="rows"></tbody>
</table>

<script>
const { remote } = require('electron');
const { app } = remote;

renderData([
['Command line', JSON.stringify(remote.process.argv, null, ' ')],
['Executable', app.getPath('exe')],
['App Path', app.getAppPath()],
['App data path', app.getPath('appData')],
['User data path', app.getPath('userData')],
['Resource path', remote.process.resourcesPath],
['App version', app.getVersion()],
]);


function renderData(data) {
const tableBody = document.getElementById('rows');
data
.map(([name, value]) => {
const tr = document.createElement('tr');

const td1 = document.createElement('td');
td1.appendChild(document.createTextNode(name));

const td2 = document.createElement('td');
td2.appendChild(document.createTextNode(value));

tr.appendChild(td1);
tr.appendChild(td2);

return tr;
})
.forEach((node) => {
tableBody.appendChild(node);
});
}
</script>

</body>
</html>
14 changes: 14 additions & 0 deletions example/app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const {app, BrowserWindow} = require('electron');

let mainWindow;

app.on('ready', () => {
mainWindow = new BrowserWindow({
height: 600,
width: 800
});

mainWindow.loadURL('file://' + __dirname + '/index.html');
});
15 changes: 15 additions & 0 deletions example/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "simple-updater-example",
"productName": "Simple Updater Example",
"version": "0.0.1",
"description": "Example of using electron-simple-updater",
"main": "main.js",
"author": "Alexey Prokhorov",
"license": "MIT",
"engines": {
"node": ">=6.0"
},
"dependencies": {
"electron-simple-updater": "*"
}
}
26 changes: 26 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "electron-simple-updater-example",
"description": "Example of using electron-simple-updater",
"main": "app/main.js",
"scripts": {
"postinstall": "install-app-deps",
"start": "node_modules/.bin/electron ./app",
"dist": "build"
},
"author": "Alexey Prokhorov",
"license": "MIT",
"private": true,
"build": {
"appId": "megahertz.electron-simple-updater-example",
"mac": {
"category": "public.app-category.developer-tools"
}
},
"engines": {
"node": ">=6.0"
},
"devDependencies": {
"electron": "*",
"electron-builder": "7.8.0"
}
}
85 changes: 84 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,35 @@ const normalizeOptions = require('./lib/normalize-options');

class SimpleUpdater extends events.EventEmitter {
init(options) {
if (this.options) {
this.options.logger.warn(
'electron-simple updater has been initialized before'
);
}

this.options = normalizeOptions(options);

const squirrelAction = win32.getSquirrelInstallerAction();
if (squirrelAction) {
const exitNow = this.options.onSquirrelWinInstaller(squirrelAction);
if (exitNow) {
process.exit();
}
}

if (this.options.checkUpdatesOnStart) {
this.checkForUpdates();
}

/**
* @event update-downloaded
* @param {Object} meta Update metadata
*/
autoUpdater.on('update-downloaded', () => {
const version = this.meta.version;
this.options.logger.log(`New version ${version} has been downloaded`);
this.emit('update-downloaded', this.meta);
});
}

setFeedURL(url) {
Expand All @@ -26,12 +54,67 @@ class SimpleUpdater extends events.EventEmitter {
}

checkForUpdates() {
getUpdatesMeta(this.options.url)
const opt = this.options;

if (opts.disabled) {
opt.logger.warn(`Update is disabled`);
}

getUpdatesMeta(opt.url, opt.build, opt.channel, opt.version)
.then((updateMeta) => {
if (updateMeta) {
this.onFoundUpdate(updateMeta);
} else {
opt.logger.log(`Updates for ${this.build} are not available`);
this.emit('update-not-available');
}
})
.catch((e) => {
opt.logger.warn(e);
this.emit('error', e);
});
}

downloadUpdate() {
const feedUrl = autoUpdater.getFeedURL();
/**
* @event update-downloading
* @param {Object} meta Update metadata
*/
this.emit('update-downloading', this.meta);
this.options.logger.log(`Downloading updates from ${feedUrl}`);

if (this.meta.platform === 'linux') {

} else {
autoUpdater.checkForUpdates();
}
}

quitAndInstall() {
return autoUpdater.quitAndInstall();
}

/**
* Called when updates metadata has been downloaded
* @private
* @param meta
*/
onFoundUpdate(meta) {
this.meta = meta;
const opt = this.options;

opt.logger.log(`Found version ${meta.name} at '${meta.updateUrl}'`);
autoUpdater.setFeedURL(meta.updateUrl);
/**
* @event update-available
* @param {Object} meta Update metadata
*/
this.emit('update-available', meta);
if (opt.autoDownload) {
this.downloadUpdate();
}
}
}

return new SimpleUpdater();
7 changes: 7 additions & 0 deletions lib/linux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports.install = install;

function install(meta) {
console.log('Try installing under linux');
}
4 changes: 3 additions & 1 deletion lib/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module.exports = normalizeOptions;
function normalizeOptions(options) {
const def = {
onSquirrelWinInstaller: win32.executeSquirrelDefaultAction,
logger: console
logger: console,
checkUpdatesOnStart: true,
autoDownload: true
};

if (typeof options === 'string') {
Expand Down
4 changes: 4 additions & 0 deletions lib/win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ function executeSquirrelDefaultAction() {
}
case SQUIRREL_OBSOLETE: {
app.quit();
return;
}
default: {
return;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"files": [
"index.js",
"README.md"
"README.md",
"lib/*"
],
"keywords": [
"electron",
Expand Down Expand Up @@ -42,14 +43,14 @@
"chai": "*",
"electron": "*",
"eslint": "*",
"http-get-shim": "^0.1.1",
"mocha": "*",
"mock-require": "^1.3.0",
"rewire": "*",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0"
},
"dependencies": {
"http-get-shim": "^0.1.1",
"semver": "^5.3.0"
}
}

0 comments on commit 5e264f9

Please sign in to comment.