Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

Can I open a folder in VS Code via the CLI WITH a command to be run on the intergrated terminal?

When I open a folder from my native terminal, with code ./workspace I'd like to add parameters to tell that it should straight-up run a specific command on an integrated terminal inside VS Code itself.

Like code ./workspace/ --npm build or code ./workspace/ && code windowindex=0 --t --npm build?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The VS Code CLI does not support this AFAIK.

My current workaround is to use code to open a workspace normally, then use extensions that trigger on VS Code startup (when the window opens) to create my terminals and runs commands on them.

There are a couple of these extensions, but personally I use Terminals Manager.

  1. Create and save a workspace (ex. myapp.code-workspace)

  2. On your workspace, create a .vscode/terminals.json file

    enter image description here

  3. Define your terminals in the terminals.json file (refer to the extension's page for the syntax). For example, let's say I want to have 1 terminal that will auto-run npm run start and 1 terminal that does git fetch:

    {
        "autorun": true,
        "terminals": [
            {
                "name": "NPM",
                "description": "For NPM commands",
                "focus": true,
                "commands": [
                    "cd myapp",
                    "npm run start"
                ]
            },
            {
                "name": "GIT",
                "description": "For GIT commands",
                "focus": false,
                "command": "git fetch -v"
            }
        ]
    }
    
  4. When you do code myapp.code-workspace that should open VS Code and open the defined terminals (with the focus on the terminal with focus: true:

    enter image description here

I like this better even if there's a code --option to do this, because if I open VS Code "manually" (i.e. by clicking its icon from the dock), then it will also do the same thing. I won't need a separate config/setup.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...