简体   繁体   English

使用Node.js更改Bash终端的当前工作目录

[英]Change current working directory of bash terminal with Nodejs

I want to develop a Node CLI app which clones a git repo and cd's into it. 我想开发一个Node CLI应用程序,它将git repo和cd克隆到其中。

For eg. 例如。

dummyuser:~$ dummy-cli-command dummy-git-repo-url
Cloning repository...
dummyuser:~/Workspace/dummy-git-repo$ 

I was able to clone it but am not able to cd into it. 我能够克隆它,但无法将其插入cd I know that node forks a child process under which it executes my code and there is no way to change directory of the bash terminal through which I first executed my node-cli-command. 我知道节点派生了一个子进程,在该子进程下执行我的代码,并且没有办法更改bash终端的目录,而我第一次通过该目录执行node-cli-command。 Can anyone help me with any workaround ? 有人可以帮助我解决任何问题吗?

You will not be able to do that - not with Node and not with any other program. 您将无法执行此操作-不能使用Node或任何其他程序。 You cannot even do it with a shell script, though you can do it with a shell alias or function but only if it is not run as a subprocess. 尽管您可以使用Shell别名或函数来执行此操作,但您甚至不能使用Shell脚本来执行此操作,但前提是它不能作为子进程运行。

What it means is that you may be able to achieve that if your Node program returns the correct directory to a shell alias or shell function that runs it, and it's the alias or function that does the directory changing. 这意味着,如果您的Node程序将正确的目录返回给运行它的shell别名或shell函数,并且是别名或函数会更改目录,则您可能会实现该目标。 That alias or function could know the destination in the first place if it gets it by command line arguments. 如果别名或函数是通过命令行参数获取的,则它首先可以知道目标。 But then again, if you use a shell function then you don't need Node to clone the repo in the first place. 但是话又说回来,如果您使用shell函数,则不需要Node首先克隆存储库。

The bottom line is that you will not be able to change the working directory of your shell from a subprocess so your only options are the ways to do it by running code in the same process like an alias or a function in the shell. 最重要的是,您将无法从子进程更改Shell的工作目录,因此您唯一的选择是通过在同一进程中运行代码(例如别名或Shell中的函数)来执行此操作的方法。

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

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