在Electron应用程序中管理应用程序设置 [英] Managing application settings in an Electron application

查看:141
本文介绍了在Electron应用程序中管理应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道如何一个在电子应用程序中管理应用程序设置?我在这里找到了一些很好的资源(



只要我正在开发应用程序,它就可以正常工作。该应用程序正在正确地从 config 文件夹中拾取 default.json 文件,并正确地应用了这些设置。



问题是在打包应用程序(MSI,DMG等)时出现的。我正在使用 electron-builder 为此目的打包。



config 软件包的问题在于它寻找 config 文件夹位于应用程序当前工作目录中,并且由于在安装应用程序的文件夹中找不到该文件夹​​,因此它只会引发错误。我什至尝试手动将此文件夹复制到我的 app 文件夹中(这是电子制造商用来制造软件包的文件夹),但这也无济于事。理想情况下,我想将应用程序设置捆绑在应用程序的ASAR文件中,以使其无法反编译。



我的问题是:




  • 人们如何管理电子应用程序的应用程序设置?

  • 可以配置 NPM软件包可以用来做什么?还是有专门针对电子应用的封装的替代品?


解决方案

我没有使用npm软件包,但是我的方法与您提到的类似。我有一个配置目录,其中包含针对不同环境的不同配置文件:dev,test,prod。然后在我的package.json中添加了特定于环境的构建命令。例如对于产品:

  build-prod-config: config / buildProdConfig.sh,
build- renderer-prod: webpack --config = webpack.config.renderer.prod.js,
build-main-prod: webpack --config = webpack.config.main.prod.js ,
build-prod: npm run build-prod-config&& npm run build-main-prod& npm run build-renderer-prod,

buildProdConfig .sh
#!/ usr / bin / env bash

cp config / app.config.prod.js config / app.config.js
echo将ProdConfig复制到配置

//配置文件看起来像
const Config = {
suppDataDirectoryPath:'/ suppData /',
BuiltFor:'prod',
}

module.exports = Config;

然后我在应用程序中需要的任何地方都需要配置并使用这些值。目前这很简单,也许您所链接的config软件包没有灵活性,但是可以使用。



另外,另一重要的事情是我没有将应用程序打包到ASAR档案中,但是我认为我的方法仍然有效,因为我使用webpack。


I'm curious to know how can one manage application settings in an Electron application? I have found some excellent resources here (Where to store user settings in Electron (Atom Shell) Application?, for example) and elsewhere when it comes to managing user settings but couldn't find anything related to managing application settings.

To me, the difference between the two is that application settings could vary from environment to environment (dev/test/production) but would remain the same for all users of the application. They would contain things like API endpoints etc. User settings on the other hand would change from user to user based on their preferences like window width/height etc.

What I have done so far?

I have found this excellent package called config and start using it in my project. Based on the instructions, I have created a config folder and a default configuration file (I will create environment specific configuration files later).

It is working fine as long as I am developing the application. The application is picking up the default.json file properly from the config folder and is applying those settings correctly.

The problem comes when I package the application (MSI, DMG etc.). I am using electron-builder package for that purpose.

The problem with config package is that it looks for config folder inside the application's current working directory and because it doesn't find it in the folder where the application is installed, it simply throws an error. I even tried to manually copy this folder in my app folder (which is where electron-builder makes the packages) but that didn't help either. Ideally I would like to bundle the app settings in application's ASAR file so that it can't be decompiled.

My questions are:

  • How are people managing application settings for an Electron application?
  • Can config NPM package be used for that? Or is there an alternative to that package specifically for Electron applications?

解决方案

I am not using an npm package but my approach is similar to what you have mentioned. I have a config directory with different config files for different environments: dev, test, prod. Then in my package.json I have added environment specific build commands. e.g. For prod:

"build-prod-config": "config/buildProdConfig.sh",
"build-renderer-prod": "webpack --config=webpack.config.renderer.prod.js",
"build-main-prod": "webpack --config=webpack.config.main.prod.js",
"build-prod": "npm run build-prod-config && npm run build-main-prod & npm run build-renderer-prod",

buildProdConfig.sh
#!/usr/bin/env bash

cp config/app.config.prod.js config/app.config.js
echo "Copied ProdConfig to Config"

//This is what a config file looks like
const Config = {
  suppDataDirectoryPath: '/suppData/',
  builtFor: 'prod',
}

module.exports = Config;

I then require Config whereever I need in my application and use the values. This is a simple thing for now, and perhaps doesn't have the flexibility of the config package you linked to, but it works.

Also, another important thing is that I am not packing my application into an ASAR archive, but I think my approach would still work because I am packing everything into a bundle using webpack.

这篇关于在Electron应用程序中管理应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆