将参数传递给打包的电子应用程序 [英] Pass arguments to packaged electron application

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

问题描述

我们正在使用 electron-packager 来捆绑和分发我们的前端Web应用程序.我们需要能够将服务器的hostport传入到electron前端进行连接.当我们通过 electron main.js --host blah --port 8080 启动时,它可以工作.打包后,我们通过 ./MyApp --host blah --port 8080 运行,但它不起作用.这很糟糕,因为我们不希望客户需要自己安装电子/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?

推荐答案

你是如何解析命令行的?当您以 ./MyApp --host blah --port 8080 开头时,process.argv 是什么样的?

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 it在其资源文件夹中用于app"、app.asar"或default_app";当您使用 electron 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天全站免登陆