运行多个 NPM 命令 vscode 任务

时间:2021-06-16 07:41:33

标签: visual-studio-code automation task

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

cd client
npm start

cd server
npm run dev

我只是在寻找一种方法来自动执行此操作。

1 个答案:

答案 0 :(得分:2)

答案:

<块引用>
目前不支持在打开 VS-Code 时运行任务; VSCode 大约在 2 年前添加了对在打开目录时运行任务的支持。



示例 #1:

<块引用>
这是配置属性的样子:

// 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 作为基本属性添加到任务中,真实示例如下所示:

// 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.代码来源如下
<块引用>

来源:

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





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



如何按顺序运行 4 个命令:

所以,我知道这在 Ubuntu Shell 中有效,我希望它在 Powershell 中运行良好。正如我之前所说,我 100% 使用 Linux,没有别的。如果 Powershell 是一个真正的 shell,正如它的名字所暗示的那样,那么它就可以工作......

任何人,试一试:

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


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



我可能有坏消息要告诉你

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