[VSCode] Launch dev environment automatically
Tags: vscode, automation, shell, bash
Published: 2022-05-08 14:00:44

---
title: [VSCode] Launch dev environment automatically
published: true
description: Introducing how to launch dev environment automatically when you open a project in VSCode!
tags: vscode,automation,shell,bash
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2plz0aps0jogl3k08dsn.png
--- 

## [VSCode] Launch dev environment automatically

This is quite sudden, but there are two kinds of developers in the world, keeping the development environment up always and an engineer who drops it every time.

For developers who drop every time, including me, it is a pain to have to open multiple terminals and type a lot of commands in each one every time.

So in this article, I’m introducing **how to launch the dev environment automatically when you open a project in VSCode!**

If you even set up Alfred, which I'll show you as a bonus, you can launch the development environment with a single command, and it will look something like this!

![gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7ax1r5ox1fepaozagm8.gif)

Let’s go!

---

## Writer’s VSCode and OS version

- VSCode: 1.66.0
- macOS Monterey

---

## Hello Custom Task

To automatically execute commands when a project is opened in VSCode, you can use [Custom tasks](https://code.visualstudio.com/docs/editor/tasks#_custom-tasks).

**Practice makes perfect.**

First, let's define a task that displays  `Hello Custom Task` in your terminal.

## Define the task

1. Make an empty directory and create a `.vscode/tasks.json` in it.

```shell
$ mkdir ~/hellovscode-tasks                    
$ mkdir ~/hellovscode-tasks/.vscode            
$ touch ~/hellovscode-tasks/.vscode/tasks.json
```

2. Open the project(You need to install [code](https://code.visualstudio.com/docs/setup/mac) command)

```shell
code ~/hellovscode-tasks
```

3. Define a custom task, edit the `.vscode/tasks.json` created in step 1 as follows

```json
{
  "version": "2.0.0",
  "tasks": [
    {
      // Display name
      "label": "Hello Custom Task",
      // Type of task。shell or process
      "type": "shell",
　　　 // 🌟 Command to execute
      "command": "echo Hello Custom Task", 
      // Write over default properties when executed in windows
      "windows": {
        "command": "echo Hello Custom Task(Windows)"
      },
　　　 // Group of task belongs to。test、build、none
      "group": "none",
　　　 // Specify how to display execution results
      "presentation": {
        "reveal": "always",
        "panel": "new"
      },
      "runOptions": {
	// 🌟 When the command execlutes
        "runOn": "folderOpen"
      }
    }
  ]
}
```

The important part is the part marked with 🌟.

- `"command": "echo Hello Custom Task"` : Command to execute
- `"runOn": "folderOpen"` : Runs as soon as the project is opened

You can see more detail in the schema of [tasks. json](https://code.visualstudio.com/docs/editor/tasks-appendix#_schema-for-tasksjso)

Then, **I thought this would run if I reopened the folder, but it does not.**

This is the big pitfall. 
**To work `"runOn": "folderOpen"`, you need to allow execution by running command manually.**

### Allow execution of tasks

1. Select `Cmd+P` > `Tasks: Run Task`
![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oigdo3k6xfg3o9dp63hk.png)
 

2. Select the `HelloCustomTask` you defined.
![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z17vc84bbvpzo669539.png)

3. Then a toast will appear in the lower right corner of the screen asking if you want to allow automatic execution when the folder is opened.
![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36uwejhw9ei36nru25te.png)
 

That's it, you've defined your task!

Let’s check it woking by close and reopening the project!

---

## Try it out on a real project

Now that you understand the basics of custom tasks, let's try them out on a real project.

This time, I will use the development environment of [Nitte](https://site.nitte.app/), a service scheduling system that I am personally developing, as an example.

Please replace the command with your own development environment!

### Define tasks for each command

The following two commands must be executed when launching my development environment.

- yarn run serve: start server
- yarn ngrok: start ngrok

So, I first define tasks to execute these two commands in `.vscode/tasks.json.`

```json
{
  "version": "2.0.0",
  "tasks": [
    // yarn serve command
    {
      "label": "yarn serve",
      "type": "shell",
      "command": "yarn serve",
      "group": "none",
      "presentation": {
        "reveal": "always",
        "panel": "new"
      }
    },
    // yarn ngrok command
    {
      "label": "yarn ngrok",
      "type": "shell",
      "command": "yarn ngrok",
      "group": "none",
      "presentation": {
        "reveal": "always",
        "panel": "new"
      }
    }
  ]
}
```

### Combine multiple tasks into one task using dependsOn

Then, define a task that combines the tasks you have created into a single task.

To combine multiple tasks into one, use the `dependsOn` property.
Multiple tasks can be grouped together by describing the labels of the tasks to be grouped as an array.

```json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "yarn serve",
      ...
    },
    {
　　　 "label": "yarn ngrok",
      ...
    },
　　　// To combine multiple tasks
    {
      "label": "dev",
      "type": "shell",
      // Describe the labels of the tasks to be grouped as an array.
      "dependsOn": [
        "yarn serve",
        "yarn ngrok"
      ],
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}
```

💡 By default, tasks written in "dependsOn" are executed in parallel. 
To specify the order of execution, specify "dependsOrder": "sequence".💡 

Then, as before, allow execution once from `Tasks: Run Task.`

Let’s check it working, It is OK if all defined tasks are executed and the development environment is launched by simply opening the folder.

---

## (Bonus) Let's connect with Alfred to make it even easier.

It's already a lot easier, but for me, it is still a hassle to open a terminal and type every time.

```tsx
code path/to/project
```

So, we use [Alfred Workflows](https://www.alfredapp.com/workflows/)  to make it easier.

⚠️Workflowを使うにはPowerPack(有料)が必要です。⚠️

1. On the Workflows tab in Alfred, select + > Blank Workflow at the bottom right of the screen

![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4pme7u7bnxqctb984lv.png)
 

2. Input name and select `Create`
![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe70rh8j66vrj401pzzr.png)
 

3. In the Workflow view, right-click > Inputs > Keyword
![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gna48etmbpzn8n7rysn.png)
 

4. Enter a keyword to call Workflow. Change to `No Argument` and select `Save` 
![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cy3wqmrdm59d8s5oeo7.png)
 

5. Select the projection coming out of the keyword node > Actions > RunScript
![5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bn5di7sqn12lkd8payq0.png)
 
6. Write a script to open the VSCode project and Save.

```shell
# Backend
/usr/local/bin/code /Users/matsumoto.kazuya/ghq/github.com/WombatTechnology/nitte-backend/functions
# Frontend
/usr/local/bin/code /Users/matsumoto.kazuya/ghq/github.com/WombatTechnology/nitte-frontend
# Open Browser
/usr/bin/open -a "/Applications/Google Chrome.app" 'http://localhost:8080'
```

💡When I ran bash from Alfred, for some reason there is no path, so I ran it with the full path /usr/local/bin/code instead of code💡 

Now everything is up and running in the development environment with a single command, just like in the video at the beginning!
We did it 🎉