Alex McMillan
Alex McMillan

Reputation: 17942

Is it possible to open a new terminal inside VSCode from inside a script?

I want to start 3 servers from a single command.

I have package.json scripts like so:

"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"

and I have a script launch_script.sh like so:

#!/bin/bash

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

but this opens them all in a single terminal window, and they end up tripping all over each other.

I know you can open new terminals from the VSCode GUI, but is it possible to open a new terminal from within one? Or to tell VSCode to open 3 terminals each with a separate command ?

Upvotes: 7

Views: 3049

Answers (1)

DAXaholic
DAXaholic

Reputation: 35328

I think this could be something for 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"]
        }
    ]
}

Compound tasks
You can also compose tasks out of simpler tasks with the dependsOn property. For example, if you have a workspace with a client and server folder and both contain a build script, you can create a task that starts both build scripts in separate terminals. If you list more than one task in the dependsOn property, they are executed in parallel by default.

Also compound launch configurations may be interesting to you as it seems like your scripts are for starting a frontend and backend app.

{
    "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"]
        }
    ]
}

Both are examples from the corresponding docs page but adjusting them to your scripts should be straightforward.

Upvotes: 9

Related Questions