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

1.9 KiB
Raw Blame History

修改electron-packager代码

首先确认electron-packager安装位置全局或者局部之后进入node_modules\electron-packager\路径。使用VSCode搜索packageForPlatformAndArch。代码如下:

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放在项目目录中即可。