简体   繁体   English

在使用电子构建器安装应用程序的同时向用户环境添加命令行脚本

[英]Adding a command line script to the user environment whilst installing an application using electron-builder

I'm currently working on a project with Electron 9.0.4 and Electron-Builder 22.8.0 and am faced with a problem that doesn't seem too difficult but there isn't a valid solution online!我目前正在使用Electron 9.0.4Electron-Builder 22.8.0开展一个项目,并且面临一个看起来不太困难但在线没有有效解决方案的问题! (At least I couldn't find it) (至少我找不到)

I have my main program that does all of the UI tasks, and a command line script that does some backend.我有执行所有 UI 任务的主程序和执行某些后端的命令行脚本。 The reason I have this command line script is so that I can run certain parts of the application without opening the window itself.我有这个命令行脚本的原因是我可以在不打开窗口本身的情况下运行应用程序的某些部分。 Everything works fine on my computer.在我的电脑上一切正常。 After running npm link , my CL script is added to my environment variables and I can just run it from the console.运行npm link ,我的 CL 脚本被添加到我的环境变量中,我可以从控制台运行它。 However, when I try to build with electron-builder , the problem occurs.但是,当我尝试使用electron-builder ,问题出现了。

If I use my Setup.exe on another computer, the command line script just won't be added to the environment variables and I couldn't find instructions on how to do this in the electron , nodejs , or electron-builder documentation.如果我在另一台计算机上使用我的Setup.exe ,命令行脚本将不会添加到环境变量中,而且我无法在electronnodejselectron-builder文档中找到有关如何执行此操作的说明。 What I found was a suggestion on another question to add npm -g install as a post-install script, but that had no effect either.我发现是关于另一个问题的建议将npm -g install添加为安装后脚本,但这也没有效果。

Someone else suggested adding npm link as a post-installation script, but firstly if I am not mistaken this function is not intended for production and secondly it created an infinite loop as npm link triggered the post-installation script over and over again.其他人建议添加npm link作为安装后脚本,但首先,如果我没记错的话,这个函数不是用于生产的,其次它创建了一个无限循环,因为npm link一遍又一遍地触发安装后脚本。

Thats how the script is added to the project这就是将脚本添加到项目中的方式

"bin": {
    "command-name": "/cl.js"
  }

Any help is appreciated!任何帮助表示赞赏!

Since I couldn't find a direct solution to my problem and didn't want to look any further for a solution while being able to take a different approach.由于我找不到直接解决我的问题的方法,并且不想在能够采取不同方法的同时进一步寻找解决方案。

I decided to take a step back and look for another method to solve my problem I came to the conclusion that I didn't really need to add a script to the command line.我决定退后一步,寻找另一种方法来解决我的问题。我得出的结论是,我真的不需要向命令行添加脚本。 My solution was to look for a certain argument when starting the regular application.我的解决方案是在启动常规应用程序时寻找某个参数。

if (process.argv.includes("cli")) {  /* Do commandline stuff */ }

When the custom argument is found, I simply run the script that should've been run from the command line.找到自定义参数后,我只需运行应该从命令行运行的脚本。 Using this approach, you can create a shortcut to my executable that contains the custom argument and then instead of the application it runs the command line script.使用这种方法,您可以创建一个包含自定义参数的可执行文件的快捷方式,然后它运行命令行脚本而不是应用程序。

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

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