简体   繁体   English

运行多个 NPM 命令 vscode 任务

[英]Run multiple NPM commands vscode task

I'm not very familiar with vscode tasks but I want to automate one tiny thing.我对 vscode 任务不是很熟悉,但我想自动化一件小事。 Everytime I open my editor, I need to run 4 commands:每次打开编辑器时,我都需要运行 4 个命令:

cd client
npm start

cd server
npm run dev

I'm just looking for a way to automate this.我只是在寻找一种自动化的方法。

ANSWER:回答:

There is currently no support for running a task when VS-Code is opened, however;但是,目前不支持在打开 VS-Code 时运行任务; VSCode added support, roughly 2 years ago, for running a task when a directory is opened.大约 2 年前,VSCode 添加了对打开目录时运行任务的支持。



EXAMPLE #1:示例 #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:示例 #2:

runOptions is added as a base property to the task, a real life example looks like this: runOptions作为基本属性添加到任务中,现实生活中的示例如下所示:

// 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 VS Code sources below有关更多信息,请访问下面的官方 VS Code

Sources:资料来源:

* Example 1) VSCode appendix (TASKS) * 示例 1) VSCode 附录 (TASKS)
* Example 2) VSCode's v(1.30) Release Notes * 示例 2) VSCode 的 v(1.30) 发行说明






EDIT 2021 JUNE 17th @ 7:35pm UTC编辑 2021 年 6 月 17 日 @ 下午 7:35 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:如何在一个序列中运行 4 个命令:

So, I know this works in the Ubuntu Shell , and I would expect that it executes fine in Powershell.所以,我知道这在Ubuntu Shell中有效,我希望它在 Powershell 中执行良好。 As I stated earlier, I use Linux 100%, and nothing else.如前所述,我 100% 使用 Linux,仅此而已。 If Powershell is a true shell, as its name suggest, then it will work...如果 Powershell 是真正的 shell,正如它的名字所暗示的那样,那么它将工作......

Any-who, give this a go:任何人,给这个 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.您不必 append 最后一个命令的分号。 What the semi-colon does, is it tells the shell where the end of a command is at.分号的作用是告诉 shell 命令结束的位置。 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.当 shell 读取分号后的文本时,它现在知道它正在读取一个新命令,并将继续将该新命令解释为一个单独的命令,该命令在下一个分号处结束。 The process continues recursively... you can enter 100 commands in one line this way.该过程以递归方式继续……您可以通过这种方式在一行中输入 100 个命令。


EXAMPLE: 4 Commands in Sequence示例:4 个顺序命令
    ~/$ 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.您正在尝试在 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.如果终端运行一个没有结束的程序,比如服务器,在服务器完成运行之前,终端将无法启动另一个程序,因此您需要打开另一个终端来运行您的客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM