运行 多个 NPM 命令 vscode 任务

Run multiple NPM commands vscode task

我不太熟悉 vscode 任务,但我想自动化一件小事。每次我打开我的编辑器,我需要 运行 4 个命令:

cd client
npm start

cd server
npm run dev

我正在寻找一种自动化方法。

答案:

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.



示例 #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;
}
    }




示例 #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"
  }
}




有关详细信息,请访问官方V.S。代码 来源如下

Sources:

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






编辑 2021 年 6 月 17 日 @ 7:35pm UTC
注意:我可以解释如何按顺序 运行 四个命令,但我不知道它是否会改变你的情况。阅读下文,我将解释如何 运行 命令,然后我将解释为什么这不太可能起作用。



如何 运行 序列中的 4 个命令:

所以,我知道这在 Ubuntu Shell 中有效,我希望它在 Powershell 中执行良好。如前所述,我使用 Linux 100%,仅此而已。如果 Powershell 是真实的 shell,正如它的名字所暗示的那样,那么它将起作用...

任何人,试一试:

在命令行中键入每个命令,并在每个命令的末尾附加一个 SEMI-COLON;)-一。您不必在最后一个命令后附加分号。分号的作用是告诉 shell 命令结束的位置。当 shell 读取分号后的文本时,它现在知道它正在读取一个新命令,并且它将继续将该新命令解释为一个单独的命令,该命令在下一个分号处结束。这个过程递归地继续......你可以这样在一行中输入100个命令。


示例:序列中的 4 个命令
    ~/$ cd client; npm start; cd server; npm run dev



我可能有坏消息要告诉你

您正在尝试同时启动服务器和客户端。我猜他们将无法同时使用同一个终端启动,运行,他们的实例。如果终端 运行 是一个没有结束的程序,比如服务器,终端将无法启动另一个程序,直到服务器完成 运行ning,因此您需要打开另一个终端来运行 你的客户在。