Task in VS Code aren't limited to what the application can detect
in tools like npm and Gulp.
You can also create your own custom tasks to launch any script
or process you have permission to run on your computer.
It's all done by configuring additional task in your
tasks.json file, like the one here
we customized earlier. To add a custom task,
I could just copy the one already in this file and paste it as a
second element in the task array.
I could then make whatever changes I want to it.
However, I want to show you how to create a custom task
if you don't already have a tasks.json file in your project.
I'll first close the existing file.
I'll then delete it in the file explorer.
Remember that it lives in the .vscode directory.
I'll now open the command palette and search for task.
I'm going to run the configure task command. That auto detects the
two npm scripts I already discussed,
but just below that is an option to create tasks.json file
from template.
Since I no longer have a tasks.json file in my project,
that's what I'm going to select.
The next choice I have to make is which template to use.
You can see there are several for different build environments,
but I'm going to select others at the bottom of the list.
It's the most flexible and lets me effectively configure
whatever I want.
That creates a new tasks.json file in my .vscode directory.
It has a single basic task in the file ready for me to customize.
I'm going to customize it to run a PowerShell command that will show
me all of the VS Code processes running on my computer.
The first thing I'll do is change the label value to be something
more descriptive.
This is the text I'll see when picking the task from a list.
I'll change it to show VS Code processes.
The type property can be set to either shell as you see here
or process. You use shell
if you're going to have the task
execute a shell script and process, if you're going to launch
some other program.
Since I'm going to run a PowerShell command,
I'll leave it set to shell. Before I set the command value,
I want to set the properties that will specifically run PowerShell.
To do that, I'll first add the options element.
It's assigned an object.
That object has a shell property.
It is also assigned an object.
The properties available on it are the path to the shell executable
and an args property for any options you want to pass
to that executable.
I'll set the executable property to PowerShell.
Windows will find it without me providing a full path,
but if I were using a different shell,
I would provide the full path to the executable.
I don't need to pass any arguments to it.
I've now configured the task to use PowerShell.
The next step is to specify the command execute in the shell.
I do that with the cleverly named command property below.
I'll have it run the PowerShell commandlet
"Get-Process."
I want to pass an argument to it,
so I'll also add the args property.
The value for this is an array, but I'll just pass it the
single string code, which will filter the list of
processes to just those containing that string.
The last property I'm going to add to this task is named
problemMatcher.
Problem matchers process the output from a task and can present
output from them in the editor or problems panel.
VS Code ships with several problem matchers for TypeScript,
ESLint, and a few other tools.
You can also create your own using regular expressions to process
the text output by the task.
I'm not too concerned about my task generating errors,
so I'm going to leave the problem matcher array empty.
There are a few other properties you can explore when configuring
your task, but this is a pretty complete
example and all I need to review some basic process information.
Let's now try it out.
I'll run the task from the command palette.
I'll search for task and then select "Run Task."
Show VS Code processes now appears in the list of available task.
I can just press "Enter" to run it.
That starts PowerShell in a new terminal window and runs the
Get-Process command with the code argument I specified on the task.
The output shows me the processes being used by VS Code right now.
Tasks are a great way to run common developer tools and scripts
right inside VS Code.
It's another feature that helps you do more of the things you need
to do without switching to another application or environment.
Okay. The last topic I'm going to cover in the course is debugging
inside VS Code.
If you never have bugs in your code,
then feel free to skip the next chapter.
For everyone else, you should probably keep watching.