1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok fine. Is there a way to either run a task(by opening command palette and selecting said task) manually? Even that is fine.
The term command palette is yucky to me, but that's okay. Lets me know your using windows. I am not totally sure what you mean by "said task" but you most certainly can run a task as a command from the VSCode Terminal. Useally when it is ran like this, you attach it to a keybinding.
You know you probably could setup a file-watcher, and a Gulp task if you wanted, but you would have to run it as a program in the background for windows. You wouldn't be able to define it in a VSCode "tasks.json" file, and accomplish what you are asking. Often, developers like my self, prefer Linux for reason like this. Creating a program and running it in Linux, that uses a simple file-watcher task, is a simple thing to set up when using Mint/Ubuntu, however, windows makes doing stuff like that harder that it needs to be.
By command palette I meant pressing Command + Shift + P.... I just want to squeeze those 4 commands into one.
@Akash Alrighty Akash, I updated my answer in an attempt to try and help you figure this out. I have a new suggestion for you that is appended to the bottom of my old answer. Read it, try it, then let me know what you think.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.