Akash
Akash

Reputation: 828

Run multiple NPM commands vscode task

I'm not very familiar with vscode tasks but I want to automate one tiny thing. Everytime I open my editor, I need to run 4 commands:

cd client
npm start

cd server
npm run dev

I'm just looking for a way to automate this.

Upvotes: 1

Views: 1457

Answers (1)

AKUMA no ONI
AKUMA no ONI

Reputation: 11819

ANSWER:

There is currently no support for running a task when VS-Code is opened, however; VSCode added support, roughly 2 years ago, for running a task when a directory is opened.



EXAMPLE #1:

This is what the configuration property looks like:

// THIS EXAMPLE WAS ORIGINALLY AUTHORED @ 
// VSCode appendix (TASKS) - "https://code.visualstudio.com/docs/editor/tasks-appendix" 


 // NOTE: This is written in typescript, and hasn't been compiled (which is probably obvious to most). See below for a real world example:

interface RunOptions {

/** ----------------------------------------------------------------------
 * Controls how variables are evaluated when a task is executed through
 * the Rerun Last Task command.
 * The default is `true`, meaning that variables will be re-evaluated when
 * a task is rerun. When set to `false`, the resolved variable values from
 * the previous run of the task will be used.  
-------------------------------------------------------------------------- */

  reevaluateOnRerun?: boolean;



/** ------------------------------------------------------------------------
 * SPECIFIES WHEN A TASK WILL RUN:
 *
 * VALID VALUES ARE:
 *    "default": The task will only be run when executed through the Run Task command.
 *    "folderOpen": The task will be run when the containing folder is opened.
---------------------------------------------------------------------------*/

  runOn?: string;
}
    }




EXAMPLE #2:

runOptions is added as a base property to the task, a real life example looks like this:

// THIS EXAMPLE WAS ORIGINALLY AUTHORED @ 
// VSCode's v(1.30) Release Notes - "https://code.visualstudio.com/Docs/editor/tasks"

// FILENAME: .../.vscode/tasks.json

{
  "type": "npm",
  "script": "strict-null-check-watch",
  "label": "TS - Strict Null Checks",
  "isBackground": true,
  "problemMatcher": {
    "base": "$tsc-watch",
    "owner": "typescript-strict-null",
    "applyTo": "allDocuments"
  },
  "runOptions": {
    "runOn": "folderOpen"
  }
}




For more information, visit the Official V.S. Code sources below

Sources:

* Example 1)   VSCode appendix (TASKS)
* Example 2)   VSCode's v(1.30) Release Notes






EDIT 2021 JUNE 17th @ 7:35pm UTC
Note: I can explain how to run four commands in a sequence, but I don't know if it will change your situation all that much. Read below, and Ill explain how to run the commands, then I will explain why that is not likely going to work.



How to Run 4 Commands in a Sequence:

          So, I know this works in the Ubuntu Shell, and I would expect that it executes fine in Powershell. As I stated earlier, I use Linux 100%, and nothing else. If Powershell is a true shell, as its name suggest, then it will work...

Any-who, give this a go:

          In the command line type each command with a SEMI-COLON (;) appended to the end each-one. You don't have to append a semi-colon to the last command. What the semi-colon does, is it tells the shell where the end of a command is at. When the shell reads the text after the semi-colon, it now knows that it is reading a new command, and it will continue to interpret that new command as a single separate command that ends at the next semi colon. The process continues recursively... you can enter 100 commands in one line this way.


EXAMPLE: 4 Commands in Sequence
    ~/$ cd client; npm start; cd server; npm run dev



I may have bad news for you

          You are trying to start a server, and a client in one go. I am guessing that they wont be able to both be able to use the same terminal to start, and run, their instances. If the terminal runs a program that doesn't end, like a server, the terminal wont be able to start another program until the server is done running, therefore you will need to open another terminal to run your client in.

Upvotes: 1

Related Questions