BlueRoseNote/07-Other/Node.js/Electron/解决使用electron-packager打包时因下载包失败而导致打包失败的问题.md
2023-06-29 11:55:02 +08:00

61 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 修改electron-packager代码
首先确认electron-packager安装位置全局或者局部之后进入`node_modules\electron-packager\`路径。使用VSCode搜索`packageForPlatformAndArch`。代码如下:
```c#
packageForPlatformAndArch (downloadOpts) {
return download.downloadElectronZip(downloadOpts)
.then(zipPath => {
// Create delegated options object with specific platform and arch, for output directory naming
const comboOpts = Object.assign({}, this.opts, {
arch: downloadOpts.arch,
platform: downloadOpts.platform,
electronVersion: downloadOpts.version
})
if (!this.useTempDir) {
return this.createApp(comboOpts, zipPath)
}
if (common.isPlatformMac(comboOpts.platform)) {
/* istanbul ignore else */
if (this.canCreateSymlinks === undefined) {
return this.testSymlink(comboOpts, zipPath)
} else if (!this.canCreateSymlinks) {
return this.skipHostPlatformSansSymlinkSupport(comboOpts)
}
}
return this.checkOverwrite(comboOpts, zipPath)
})
}
```
添加逻辑:
```
let zipFile="electron-v11.2.1-win32-x64.zip";
console.log(__dirname);
let HasFile=fs.existsSync(zipFile);
if(HasFile)
{
const comboOpts = Object.assign({}, this.opts, {
arch: downloadOpts.arch,
platform: downloadOpts.platform,
electronVersion: downloadOpts.version
})
if (!this.useTempDir) {
return this.createApp(comboOpts, zipFile)
}
if (common.isPlatformMac(comboOpts.platform)) {
/* istanbul ignore else */
if (this.canCreateSymlinks === undefined) {
return this.testSymlink(comboOpts, zipFile)
} else if (!this.canCreateSymlinks) {
return this.skipHostPlatformSansSymlinkSupport(comboOpts)
}
}
return this.checkOverwrite(comboOpts, zipFile)
}
```
其中执行路径即为项目根目录直接将zip放在项目目录中即可。