Task Definitions for VsCode for local environments

Shehzad AliShehzad Ali
3 min read

I am currently studying in the AWS CloudProject Bootcamp run by Andrew Brown. To simplify development and create a standard environment we are using Gitpod which uses the concept of CDE (Cloud Development Environments).

It has lots of nice features and quality-of-life improvements as everything required for a development environment is preconfigured or can be installed. One of these features is automating the preparation and building of a GitPod project using .gitpod.yml

Problem

The downside of GitPod though is that it can be expensive to run. To avoid this I use my local VsCode environment. The problem though is that on launch I had to manually run tasks to prepare and build the environment. I was using scripts to do this but it was inconvenient.

Solution

VSCode has the ability to automate tasks using its tasks feature. I wanted to use this to automate the installation of npm packages in the frontend-react-js directory.

VSCode stores workspace-specific settings and configurations in the following folder
.vscode in the root of the repository.

We will use this file to automate the installation of npm packages

Task Configuration

Create tasks.json inside the .vscode folder. It should look like this in VSCode now.

Paste the following code to install the npm packages in the frontend-react-js folder.

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "install",
            "path": "frontend-react-js",
            "group": "build",
            "problemMatcher": [],
            "label": "npm: install - frontend-react-js",
            "detail": "install dependencies from package",
            "presentation": {
                "reveal": "always",
                "group": "develop",
                "panel": "new"
            },
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
}

This is an explanation of the settings used in the task definition

KeyExplanation
typeThe type of the task e.g. cmd, shell, Powershell
scriptThe command to pass to npm
pathThe folder to run the task in
groupDefines the group to which the task belongs e.g. build,test
labelOptional label
detailOptional
presentationSets the presentationOptions for this task's panel
revealControls whether the task output is reveal in the user interface, default is always
groupWhich group this panel belongs to
panelControls if the task panel is used for this task only (dedicated), shared between tasks (shared), or if a new panel is created on * every task execution (new). Defaults to shared
runOptionsDefines when and how a task is run
runOnSpecifies when a task is run. In this instance, we are running the task when the containing folder is opened.

Testing The Task

We can test this task through Quick Open Ctrl+P by typing 'task', Space, and the command name. In this case, 'install frontend-react-js and then pressing enter.

You can also run the task from the Command Palette using CTRL+SHIFT+P

Tasks: Run Task -> npm: install - frontend-react-js

Successful Run

If run successfully you will see the following output. The task has now been successfully configured.

From now this task will automatically run whenever the folder is opened or VsCode is started.

I hope you find this explanation of how to use VSCode tasks useful.

References

1
Subscribe to my newsletter

Read articles from Shehzad Ali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shehzad Ali
Shehzad Ali