在 VS Code 中打开项目时启动应用程序? [英] Start app when opening project in VS Code?
问题描述
是否可以让 VS 代码在打开项目时运行多个命令并启动一个(或多个)应用程序?
Is it possible to have VS code run several commands and start an app (or several) upon opening a project?
我正在做的事情的理想情况是:
The ideal case for what I am doing would be:
- 在 vscode 中打开项目
- 运行特定的 ngrok 命令
- 启动 Mongo
- 启动 NPM 脚本
目标是在 VS 代码中打开项目,然后直接开始工作,而不是运行几个设置步骤.
The goal being to open the project in VS code and just get to work instead of running through several setup steps.
推荐答案
2019 年 8 月 12 日更新
@Andrew Wolfe 在评论中有一个很好的观点,询问了 工作区激活事件.随着我在未来的项目中实现类似的配置,我可能会朝这个方向发展.
@Andrew Wolfe had a great point in the comments, asking about workspace activation events. As I implement similar configurations in future projects, I will probably go in that direction.
原答案:
最终使用了@HansPassant 的解决方案:https://code.visualstudio.com/docs/editor/tasks#_custom-tasks
Ended up using @HansPassant's solution: https://code.visualstudio.com/docs/editor/tasks#_custom-tasks
在我的 .vscode/tasks.json
文件中有类似的东西:
So something similar to this in my .vscode/tasks.json
file:
{
"version": "2.0.0",
"tasks": [
{
"label": "Launch Ngrok",
"type": "shell",
"command": "ngrok http -subdomain=<SUBDOMAIN> <PORT>",
"windows": {
"command": "ngrok http -subdomain=<SUBDOMAIN> <PORT>"
},
"group": "none",
"presentation": {
"reveal": "always",
"panel": "new"
}
},
{
"label": "Launch App",
"type": "shell",
"command": "npm start",
"windows": {
"command": "npm start"
},
"group": "none",
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
我还将我的项目保存为一个命名的工作区,以便我可以在循环浏览打开的项目时快速确定我正在查看哪个项目.
I also saved my project as a named Workspace, so that I can quickly identify which project I am looking at as I cycle through open projects.
在 .vscode
中为每个项目添加了一个类似的 tasks.json
文件,然后每次我想工作时只需使用命令面板启动每个任务东西.
Added a similar tasks.json
file in .vscode
for every project, and then just use the command palette to kick off each task every time I want to work on something.
每个应用程序对需要同时运行的依赖项有不同的要求,有些通过本地节点启动,有些我在远程服务器上启动,有些我需要始终运行 TypeScript,然后 Rsync 到远程开发服务器(并将 Rsync 任务绑定到 cmd+option+s 以进行快速更新,更多信息).
Each app has different requirements for dependencies which need to be running simultaneously, some start via node locally, some I am starting on a remote server, some I need to have TypeScript always running, and then Rsync to the remote dev server (and bound the Rsync task to cmd+option+s for quick updates, more here).
这个解决方案让上述所有事情变得轻而易举,让我不必每次都记住让每个项目运行的魔法咒语.
This solution makes all of the above a breeze, and saves me from having to remember the magic incantation to get each project running every time.
这篇关于在 VS Code 中打开项目时启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!