简体   繁体   English

来自电子构建器的 AppImage 文件系统不起作用

[英]AppImage from electron-builder with file system not working

Im using electron builder to compile my electron app to an .AppImage file and im using the fs module to write to an .json file but its not working in the appimage format (its working fine when i have the normal version not made with electron builder).我使用电子生成器将我的电子应用程序编译为 .AppImage 文件,并使用 fs 模块写入 .json 文件,但它不能以 appimage 格式工作(当我使用不是用电子生成器制作的普通版本时,它工作正常)。 I can still read from the file.我仍然可以从文件中读取。
the code (preload):代码(预加载):

setSettings: (value) => {fs.writeFileSync(path.join(__dirname, "settings.json"), JSON.stringify(value), "utf8")}

the code (on the website): 代码(在网站上):
 api.setSettings(settings);

the project: https://github.com/Nils75owo/crazyshit 项目:https://github.com/Nils75owo/crazyshit

That's not a problem with AppImage or Electron Builder but with the way you're packaging your app.这不是 AppImage 或 Electron Builder 的问题,而是您打包应用程序的方式。 Since you didn't post your package.json * , I can only guess what's wrong, but probably you haven't changed Electron Builder's default behaviour regarding packing your application.由于您没有发布您的package.json * ,我只能猜测出了什么问题,但您可能没有更改 Electron Builder 关于打包应用程序的默认行为。

By default, Electron Builder compiles your application, including all resources, into a single archive file in the ASAR format (think of it like the TAR format).默认情况下,Electron Builder 将您的应用程序(包括所有资源)编译为 ASAR 格式的单个存档文件(将其视为 TAR 格式)。 Electron includes a patched version of the fs module to be able to read from the ASAR file, but writing to it is obviously not supported. Electron 包含一个补丁版本的fs模块,可以读取 ASAR 文件,但显然不支持写入。

You have two options to mitigate this problem: Either you store your settings somewhere in the user's directory (which is the way I'd go, see below) or you refrain from packing your application to an ASAR file, but that will leave all your JavaScript code outside the executable in a simple folder.您有两种选择来缓解这个问题:要么将您的设置存储在用户目录中的某处(这是我要走的方式,见下文),要么避免将您的应用程序打包到 ASAR 文件中,但这会留下您所有的可执行文件之外的 JavaScript 代码位于一个简单的文件夹中。 (Note that ASAR is not capable of keeping your code confidential, because there are applications which can extract such archives, but it makes it at least a little harder for attackers or curious eyes to get a copy of your code.) (请注意,ASAR 无法对您的代码保密,因为有些应用程序可以提取此类档案,但这至少会让攻击者或好奇的眼睛更难获取您的代码副本。)

To disable packing to ASAR, simply tell Electron Builder that you don't want it to compile an archive.要禁用对 ASAR 的打包,只需告诉 Electron Builder 您不希望它编译存档。 Thus, in your package.json , include the following:因此,在您的package.json ,包含以下内容:

{
    // ... other options
    "build": {
        // ... other build options
        "asar": false
    }
}

However, as I mentioned above, it's probably wiser to store settings in a common place where advanced users can actually find (and probably edit, mostly for troubleshooting) them.但是,正如我上面提到的,将设置存储在高级用户可以实际找到(并且可能编辑,主要用于故障排除)的公共位置可能更明智。 On Linux, one such folder would be ~/.config , where you could create a subdirectory for your application.在 Linux 上,一个这样的文件夹是~/.config ,您可以在其中为您的应用程序创建一个子目录。

To get the specific application data path on a cross-platform basis, you can query Electron's app module from within the main process.要在跨平台的基础上获取特定的应用程序数据路径,您可以从主进程中查询 Electron 的app模块。 You can do so like this:你可以这样做:

const { app } = require ("electron"),
    path = require ("path");

var configPath;

try {
    configPath = path.join (app.getPath ("appData"), "your-app-name");
} catch (error) {
    console.error (error);
    app.quit ();
}

If you however have correctly set your application's name (by using app.setName ("..."); ), you can instead simply use app.getPath ("userData");但是,如果您正确设置了应用程序的名称(通过使用app.setName ("..."); ),则可以改为简单地使用app.getPath ("userData"); and omit the path joining.并省略路径加入。 Take a look at the documentation !看看 文档

For my Electron Applications, I typically choose to store settings in a common hidden directory (one example for a name could be the brand under which you plan to market the application) in the user's home directory and I then have separate directories for each application.对于我的 Electron 应用程序,我通常选择将设置存储在用户主目录中的公共隐藏目录中(名称的一个示例可以是您计划推广应用程序的品牌),然后我为每个应用程序设置单独的目录。 But how you organise this is up to you completely.但是你如何组织这完全取决于你。


* For the future, please refrain from directing us to a GitHub repository and instead include all information (and code is information too) needed to replicate/understand your problem in your question. *对于未来,请不要将我们定向到 GitHub 存储库,而是包含在您的问题中复制/理解您的问题所需的所有信息(代码也是信息)。 That'd save us a lot of time and could potentially get you answers faster.这将为我们节省大量时间,并可能更快地为您提供答案。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM