将参数传递给打包电子申请

时间:2016-01-11 21:55:37

标签: javascript node.js electron atom-editor yargs

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

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

2 个答案:

答案 0 :(得分:4)

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?

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.

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.

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.

答案 1 :(得分:0)

我建议您使用命令行参数管理系统,例如“minimist”。

您可以在json中使用它:"start": "electron . --srv=server.com --prt=112 --arg3=myarg3"

在你的main.js中你可以使用它:

var args = require('minimist')(process.argv);

console.log(args)

你可以在主javascript文件中使用你的args。

对于Package,您可以通过添加myapp.exe --srv=server.com --prt=112 --arg3=myarg3

在快捷方式中执行相同的操作