Skip to content

Instantly share code, notes, and snippets.

@luchenqun
Created March 20, 2018 06:03
Show Gist options
  • Select an option

  • Save luchenqun/2cb5bb0bdcb92a502eb87ce26227a49f to your computer and use it in GitHub Desktop.

Select an option

Save luchenqun/2cb5bb0bdcb92a502eb87ce26227a49f to your computer and use it in GitHub Desktop.
import { app, BrowserWindow } from 'electron'
import { autoUpdater } from "electron-updater"
import log from "electron-log"
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = "info";
log.info("App starting...");
/**
* Set `__static` path to static files in production
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
*/
if (process.env.NODE_ENV !== 'development') {
global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
let mainWindow
const winURL = process.env.NODE_ENV === 'development'
? `http://localhost:9080`
: `file://${__dirname}/index.html`
function createWindow() {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
height: 563,
useContentSize: true,
width: 1000
})
mainWindow.loadURL(winURL)
mainWindow.on('closed', () => {
mainWindow = null
})
autoUpdater.setFeedURL("http://localhost:8080");
autoUpdater.checkForUpdatesAndNotify();
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
function sendStatusToWindow(text) {
log.info(text);
win.webContents.send("message", 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 => {
sendStatusToWindow("Error in auto-updater. " + err);
});
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("update-downloaded", info => {
sendStatusToWindow("Update downloaded");
});
/**
* Auto Updater
*
* Uncomment the following code below and install `electron-updater` to
* support auto updating. Code Signing with a valid certificate is required.
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
*/
/*
import { autoUpdater } from 'electron-updater'
autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall()
})
app.on('ready', () => {
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment