Dopynose
Dopynose

Reputation: 21

How to automate Visual Studio Code startup?

I need to setup my development environment multiple times a day, I want to automate the process to a one-click solution.

The goal is to having a main script which opens up two VS Code instances, one for the frontend, one for the backend project.

The steps should be the following:

- open VS Code
- open Backend Project (located at e.g.: C:/myCompany/backend)
- run git pull
- open terminal
- run docker-compose up
- open split terminal
- run npm run start:dev
- open another vscode
- open terminal
- git pull
- open terminal
- run npm run start:dev

I am running windows, I can create very basic ps1 files, I know you can use terminal and run 'code' command to start an instance of VS Code. After that I don't find information what to do next. I know there some kind of scripts you can run in Vs Code too, but I cannot put it all together.

Upvotes: 2

Views: 2110

Answers (1)

Kamen Minkov
Kamen Minkov

Reputation: 3710

There's a certain level of automation that can be achieved with VSCode's tooling itself.

Let's start with the backend part of the project. Inside C:/myCompany/backend create a folder .vscode and inside of it place two files: settings.json and tasks.json. They should be as follows:

// C:/myCompany/backend/.vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "git pull",
      "type": "shell",
      "command": "git pull",
      "problemMatcher": [],
      "runOptions": {
        "runOn": "folderOpen"
      }
    },
    {
      "label": "docker-compose up",
      "command": "docker-compose up",
      "type": "shell",
      "presentation": {
        "reveal": "always",
        "panel": "dedicated",
        "group": "dev"
      },
      "group": "build",
      "runOptions": {
        "runOn": "folderOpen"
      }
    },
    {
      "label": "start:dev",
      "type": "shell",
      "command": "npm run start:dev",
      "presentation": {
        "panel": "dedicated",
        "group": "dev"
      },
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}
// C:/myCompany/backend/.vscode/settings.json
{
  "task.allowAutomaticTasks": "on"
}

Similarly, under C:/myCompany/frontend create the same .vscode folder and the same two files under it; settings.json would stay the same, but tasks.json would be as follows:

// C:/myCompany/frontend/.vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "git pull",
      "type": "shell",
      "command": "git pull",
      "problemMatcher": [],
      "runOptions": {
        "runOn": "folderOpen"
      }
    },
    {
      "label": "start:dev",
      "type": "shell",
      "command": "npm run start:dev",
      "presentation": {
        "panel": "dedicated",
        "group": "dev"
      },
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}

To finish things up, the powershell script would be as simple as this:

code C:\myCompany\backend
code C:\myCompany\frontend

In previous VSCode versions it was necessary to invoke workbench.action.tasks.manageAutomaticRunning and then to choose Allow Automatic Tasks in Folder once for each folder, but that doesn't seem to be the case any more (the setting in settings.json seems to suffice).

For further customisation (e.g. task execution order and dependency), you can look at the documentation: https://code.visualstudio.com/Docs/editor/tasks. You can also experiment with running git pull right from the powershell script instead of the VSCode tasks.

Upvotes: 2

Related Questions