Electron官方提供了一个npm包“electron-dl”,可以帮助我们在不同操作系统上下载相应版本的Electron。
首先需要安装“electron-dl”:
npm install electron-dl
然后可以使用以下示例代码来下载Electron:
const { download } = require('electron-dl');
const electronVersion = '12.0.2'; // 需要下载的Electron版本号
const platform = process.platform; // 获取当前操作系统
download(BrowserWindow.getFocusedWindow(), `https://github.com/electron/electron/releases/download/v${electronVersion}/electron-v${electronVersion}-darwin-x64.zip`, { // 下载Mac版Electron
filename: `electron-v${electronVersion}.zip`,
directory: './',
onProgress: (progress) => {
// 下载进度
console.log(progress);
}
}).then(dl => {
// 下载完成后的处理
console.log(dl.getSavePath());
}).catch(err => {
// 下载失败的处理
console.log(err);
});
在上面的代码中,我们根据当前操作系统选择下载相应的版本,并使用“electron-dl”的“download”方法来进行下载。下载完成后,我们可以处理下载文件的路径等信息。