'use strict';
var electron = require('electron');
var { app } = electron;
var { BrowserWindow } = electron;
var path = require('path')
var mainWindow = null;
// grunt 打包是 安装 更新 卸载是 生成桌面快捷方式
var handleStartupEvent = function () {
if (process.platform !== 'win32') {
return false;
}
var squirrelCommand = process.argv[1];
switch (squirrelCommand) {
case '--squirrel-install':
case '--squirrel-updated':
install();
return true;
case '--squirrel-uninstall':
uninstall();
app.quit();
return true;
case '--squirrel-obsolete':
app.quit();
return true;
}
// 安装
function install() {
var cp = require('child_process');
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var target = path.basename(process.execPath);
var child = cp.spawn(updateDotExe, ["--createShortcut", target], { detached: true });
child.on('close', function(code) {
app.quit();
});
}
// 卸载
function uninstall() {
var cp = require('child_process');
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var target = path.basename(process.execPath);
var child = cp.spawn(updateDotExe, ["--removeShortcut", target], { detached: true });
child.on('close', function(code) {
app.quit();
});
}
const { remote } = require('electron');
const updater = remote.require('electron-simple-updater');
updater.on('update-available', onUpdateAvailable);
updater.on('update-downloading', onUpdateDownloading);
updater.on('update-downloaded', onUpdateDownloaded);
updater.on('error', function(err){
alert(JSON.stringify(err))
});
function onUpdateAvailable(meta) {
updater.downloadUpdate();
}
function onUpdateDownloading() {
console.log(下载中)
}
function onUpdateDownloaded() {
//下载成功,执行安装
if (window.confirm('The app has been updated. Do you like to restart it now?')) {
updater.quitAndInstall();
}
}
updater.checkForUpdates();//获取更新
1.npm install -g windows-build-tools
2.安装 Python 2.7版本注意要2.7的版本噢(https://www.python.org/getit/)
3.Visual C++ Build Environment:环境
Option 1: Install Visual C++ Build Tools using the Default Install option.
Option 2: Install Visual Studio 2015 (or modify an existing installation) and select Common Tools for Visual C++ during setup. This also works with the free Community and Express for Desktop editions.
第一部分生成electron应用
第一步中的注意事项:
1.打包时node_modules 文件夹是被忽略,所以需要我们打包完成后找到\myApp\myApp-win32-x64\resources\app 这里就是源文件,在此处进行手动安装所需要的包例如:electron-node-getmac
第二部分打包electron应用成安装文件
// grunt 打包是 安装 更新 卸载是 生成桌面快捷方式 var handleStartupEvent = function () { if (process.platform !== 'win32') { return false; } var squirrelCommand = process.argv[1]; switch (squirrelCommand) { case '--squirrel-install': case '--squirrel-updated': install(); return true; case '--squirrel-uninstall': uninstall(); app.quit(); return true; case '--squirrel-obsolete': app.quit(); return true; } // 安装 function install() { var cp = require('child_process'); var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe'); var target = path.basename(process.execPath); var child = cp.spawn(updateDotExe, ["--createShortcut", target], { detached: true }); child.on('close', function(code) { app.quit(); }); } // 卸载 function uninstall() { var cp = require('child_process'); var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe'); var target = path.basename(process.execPath); var child = cp.spawn(updateDotExe, ["--removeShortcut", target], { detached: true }); child.on('close', function(code) { app.quit(); }); }
}; if (handleStartupEvent()) { return; } app.on('ready', function() { mainWindow = new BrowserWindow({width: 500, height: 960}) mainWindow.loadURL('file://' + __dirname + './index.html'); });
//自动更新 注册 const updater = require('electron-simple-updater'); updater.init({ checkUpdateOnStart: false, autoDownload: false });
{ "name": "myApp", "title": "myApp", "version": "0.0.3", "main": "./main.js", "updater": { "url": "http://127.0.0.1:3000/github/update.json" }, "scripts": { "start": "electron .", "package": "electron-packager ./ myApp --win --out myApp --arch=x64 --version 1.4.13 --overwrite --ignore=node_modules --version-string.CompanyName=myApp --version-string.ProductName=myApp" }, "devDependencies": { "electron-packager": "^8.7.0" }, "dependencies": { "electron-simple-updater": "^1.2.0" } }
{ "linux-x64-prod": { "readme": "Second release", "update": "https://github.com/megahertz/electron-simple-updater/releases/download/linux-x64-prod-v0.0.2/simple-updater-example-0.0.2-x86_64.AppImage", "install": "https://github.com/megahertz/electron-simple-updater/releases/download/linux-x64-prod-v0.0.2/simple-updater-example-0.0.2-x86_64.AppImage", "version": "0.0.2" }, "win32-x64-prod": { "readme": "Second release", "update": "https://github.com/megahertz/electron-simple-updater/releases/download/win32-x64-prod-v0.0.2", "install": "https://github.com/megahertz/electron-simple-updater/releases/download/win32-x64-prod-v0.0.2/Simple.Updater.Example.Setup.0.0.2.exe", "version": "0.0.2" }, "darwin-x64-prod": { "readme": "Second Release", "update": "https://github.com/megahertz/electron-simple-updater/releases/download/darwin-x64-prod-v0.0.2/release.json", "install": "https://github.com/megahertz/electron-simple-updater/releases/download/darwin-x64-prod-v0.0.2/Simple.Updater.Example-0.0.2.dmg", "version": "0.0.2" } }
$ npm install -g node-gyp
1.npm install -g windows-build-tools 2.安装 Python 2.7版本注意要2.7的版本噢(https://www.python.org/getit/) 3.Visual C++ Build Environment:环境 Option 1: Install Visual C++ Build Tools using the Default Install option. Option 2: Install Visual Studio 2015 (or modify an existing installation) and select Common Tools for Visual C++ during setup. This also works with the free Community and Express for Desktop editions.