electron版本升級(jí)的簡(jiǎn)單步驟
electron-updater使用指南
基礎(chǔ)
檢測(cè)是否最新版
autoUpdater.checkForUpdates()
下載最新版
autoUpdater.downloadUpdate()
項(xiàng)目使用
update.js
const { ipcMain } = require('electron')
const { autoUpdater } = require('electron-updater')
const path = require("path")
// 更新地址,該地址下放的是安裝包和latest.yml
const updateURL = 'https://jkcgy.obs.cn-south-1.myhuaweicloud.com/desktop/'
const message = {
error: '軟件更新異常,請(qǐng)重試',
checking: '正在檢查更新',
updateAva: '檢測(cè)到新版本,準(zhǔn)備下載',
updateDown: '軟件下載中,請(qǐng)耐心等待',
updateSet: '下載完成,準(zhǔn)備安裝',
updateNotAva: '已經(jīng)是最新版本',
}
//軟件版本更新
ipcMain.handle('on-soft-update', (e) => {
autoUpdater.checkForUpdates()
})
ipcMain.on("updateDesktop", () => {
console.log("checkForUpdates");
autoUpdater.checkForUpdates()
// console.log("downloadUpdate");
})
ipcMain.on("updateDesktopping", () => {
autoUpdater.downloadUpdate()
})
// 檢測(cè)更新,在你想要檢查更新的時(shí)候執(zhí)行,renderer事件觸發(fā)后的操作自行編寫(xiě)
function handleUpdate(mainWindow, callback) {
// 設(shè)置是否自動(dòng)下載,默認(rèn)是true,當(dāng)點(diǎn)擊檢測(cè)到新版本時(shí),會(huì)自動(dòng)下載安裝包,所以設(shè)置為false
autoUpdater.autoDownload = false
// 如果安裝包下載好了,當(dāng)應(yīng)用退出后是否自動(dòng)安裝更新
autoUpdater.autoInstallOnAppQuit = false
if (process.env.NODE_ENV == "development") {
autoUpdater.updateConfigPath = path.join(__dirname, "../../aaa/app-update.yml");
}
// 設(shè)置版本更新服務(wù)器地址
autoUpdater.setFeedURL(updateURL)
// 更新發(fā)生錯(cuò)誤時(shí)觸發(fā)
autoUpdater.on('error', function () {
console.log(" 更新發(fā)生錯(cuò)誤時(shí)觸發(fā)",);
sendUpdateMessage(message.error, "error")
})
// 開(kāi)始檢查更新事件
autoUpdater.on('checking-for-update', function () {
console.log(" 開(kāi)始檢查更新事件",);
sendUpdateMessage(message.checking, "checking")
})
// 沒(méi)有可更新版本
autoUpdater.on('update-not-available', function (info) {
console.log(" 開(kāi)始檢查更新事件",);
sendUpdateMessage(message.updateNotAva, "updateNotAva")
})
// 發(fā)現(xiàn)可更新版本
autoUpdater.on('update-available', function (info) {
console.log(" 發(fā)現(xiàn)可更新版本",);
// autoUpdater.downloadUpdate()
sendUpdateMessage(message.updateAva, "updateAva")
})
// 更新下載進(jìn)度事件
autoUpdater.on('download-progress', function (progressObj) {
console.log(" 更新下載進(jìn)度事件",);
sendUpdateMessage(message.updateDown, "updateDown")
mainWindow.webContents.send('on-soft-download', progressObj.percent)
})
// 下載監(jiān)聽(tīng)
autoUpdater.on(
'update-downloaded',
function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
mainWindow.webContents.send('on-soft-download', 100)
sendUpdateMessage(message.updateSet, "updateSet")
//3秒后更新
setTimeout(() => {
autoUpdater.quitAndInstall()
}, 3000)
}
)
// 向渲染進(jìn)程發(fā)送消息
function sendUpdateMessage(text, type) {
mainWindow.webContents.send('on-soft-message', text, type)
}
}
module.exports = {
handleUpdate,
}然后再 main.js 調(diào)用即可
mainWindow.on('ready-to-show', () => {
mainWindow.show();
updater.handleUpdate(mainWindow)
if (!ipc) ipc = new IPC(mainWindow);
mainWindow.openDevTools();
// if(!callWindowIpc) callWindowIpc = new CallWindowIpc(mainInstance);
});main.js 所有代碼
const { app, BrowserWindow, ipcMain, globalShortcut, Tray, Menu } = require('electron');
const Store = require('electron-store');
const CaptureView = require('./electron-captureview/main/captureview').default;
const path = require('path');
const url = require('url');
const TimMain = require('im_electron_sdk/dist/main');
const { SDK_APP_ID, GET_FILE_INFO_CALLBACK, SCREENSHOTMAC } = require('./const/const');
const IPC = require('./ipc');
const CallWindowIpc = require('./callWindowIpc');
const child_process = require('child_process')
const fs = require('fs')
const updater= require("./update")
const store = new Store();
Store.initRenderer();
const { autoUpdater } = require('electron-updater')
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
let callWindowIpc;
let ipc;
let mainInstance;
let catchedSdkAppId;
const settingConfig = store.get('setting-config');
const sdkappid = catchedSdkAppId = settingConfig?.sdkappId ?? SDK_APP_ID;
let tray = null // 在外面創(chuàng)建tray變量,防止被自動(dòng)刪除,導(dǎo)致圖標(biāo)自動(dòng)消失
const initTimMain = (appid) => {
mainInstance = new TimMain({
sdkappid: Number(appid)
});
}
initTimMain(sdkappid);
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
ipcMain.handle('re-create-main-instance', async (event, newSdkAppid) => {
console.log("************ re-create-main-instance", newSdkAppid)
mainInstance.setSDKAPPID(newSdkAppid)
return
})
// This allows TypeScript to pick up the magic constant that's auto-generated by Forge's Webpack
// plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on
// whether you're running in development or production).
// declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
// app.on('window-all-closed', function () {
// if (process.platform !== 'darwin') app.quit()
// })
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
height: 628,
width: 975,
minWidth: 975,
minHeight: 628,
// show:false,
icon: path.resolve(__dirname, "../../icon/logo.png"),
frame: false,
webPreferences: {
webSecurity: true,
nodeIntegration: true,
nodeIntegrationInWorker: true,
enableRemoteModule: true,
contextIsolation: false,
}
});
mainInstance.enable(mainWindow.webContents)
global.WIN = mainWindow;
mainWindow.on('ready-to-show', () => {
mainWindow.show();
updater.handleUpdate(mainWindow)
if (!ipc) ipc = new IPC(mainWindow);
mainWindow.openDevTools();
// if(!callWindowIpc) callWindowIpc = new CallWindowIpc(mainInstance);
});
mainWindow.on('close', (e) => {
// mainWindow.webContents.send('updateHistoryMessage');
// setTimeout(() => {
// app.exit();
// }, 30);
e.preventDefault(); // 阻止退出程序
mainWindow.setSkipTaskbar(true) // 取消任務(wù)欄顯示
mainWindow.hide(); // 隱藏主程序窗口
});
console.log('======process env======', process.env?.NODE_ENV);
if (process.env?.NODE_ENV?.trim() === 'development') {
mainWindow.loadURL(`http://localhost:3000`);
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, '../../bundle/index.html'),
protocol: 'file:',
slashes: true
})
);
}
// 創(chuàng)建任務(wù)欄圖標(biāo)
tray = new Tray(path.join(__dirname, "../../icon/logo.png"))
// 自定義托盤(pán)圖標(biāo)的內(nèi)容菜單
const contextMenu = Menu.buildFromTemplate([
{
// 點(diǎn)擊退出菜單退出程序
label: '退出', click: function () {
mainWindow.webContents.send('updateHistoryMessage');
setTimeout(() => {
app.exit();
}, 30);
// mainWindow.destroy()
}
}
])
tray.setToolTip('君凱智管') // 設(shè)置鼠標(biāo)指針在托盤(pán)圖標(biāo)上懸停時(shí)顯示的文本
tray.setContextMenu(contextMenu) // 設(shè)置圖標(biāo)的內(nèi)容菜單
// 點(diǎn)擊托盤(pán)圖標(biāo),顯示主窗口
tray.on("click", () => {
mainWindow.show();
mainWindow.setSkipTaskbar(false) // 取消任務(wù)欄顯示
})
// const capture = new CaptureView({
// devTools: false,
// Mosaic: false,
// Text: false,
// // onShow: () => {
// // console.log('start screenshot');
// // },
// onClose: () => {
// const png = clipboard.readImage().toBitmap();
// const fileExample = new File([png], 'xxx.png', { type: 'image/jpeg' });
// console.log('結(jié)束截圖', fileExample);
// },
// onShowByShortCut: () => {
// console.log('shortcut key to start screenshot')
// }
// });
// capture.setMultiScreen(true);
// capture.updateShortCutKey('shift+option+c');
globalShortcut.register('Shift+CommandOrControl+C', function () {
console.log("i am shortcut~~~~~~~~~");
const newdate = new Date();
const date = newdate.toISOString().replaceAll(":", "");
// console.log(date.toISOString());
if (process.platform == "darwin") {
let ex = "screencapture -i ~/desktop/screenshot" + date + ".png"
child_process.exec(`screencapture -i ~/desktop/screenshot` + date + `.png`, (error, stdout, stderr) => {
if (!error) {
var _img = fs.readFileSync(process.env.HOME + "/desktop/screenshot" + date + ".png");
// console.log(_img);
mainWindow.webContents.send(GET_FILE_INFO_CALLBACK, {
triggerType: SCREENSHOTMAC,
data: { _img: _img, date }
})
}
});
} else {
let url = path.resolve(__dirname, "../Snipaste-2.8.2-Beta-x64/Snipaste.exe");
let command = url + " snip -o C:\\Users\\Public\\Desktop\\screenshot" + date + ".png";
// console.log(command);
var id = setInterval(dealFile, 300);
child_process.exec(command, async (error, stdout, stderr) => {
if (!error) {
console.log("done capture");
}
})
function dealFile() {
try {
var _img = fs.readFileSync("C:\\Users\\Public\\Desktop\\screenshot" + date + ".png");
clearInterval(id);
console.log("file exists");
console.log(_img);
event.reply(GET_FILE_INFO_CALLBACK, {
triggerType: SCREENSHOTMAC,
data: { _img: _img, date }
})
} catch (err) {
if (err.code == 'ENOENT') {
// console.log("file doesn't exist yet")
} else {
throw err;
}
}
}
}
})
// mainWindow.loadURL(`http://localhost:3000`);
// mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
Object.defineProperty(app, 'isPackaged', {
get() {
return true;
}
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
console.log('all-window-closed');
if (process.platform !== 'darwin') {
app.exit();
}
});
app.on('activate', () => {
// 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 (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.常見(jiàn)錯(cuò)誤指南
Electron更新報(bào)錯(cuò)-Skip checkForUpdates because application is not packed and dev update config is not forced
原因是在本地啟動(dòng)的不是安裝導(dǎo)致的,在根中配置即可
const { app, BrowserWindow, ipcMain, globalShortcut, Tray, Menu } = require('electron');
Object.defineProperty(app, 'isPackaged', {
get() {
return true;
}
});Electron更新報(bào)錯(cuò)-
必須設(shè)置這個(gè)內(nèi)容
"publish": [
{
"provider": "generic",
"channel": "latest",
"url": "https://jkcgy.obs.cn-south-1.myhuaweicloud.com/desktop/%E5%90%9B%E5%87%AF%E6%99%BA%E7%AE%A1%20Setup%200.0.2.exe"
}
],Electron更新報(bào)錯(cuò)-UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '****\app-update.yml'
找不到app-update.yml 文件,可以去打包路徑中的 dist\win-unpacked\resources 復(fù)制,然后通過(guò)修改 autoUpdater.updateConfigPath 路徑
該路徑是自定義的路徑,自己在 dist\win-unpacked\resources 復(fù)制的
const { autoUpdater } = require('electron-updater')
autoUpdater.updateConfigPath = path.join(__dirname, "../../aaa/app-update.yml");Electron更新報(bào)錯(cuò)-找不到latest.yml 文件
找不到latest.yml文件 可以去dist 復(fù)制
這里跟配置的 autoUpdater.setFeedURL 路徑有關(guān)系
const { autoUpdater } = require('electron-updater')
const updateURL = 'https://jkcgy.obs.cn-south-1.myhuaweicloud.com/desktop/'
// 設(shè)置版本更新服務(wù)器地址
autoUpdater.setFeedURL(updateURL)拓展:更新electron的版本
方式1
首先,查看electron的版本
electron -v

發(fā)現(xiàn)版本為1.4.13
我們輸入如下的cmd來(lái)查看 electron系列版本
npm view electron versions

可供更新的版本有如下[部分截圖]:

完整可供下載的版本如下:
[ '0.1.0', '0.1.1', '0.1.2', '0.2.0', '0.2.1', '0.3.0', '0.4.0', '0.4.1', '1.3.1', '1.3.2', '1.3.3', '1.3.4', '1.3.5', '1.3.6', '1.3.7', '1.3.8', '1.3.9', '1.3.10', '1.3.12', '1.3.13', '1.3.14', '1.3.15', '1.4.0', '1.4.1', '1.4.2', '1.4.3', '1.4.4', '1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.10', '1.4.11', '1.4.12', '1.4.13', '1.4.14', '1.4.15', '1.4.16', '1.5.0', '1.5.1', '1.6.0', '1.6.1', '1.6.2', '1.6.3', '1.6.4', '1.6.5', '1.6.6', '1.6.7', '1.6.8', '1.6.9', '1.6.10', '1.6.11', '1.6.12', '1.6.13', '1.6.14', '1.6.15', '1.6.16', '1.6.17', '1.6.18', '1.7.0', '1.7.1', '1.7.2', '1.7.3', '1.7.4', '1.7.5', '1.7.6', '1.7.7', '1.7.8', '1.7.9', '1.7.10', '1.7.11', '1.7.12', '1.7.13', '1.7.14', '1.7.15', '1.7.16', '1.8.1', '1.8.2-beta.1', '1.8.2-beta.2', '1.8.2-beta.3', '1.8.2-beta.4', '1.8.2-beta.5', '1.8.2', '1.8.3', '1.8.4', '1.8.5', '1.8.6', '1.8.7', '1.8.8', '2.0.0-beta.1', '2.0.0-beta.2', '2.0.0-beta.3', '2.0.0-beta.4', '2.0.0-beta.5', '2.0.0-beta.6', '2.0.0-beta.7', '2.0.0-beta.8', '2.0.0', '2.0.1', '2.0.2', '2.0.3', '2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8-nightly.20180819', '2.0.8-nightly.20180820', '2.0.8', '2.0.9', '2.0.10', '2.0.11', '2.0.12', '2.0.13', '2.0.14', '2.0.15', '2.0.16', '2.0.17', '2.0.18', '2.1.0-unsupported.20180809', '3.0.0-beta.1', '3.0.0-beta.2', '3.0.0-beta.3', '3.0.0-beta.4', '3.0.0-beta.5', '3.0.0-beta.6', '3.0.0-beta.7', '3.0.0-beta.8', '3.0.0-beta.9', '3.0.0-beta.10', '3.0.0-beta.11', '3.0.0-beta.12', '3.0.0-beta.13', '3.0.0-nightly.20180818', '3.0.0-nightly.20180821', '3.0.0-nightly.20180904', '3.0.0', '3.0.1', '3.0.2', '3.0.3', '3.0.4', '3.0.5', '3.0.6', '3.0.7', '3.0.8', '3.0.9', '3.0.10', '3.0.11', '3.0.12', '3.0.13', '3.0.14', '3.0.15', '3.0.16', '3.1.0-beta.1', '3.1.0-beta.2', '3.1.0-beta.3', '3.1.0-beta.4', '3.1.0-beta.5', '3.1.0', '3.1.1', '3.1.2', '3.1.3', '3.1.4', '3.1.5', '3.1.6', '3.1.7', '3.1.8', '3.1.9', '3.1.10', '3.1.11', '3.1.12', '3.1.13', '4.0.0-beta.1', '4.0.0-beta.2', '4.0.0-beta.3', '4.0.0-beta.4', '4.0.0-beta.5', '4.0.0-beta.6', '4.0.0-beta.7', '4.0.0-beta.8', '4.0.0-beta.9', '4.0.0-beta.10', '4.0.0-beta.11', '4.0.0-nightly.20180816', '4.0.0-nightly.20180817', '4.0.0-nightly.20180819', '4.0.0-nightly.20180821', '4.0.0-nightly.20180929', '4.0.0-nightly.20181006', '4.0.0-nightly.20181010', '4.0.0', '4.0.1', '4.0.2', '4.0.3', '4.0.4', '4.0.5', '4.0.6', '4.0.7', '4.0.8', '4.1.0', '4.1.1', '4.1.2', '4.1.3', '4.1.4', '4.1.5', '4.2.0', '4.2.1', '4.2.2', '4.2.3', '4.2.4', '4.2.5', '4.2.6', '4.2.7', '4.2.8', '4.2.9', '4.2.10', '4.2.11', '4.2.12', '5.0.0-beta.1', '5.0.0-beta.2', '5.0.0-beta.3', '5.0.0-beta.4', '5.0.0-beta.5', '5.0.0-beta.6', '5.0.0-beta.7', '5.0.0-beta.8', '5.0.0-beta.9', '5.0.0', '5.0.1', '5.0.2', '5.0.3', '5.0.4', '5.0.5', '5.0.6', '5.0.7', '5.0.8', '5.0.9', '5.0.10', '5.0.11', '5.0.12', '5.0.13', '6.0.0-beta.1', '6.0.0-beta.2', '6.0.0-beta.3', '6.0.0-beta.4', '6.0.0-beta.5', '6.0.0-beta.6', '6.0.0-beta.7', '6.0.0-beta.8', '6.0.0-beta.9', '6.0.0-beta.10', '6.0.0-beta.11', '6.0.0-beta.12', '6.0.0-beta.13', '6.0.0-beta.14', '6.0.0-beta.15', '6.0.0', '6.0.1', '6.0.2', '6.0.3', '6.0.4', '6.0.5', '6.0.6', '6.0.7', '6.0.8', '6.0.9', '6.0.10', '6.0.11', '6.0.12', '6.1.0', '6.1.1', '6.1.2', '6.1.3', '6.1.4', '6.1.5', '6.1.6', '6.1.7', '6.1.8', '6.1.9', '6.1.10', '6.1.11', '6.1.12', '7.0.0-beta.1', '7.0.0-beta.2', '7.0.0-beta.3', '7.0.0-beta.4', '7.0.0-beta.5', '7.0.0-beta.6', '7.0.0-beta.7', '7.0.0', '7.0.1', '7.1.0', '7.1.1', '7.1.2', '7.1.3', '7.1.4', '7.1.5', '7.1.6', '7.1.7', '7.1.8', '7.1.9', '7.1.10', '7.1.11', '7.1.12', '7.1.13', '7.1.14', '7.2.0', '7.2.1', '7.2.2', '7.2.3', '7.2.4', '7.3.0', '8.0.0-beta.1', '8.0.0-beta.2', '8.0.0-beta.3', '8.0.0-beta.4', '8.0.0-beta.5', '8.0.0-beta.6', '8.0.0-beta.7', '8.0.0-beta.8', '8.0.0-beta.9', '8.0.0', '8.0.1', '8.0.2', '8.0.3', '8.1.0', '8.1.1', '8.2.0', '8.2.1', '8.2.2', '8.2.3', '8.2.4', '8.2.5', '8.3.0', '9.0.0-beta.1', '9.0.0-beta.2', '9.0.0-beta.3', '9.0.0-beta.4', '9.0.0-beta.5', '9.0.0-beta.6', '9.0.0-beta.7', '9.0.0-beta.9', '9.0.0-beta.10', '9.0.0-beta.12', '9.0.0-beta.13', '9.0.0-beta.14', '9.0.0-beta.15', '9.0.0-beta.16', '9.0.0-beta.17', '9.0.0-beta.18', '9.0.0-beta.19', '9.0.0-beta.20', '9.0.0-beta.21', '9.0.0-beta.22', '9.0.0-beta.24', '9.0.0', '10.0.0-beta.1' ]
接下來(lái),我們使用如下命令卸載原來(lái)的electron:
npm uninstall -g electron

使用安裝electron的命令:
npm uninstall -g electron

[選] 如果出現(xiàn)如上圖所示,說(shuō)明是文件夾沒(méi)刪干凈。這時(shí)候我們需要去刪除對(duì)應(yīng)文件夾,它這里提示該路徑文件還存在。
'C:\Users\xiaoc\AppData\Roaming\npm\node_modules\electron\cli.js
輸入win + r, 輸入
%AppData%

在該文件夾下 找到 npm,點(diǎn)進(jìn)去

把該目錄下的文件全刪了。在 cmd中重新執(zhí)行
npm install -g electron@v6.1.7
@后面跟隨的是版本號(hào),具體可以更新的版本號(hào)在上面所示。

這樣就表示已經(jīng)安裝完畢。
接下來(lái)查看版本

已經(jīng)更新到想要的版本了.
方式二 :官網(wǎng)
以上就是electron版本升級(jí)的簡(jiǎn)單步驟的詳細(xì)內(nèi)容,更多關(guān)于electron版本升級(jí)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JS點(diǎn)擊某個(gè)圖標(biāo)或按鈕彈出文件選擇框的實(shí)現(xiàn)代碼
本文給大家介紹js點(diǎn)擊點(diǎn)擊某個(gè)圖標(biāo)或按鈕彈出文件選擇框的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),感興趣的朋友可以參考下2016-09-09
微信小程序 SOTER 生物認(rèn)證DEMO 指紋識(shí)別功能
這篇文章主要介紹了微信小程序 SOTER 生物認(rèn)證DEMO指紋識(shí)別功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
JavaScript中alert的使用方法超詳細(xì)介紹
JS中的alert作用是在瀏覽器中彈出一個(gè)警告框,而使用alert有三種方式,不同的方式所呈現(xiàn)的效果也不相同,這篇文章主要給大家介紹了關(guān)于JavaScript中alert使用方法的相關(guān)資料,需要的朋友可以參考下2024-01-01
前端URL拼接路徑參數(shù)具體實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于前端URL拼接路徑參數(shù)具體實(shí)現(xiàn)的相關(guān)資料,url地址拼接是經(jīng)常會(huì)遇到的問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
計(jì)算世界完全對(duì)稱(chēng)日的js代碼,粗糙版
世界完全對(duì)稱(chēng)日,指的是公歷紀(jì)年日期中數(shù)字左右完全對(duì)稱(chēng)的日期,這一天朋友們可以互送祝福同時(shí)讓大家感嘆時(shí)間的寶貴。2011-11-11

