使用 electron-builder electron-updater 跨 electon 应用程序更新的持久文件存储
Persistent file storage across electon app updates with electron-builder electron-updater
当我使用 electron-builder autoUpdater 更新 Electron 应用程序时,我定义的所有文件存储都被覆盖。我需要哪些设置才能使这些文件持久化?
这是一个 MCVE 示例(针对主进程):
const { app, BrowserView, BrowserWindow,ipcMain } = require('electron');
const log = require("electron-log");
const { autoUpdater } = require("electron-updater");
const fs = require( 'fs');
const path = require('path');
let win
let rand = String(Math.floor(Math.random() * 10000));
console.log('Generated random number',rand)
app.whenReady().then(async () => {
win = new BrowserWindow({
fullscreen: false,
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html');
setInterval(function() {
let a = autoUpdater.checkForUpdatesAndNotify();
}, 60000);
let exists = false;
fs.stat('persistentFile',(err, stats) => {
if (err == null){
exists = true
}
if (!exists){
fs.writeFile('persistentFile',rand,(err)=>{
if (err) throw err;
win.webContents.send('console_message', 'Persistent file has been created!',rand);
console.log('Persistent file has been created!',rand);
})
}
else {
fs.readFile('persistentFile',(err,data)=>{
if (err) throw err;
win.webContents.send('console_message', 'Persistent already exists',data);
console.log('Persistent already exists',data);
})
}
})
win.webContents.send('console_message', 'Random No is' + String(rand));
})
在本示例中,我希望文件 persistentFile
在更新后保持不变,以便它包含从第一个版本生成的数字。但是,目前,每次更新都会覆盖该文件。
您将如何确保跨 electron-builder 自动更新的持久文件存储?
在看了 Cameron Nokes 的优秀博客后,我设法做到了这一点:
cameronnokes.com/blog/how-to-store-user-data-in-electron:
Storing user data in the operating system’s designated location for
user’s app data is the idiomatic way for native app’s to persist user
data because:
when we auto-update the app, our source files may get moved or delete
changing or adding to an app’s internal files will invalidate the code
signature
为此,我使用 const userDataPath = app.getPath('userData');
获取 OS 应用程序存储位置的路径。然后将对 'persistentFile'
的引用更改为 String(userDataPath)+'/persistentFile'
:
当我使用 electron-builder autoUpdater 更新 Electron 应用程序时,我定义的所有文件存储都被覆盖。我需要哪些设置才能使这些文件持久化?
这是一个 MCVE 示例(针对主进程):
const { app, BrowserView, BrowserWindow,ipcMain } = require('electron');
const log = require("electron-log");
const { autoUpdater } = require("electron-updater");
const fs = require( 'fs');
const path = require('path');
let win
let rand = String(Math.floor(Math.random() * 10000));
console.log('Generated random number',rand)
app.whenReady().then(async () => {
win = new BrowserWindow({
fullscreen: false,
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html');
setInterval(function() {
let a = autoUpdater.checkForUpdatesAndNotify();
}, 60000);
let exists = false;
fs.stat('persistentFile',(err, stats) => {
if (err == null){
exists = true
}
if (!exists){
fs.writeFile('persistentFile',rand,(err)=>{
if (err) throw err;
win.webContents.send('console_message', 'Persistent file has been created!',rand);
console.log('Persistent file has been created!',rand);
})
}
else {
fs.readFile('persistentFile',(err,data)=>{
if (err) throw err;
win.webContents.send('console_message', 'Persistent already exists',data);
console.log('Persistent already exists',data);
})
}
})
win.webContents.send('console_message', 'Random No is' + String(rand));
})
在本示例中,我希望文件 persistentFile
在更新后保持不变,以便它包含从第一个版本生成的数字。但是,目前,每次更新都会覆盖该文件。
您将如何确保跨 electron-builder 自动更新的持久文件存储?
在看了 Cameron Nokes 的优秀博客后,我设法做到了这一点:
cameronnokes.com/blog/how-to-store-user-data-in-electron:
Storing user data in the operating system’s designated location for user’s app data is the idiomatic way for native app’s to persist user data because:
when we auto-update the app, our source files may get moved or delete
changing or adding to an app’s internal files will invalidate the code signature
为此,我使用 const userDataPath = app.getPath('userData');
获取 OS 应用程序存储位置的路径。然后将对 'persistentFile'
的引用更改为 String(userDataPath)+'/persistentFile'
: