【问题标题】:Is it possible to open a new terminal inside VSCode from inside a script?是否可以从脚本内部在 VSCode 中打开一个新终端?
【发布时间】:2019-12-27 06:49:00
【问题描述】:

我想通过一个命令启动 3 个服务器。

我有 package.json 这样的脚本:

"serve_auth": "cd dev/mock/auth && nodemon --exec babel-node ./server.js --presets @babel/env",
"serve_db": "cd dev/mock/db && nodemon --exec babel-node ./server.js --presets @babel/env",
"start": "react-scripts start",
"develop": "./launch_script.sh"

我有一个脚本launch_script.sh,如下所示:

#!/bin/bash

( yarn serve_db ) & ( yarn serve_auth ) & ( yarn start )

但这会在一个终端窗口中将它们全部打开,最终它们会相互绊倒。

我知道您可以从 VSCode GUI 打开新终端,但是否可以从一个终端中打开新终端?还是告诉 VSCode 用单独的命令打开 3 个终端?

【问题讨论】:

  • 复合任务/启动配置对您有用吗?
  • @DAXaholic 还没有,我仍在努力——如果我能让它工作,肯定会回来接受/投票。问题是,我的服务器是用 ES6 编写的,所以我需要先用 babel 进行转译......这是个问题,因为我在 Win10 机器上的 VSCode 中运行 WSL。

标签: visual-studio-code


【解决方案1】:

我认为这可能适合compound tasks

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Client Build",
            "command": "gulp",
            "args": ["build"],
            "options": {
                "cwd": "${workspaceRoot}/client"
            }
        },
        {
            "label": "Server Build",
            "command": "gulp",
            "args": ["build"],
            "options": {
                "cwd": "${workspaceRoot}/server"
            }
        },
        {
            "label": "Build",
            "dependsOn": ["Client Build", "Server Build"]
        }
    ]
}

复合任务
您还可以使用更简单的任务组合任务 依赖属性。例如,如果您有一个带有 客户端和服务器文件夹都包含构建脚本,您可以 创建在不同终端中启动两个构建脚本的任务。如果 您在 dependsOn 属性中列出了多个任务,它们是 默认并行执行。

另外,compound launch configurations 您可能会感兴趣,因为您的脚本似乎是用于启动前端和后端应用程序。

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Server",
            "program": "${workspaceFolder}/server.js",
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "Client",
            "program": "${workspaceFolder}/client.js",
            "cwd": "${workspaceFolder}"
        }
    ],
    "compounds": [
        {
            "name": "Server/Client",
            "configurations": ["Server", "Client"]
        }
    ]
}

这两个都是相应文档页面中的示例,但将它们调整为您的脚本应该很简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2021-04-19
    • 2019-07-01
    • 2017-12-10
    • 2020-05-14
    • 1970-01-01
    • 2018-03-09
    相关资源
    最近更新 更多