将参数传递给打包电子申请 [英] Pass arguments to packaged electron application

查看:104
本文介绍了将参数传递给打包电子申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 electron-packager 捆绑和分发我们的前端Web应用程序。我们需要能够将服务器的主机端口传递到电子前端进行连接。当我们通过电子main.js --host blah --port 8080 启动时,它可以工作。一旦打包,我们通过 ./ MyApp --host blah --port 8080 运行,但它不起作用。这很糟糕,因为我们不希望客户需要自己安装electron / npm。另外值得注意的是,无论我们是否将应用程序打包在 asar 存档中,都会发生这种情况。

We're using electron-packager to bundle up and distribute the front-end of our web application. We need to be able to pass in the host and port of the server to the electron front-end for connecting. When we launch via electron main.js --host blah --port 8080 it works. Once it's packaged, we run via ./MyApp --host blah --port 8080 and it doesn't work. This is bad because we don't want customers to need to install electron/npm itself. Also worth noting is that this happens whether we package the app in an asar archive or not.

我们可以尝试的任何想法,或者我们是否试图以错误的方式解决这个问题?

Any ideas on things we could try, or if we're trying to go about this the wrong way?

推荐答案

那么你是如何解析命令行的呢?什么 process.argv 看起来像是从开头./ MyApp --host blah --port 8080

Well how are you trying to parse the command line? What does process.argv look like when you start with ./MyApp --host blah --port 8080?

基本上,当你启动Electron 它在资源文件夹中查找app,app.asar或default_app;当您使用电子main.js --host blah --port 启动应用程序时,实际发生的是Electron的默认应用程序启动,其中包括解析您的命令行参数。打包应用程序时,它会以'app'或'app.asar'的形式复制到资源文件夹中,并在以后运行 MyApp 时直接启动。也就是说,你是以两种根本不同的方式开始你的应用程序,这可能是你问题的根源。

Basically, when you start Electron it looks in its resource folder for 'app', 'app.asar', or 'default_app'; when you start your app with electron main.js --host blah --port what actually happens is that Electron's default app is started which, among other things, parses your command line arguments. When you package your app, it is copied to the the resource folder as 'app' or 'app.asar' and will be started directly when you run MyApp later on. That is to say, you are starting your app in two fundamentally different ways and this is likely the source of your problem.

为了缓解这种情况,我喜欢做的是在开发过程中将我的开发文件夹链接到Electron的资源文件夹中;这样我就可以绕过'default_app',并且无论应用程序是否打包都有相同的执行路径。

To mitigate this, what I like to do is to link my development folder into Electron's resource folder during development; this way I can bypass 'default_app' and have the same execution path whether or not the app is packaged.

话虽如此,你开始使用哪种方式并不重要应用程序,你绝对应该能够解析命令行参数。作为参考,我刚刚设置了我的一个应用程序与yargs,所以你一定能够让它工作。

Having said that, it does not matter which way you start the app, you should definitely be able to parse the command line arguments. For reference, I just set this up in one of my apps with yargs so you should definitely be able to get this to work.

这篇关于将参数传递给打包电子申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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