应用程序更新问题 yml 文件不是在电子中生成的吗?

Problem with app update yml files is not generated in electron?

我的electron应用自动更新有问题, 在我完成所有应用程序部分并尝试将其推送到我的自定义更新服务器后,我在我的记录器中发现了这条错误消息:

Error unknown ENOENT: no such file or directory, open 
'C:\{appPath}\{appName}\resources\app-update.yml'

这是我的package.json构建配置

"build": {
    "appId": "com.server.app",
    "copyright": "Copyright company name",
    "generateUpdatesFilesForAllChannels": true,
    "win": {
      "target": "nsis",
      "icon": "build/icon.ico"
    },
    "mac": {
      "target": "dmg",
      "artifactName": "appName.dmg",
      "icon": "build/icon.icns"
    },
    "dmg": {
      "background": "build/i-bg.tif",
      "icon": "build/setup.icns",
      "iconSize": 80,
      "title": "${productName}-${version}",
      "window": {
        "width": 540,
        "height": 380
      }
    },
    "nsis": {
      "artifactName": "${productName}-Setup-${version}.${ext}",
      "oneClick": false,
      "perMachine": false,
      "allowToChangeInstallationDirectory": true,
      "installerIcon": "build/setup.ico",
      "uninstallerIcon": "build/setup.ico",
      "installerHeader": "build/installerHeader.bmp",
      "installerSidebar": "build/installerSidebar.bmp",
      "runAfterFinish": true,
      "deleteAppDataOnUninstall": true,
      "createDesktopShortcut": "always",
      "createStartMenuShortcut": true,
      "shortcutName": "AppName",
      "publish": [{
        "provider": "generic",
        "url": "https://my-update-server/path"
      }]
    },
    "extraFiles": [
      "data",
      "templates"
    ]
  },
  "publish": [{
    "provider": "generic",
    "url": "https://my-update-server/path"
  }],

这里是触发自动更新的代码

//-----------------------------------------------
// Auto-Update event listening 
//-----------------------------------------------

autoUpdater.on('checking-for-update', () => {
  splashLoadingStatus(`Checking for ${appName} update ...`);
})

autoUpdater.on('update-available',(info) => {
  splashLoadingStatus(`${appName} new update available.`);
})

autoUpdater.on('update-progress',(progInfo) => {
  splashLoadingStatus(`Download speed: ${progInfo.bytesPerSecond} - Download ${progInfo.percent}% (${progInfo.transferred}/${progInfo.total})`);
})

autoUpdater.on('error' , (error) => {
  dialog.showErrorBox('Error', error.message);
})

autoUpdater.on('update-downloaded', (info) => {
  const message = {
    type: 'info',
    buttons: ['Restart', 'Update'],
    title: `${appName} Update`,
    detail: `A new version has been downloaded. Restart ${appName} to apply the updates.`
  }

  dialog.showMessageBox(message, (res) => {
    if(res === 0) {
      autoUpdater.quitAndInstall();
    }
  })
})
.....
          autoUpdater.setFeedURL('https://my-update-server/path');
          autoUpdater.checkForUpdatesAndNotify();
.....

然后,当我推送构建时,它会正确生成 latest.yml 文件,但安装后我发现应用程序-update.yml 不存在 ...

如果您遇到缺少 app-update.yml 和 dev-app-update.yml 的问题,请将以下代码粘贴到 index.js:

import path from "path"
import fs from "fs"
const feed = 'your_site/update/windows_64'

let yaml = '';

yaml += "provider: generic\n"
yaml += "url: your_site/update/windows_64\n"
yaml += "useMultipleRangeRequest: false\n"
yaml += "channel: latest\n"
yaml += "updaterCacheDirName: " + app.getName()

let update_file = [path.join(process.resourcesPath, 'app-update.yml'), yaml]
let dev_update_file = [path.join(process.resourcesPath, 'dev-app-update.yml'), yaml]
let chechFiles = [update_file, dev_update_file]

for (let file of chechFiles) {
    if (!fs.existsSync(file[0])) {
        fs.writeFileSync(file[0], file[1], () => { })
    }
}

我在 autoUpdater.checkForUpdates() 之前使用 autoUpdater.setFeedURL() 修复了它。以下是适用于 github 版本的代码片段。此外,请确保在 运行 此代码之前 Github 中存在现有版本。

import { autoUpdater } from "electron-updater";

// ...

autoUpdater.setFeedURL({
  provider: "github",
  owner: "org",
  repo: "repo",
});