Electron - 解压时包无效

Lor*_*ley 5 javascript node.js electron

大约 3 周以来,我一直在开发 Electron 应用程序,并最终决定添加更新检查。根据我的研究,在 Electron 中执行此操作的标准方法(使用 Squirrel)要求用户将应用程序物理安装到他们的计算机上。我宁愿不这样做,并让一切尽可能便携。然后,我决定尝试通过让程序下载 update.zip 来制作自己的更新脚本,并将其解压以覆盖现有文件。这种方法效果很好,直到最后。在提取的最后,我收到一个Invalid package错误,并且实际app.asar文件丢失,导致应用程序无用。

我正在使用它来下载并提取更新:

function downloadFile(url, target, fileName, cb) { // Downloads
    var req = request({
        method: 'GET',
        uri: url
    });

    var out = fs.createWriteStream(target+'/'+fileName);
    req.pipe(out);

    req.on('end', function() {
        unzip(target+'/'+fileName, target, function() {
            if (cb) {
                cb();
            }
        });
    });
}
function unzip(file, target, cb) { // Unzips

    var out = fs.createReadStream(file);
    out.pipe(unzipper.Extract({ path: target })).on('finish', function () {
        dialog.showMessageBox({
            type: 'question',
            message: 'Finished extracting to `'+target+'`'
        });

        if (cb) {
            cb();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

并用以下方式调用它:

downloadFile('http://example.com/update.zip', path.join(__dirname, './'), 'update.zip', function() { // http://example.com/update.zip is not the real source
    app.relaunch();
    app.quit();
});
Run Code Online (Sandbox Code Playgroud)

我使用unzipperNPM 包(https://www.npmjs.com/package/unzipper)。

该代码适用于所有其他 zip,但在尝试提取包含 Electron 应用程序的 zip 时失败。

我做错了什么,或者可能是正确支持使用 .asar 文件提取 zip 的不同软件包?

编辑1 我刚刚发现https://www.npmjs.com/package/electron-basic-updater,它不会抛出相同的JavaScript错误,但它仍然无法正确提取.asar文件,并且会抛出它自己的错误。由于.asar仍然丢失,因此“更新”后该应用程序仍然无用

Kon*_*tin 2

感谢您的链接electron-basic-updater,我发现其中提到了这个问题:https://github.com/TamkeenLMS/electron-basic-updater/issues/4

他们提到了电子应用程序中的问题: https: //github.com/electron/electron/issues/9304

最后,在第二个主题的最后有一个解决方案:

这是由于 Electron fs 模块将 asar 文件视为目录而不是文件。要使解压缩过程正常工作,您需要执行以下两件事之一:

  • 设置 process.noAsar = true
  • 使用original-fs代替fs

我见过有人使用original-fs。但这对我来说似乎是个大麻烦。

所以我尝试设置process.noAsar = true(然后process.noAsar = false解压缩后) - 这就像一个魅力。