2024-07-23


Restart task using runCommands
It is possible to restart a task using multicommand in VSCode. First, with a user task:
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Install dependencies and start dev",
      "type": "shell",
      "command": "ni && d",
      "runOptions": { "runOn": "folderOpen" },
      "group": {
        "kind": "none",
        "isDefault": true
      },
      "presentation": {
        "reveal": "always",
        "panel": "shared"
      }
    },
  ]
}
This task runs ni and d which uses ni to install dependencies and then d which is an alias for nr dev that runs dev inside package.json. For a guide on how to add aliases see this.
This task runs when opening a folder. Sometimes you may want to rerun this task, for example after changing a package version in package.json and you want to install the new package.
In VSCode open the command palette Preferences: Open keyboard shortcuts (JSON) and paste this:
  {
    "key": "f8",
    "command": "runCommands",
    "args": {
      "commands": [
        {
          "command": "workbench.action.tasks.terminate",
          "args": "terminateAll"
        },
        {
          "command": "workbench.action.tasks.runTask",
          "args": "Install dependencies and start dev"
        }
      ]
    }
  }
Here I chose f8 to run two commands using runCommands: Terminating all tasks first, then running the specified task. It’s important that the args field has the correct name for the task that you specified in the task label.
When that is done you can press F8 at any time to close all tasks, and then run the task that installs dependencies and starts the dev server.
Go back to all posts