Skip to content

Commit

Permalink
i hope auto update works now
Browse files Browse the repository at this point in the history
  • Loading branch information
boginw committed Feb 13, 2017
1 parent 4326b42 commit cb4d730
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 23 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ notes
notes*
build
release
releases
releases
out
npm-debug.log
cert.p12
cert.pem
key.pem
token
Binary file added app/img/loading_inst.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions auto-updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const app = require('electron').app
const autoUpdater = require('electron').autoUpdater
const ChildProcess = require('child_process')
const Menu = require('electron').Menu
const path = require('path')

var state = 'checking'

exports.initialize = function () {
if (process.mas) return

autoUpdater.on('checking-for-update', function () {
state = 'checking'
exports.updateMenu()
})

autoUpdater.on('update-available', function () {
state = 'checking'
exports.updateMenu()
})

autoUpdater.on('update-downloaded', function () {
state = 'installed'
exports.updateMenu()
})

autoUpdater.on('update-not-available', function () {
state = 'no-update'
exports.updateMenu()
})

autoUpdater.on('error', function () {
state = 'no-update'
exports.updateMenu()
})

//autoUpdater.setFeedURL(`https://notable-depoyment.herokuapp.com/updates?version=${app.getVersion()}`)
autoUpdater.setFeedURL(`https://notable-depoyment.herokuapp.com/download`)
autoUpdater.checkForUpdates()
}

exports.updateMenu = function () {
if (process.mas) return

var menu = Menu.getApplicationMenu()
if (!menu) return

menu.items.forEach(function (item) {
if (item.submenu) {
item.submenu.items.forEach(function (item) {
switch (item.key) {
case 'checkForUpdate':
item.visible = state === 'no-update'
break
case 'checkingForUpdate':
item.visible = state === 'checking'
break
case 'restartToUpdate':
item.visible = state === 'installed'
break
}
})
}
})
}

exports.createShortcut = function (callback) {
spawnUpdate([
'--createShortcut',
path.basename(process.execPath),
'--shortcut-locations',
'StartMenu'
], callback)
}

exports.removeShortcut = function (callback) {
spawnUpdate([
'--removeShortcut',
path.basename(process.execPath)
], callback)
}

function spawnUpdate (args, callback) {
var updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
var stdout = ''
var spawned = null

try {
spawned = ChildProcess.spawn(updateExe, args)
} catch (error) {
if (error && error.stdout == null) error.stdout = stdout
process.nextTick(function () { callback(error) })
return
}

var error = null

spawned.stdout.on('data', function (data) { stdout += data })

spawned.on('error', function (processError) {
if (!error) error = processError
})

spawned.on('close', function (code, signal) {
if (!error && code !== 0) {
error = new Error('Command failed: ' + code + ' ' + signal)
}
if (error && error.code == null) error.code = code
if (error && error.stdout == null) error.stdout = stdout
callback(error)
})
}
58 changes: 40 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const globalShortcut = electron.globalShortcut;
const path = require('path');
const url = require('url');
const fs = require("fs");
const autoUpdater = require('./auto-updater')
const windowStateKeeper = require('electron-window-state');
const Config = require('electron-config')
const config = new Config();
Expand All @@ -29,6 +30,7 @@ if(!fs.existsSync(rootDir+'notes/init.md')){
}

function createWindow () {
autoUpdater.initialize();
// Load last window state
let mainWindowState = windowStateKeeper({
defaultWidth: 1000,
Expand Down Expand Up @@ -69,21 +71,41 @@ function createWindow () {
mainWindow = null;
});
}
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});

function initialize () {


app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
}

switch (process.argv[1]) {
case '--squirrel-install':
autoUpdater.createShortcut(function () { app.quit() })
break
case '--squirrel-uninstall':
autoUpdater.removeShortcut(function () { app.quit() })
break
case '--squirrel-obsolete':
case '--squirrel-updated':
app.quit()
break
default:
initialize()
}
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"main": "main.js",
"scripts": {
"start": "electron .",
"buildWin": "electron-packager . --overwrite --asar=true --platform=win32 --arch=ia32 --prune=true --out=release --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Notable\"",
"pack:win64": "electron-packager . $npm_package_productName --out=dist/win --platform=win32 --arch=x64 --version=1.5.0 app-version=1.0",
"build": "npm run pack:win64"
"pack-win": "electron-packager . --asar.unpack=protocol-link.html --overwrite --platform=win32 --arch=ia32 --icon=app/img/logo.ico --out=out --version-string.CompanyName='Notable.Ink' --version-string.FileDescription='a notable tool for taking notes' --version-string.ProductName='Notable'",
"installer": "node ./script/installer.js",
"sign-exe": "signcode out/notable-win32-ia32/notable.exe --cert cert.p12 --name 'Notable' --url 'https://notable.ink'",
"sign-installer": "signcode ./out/windows-installer/Setup.exe --cert ./cert.p12 --name 'Notable' --url 'https://notable.ink'"
},
"build": {
"app-bundle-id": "ink.notable",
Expand All @@ -29,10 +30,12 @@
"electron": "^1.4.1",
"electron-packager": "^8.5.1",
"electron-prebuilt": "^1.4.13",
"electron-winstaller": "^2.5.2"
"electron-winstaller": "^2.5.2",
"signcode": "^0.5.0"
},
"dependencies": {
"aliasify": "^2.1.0",
"child_process": "^1.0.2",
"cropperjs": "^0.8.1",
"electron-config": "^0.2.1",
"electron-ipc-broadcast": "^0.1.0",
Expand All @@ -41,6 +44,7 @@
"font-awesome": "^4.7.0",
"fs": "0.0.1-security",
"material-icons": "^0.1.0",
"rimraf": "^2.5.4",
"simplemde": "^1.11.2",
"vex-js": "^3.1.0",
"vue": "^2.1.10",
Expand Down
37 changes: 37 additions & 0 deletions script/installer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

const createWindowsInstaller = require('electron-winstaller').createWindowsInstaller
const path = require('path')
const rimraf = require('rimraf')

deleteOutputFolder()
.then(getInstallerConfig)
.then(createWindowsInstaller)
.catch((error) => {
console.error(error.message || error)
process.exit(1)
})

function getInstallerConfig () {
const rootPath = path.join(__dirname, '..')
const outPath = path.join(rootPath, 'out')

return Promise.resolve({
appDirectory: path.join(outPath, 'notable-win32-ia32'),
iconUrl: "https://raw.githubusercontent.com/boginw/notable/master/app/img/logo.ico",
loadingGif: path.join(rootPath, 'app', 'img', 'loading_inst.gif'),
noMsi: true,
outputDirectory: path.join(outPath, 'windows-installer'),
setupExe: 'notableSetup.exe',
setupIcon: path.join(rootPath, 'app', 'img', 'app.ico'),
skipUpdateIcon: true
})
}

function deleteOutputFolder () {
return new Promise((resolve, reject) => {
rimraf(path.join(__dirname, '..', 'out', 'windows-installer'), (error) => {
error ? reject(error) : resolve()
})
})
}

0 comments on commit cb4d730

Please sign in to comment.