Start app when opening project in VS Code?

Add this into your .vscode/tasks.json in a project folder to run a script or execute a command when you open the folder in VS code:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Launch app", // Name of task
      "type": "shell",
      "command": "npm start", // Enter your command here
      "group": "none",
      "presentation": {
        "reveal": "always",
        "panel": "new"
      },
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}

Then you need to enable automatic tasks:

  1. Press CTRL + shift + P and type > Tasks: Manage Automatic Tasks in Folder
  2. Hit Enter then choose "Allow Automatic Tasks in Folder".

Your task will execute next time you open VS Code in that folder.


Update August 12, 2019

@Andrew Wolfe had a great point in the comments, asking about workspace activation events. As I implement similar configurations in future projects, I will probably go in that direction.

Original answer:

Ended up using @HansPassant's solution: https://code.visualstudio.com/docs/editor/tasks#_custom-tasks

So something similar to this in my .vscode/tasks.json file:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Launch Ngrok",
      "type": "shell",
      "command": "ngrok http -subdomain=<SUBDOMAIN> <PORT>",
      "windows": {
        "command": "ngrok http -subdomain=<SUBDOMAIN> <PORT>"
      },
      "group": "none",
      "presentation": {
        "reveal": "always",
        "panel": "new"
      }
    },
    {
      "label": "Launch App",
      "type": "shell",
      "command": "npm start",
      "windows": {
        "command": "npm start"
      },
      "group": "none",
      "presentation": {
        "reveal": "always",
        "panel": "new"
      }
    }
  ]
}

I also saved my project as a named Workspace, so that I can quickly identify which project I am looking at as I cycle through open projects.

Added a similar tasks.json file in .vscode for every project, and then just use the command palette to kick off each task every time I want to work on something.

Each app has different requirements for dependencies which need to be running simultaneously, some start via node locally, some I am starting on a remote server, some I need to have TypeScript always running, and then Rsync to the remote dev server (and bound the Rsync task to cmd+option+s for quick updates, more here).

This solution makes all of the above a breeze, and saves me from having to remember the magic incantation to get each project running every time.