我对 vscode 任务不是很熟悉,但我想自动化一件小事。每次打开编辑器,我都需要运行 4 个命令:
cd client
npm start
cd server
npm run dev
我只是在寻找一种方法来自动执行此操作。
答案 0 :(得分:2)
// 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;
}
}
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"
}
}
所以,我知道这在 Ubuntu Shell 中有效,我希望它在 Powershell 中运行良好。正如我之前所说,我 100% 使用 Linux,没有别的。如果 Powershell 是一个真正的 shell,正如它的名字所暗示的那样,那么它就可以工作......
在命令行中键入每个命令,并在每个命令的末尾附加一个 SEMI-COLON (;
)。您不必在最后一个命令后附加分号。分号的作用是告诉 shell 命令结束的位置。当 shell 读取分号后的文本时,它现在知道它正在读取一个新命令,并且它将继续将该新命令解释为以下一个分号结尾的单个单独命令。该过程以递归方式继续……您可以通过这种方式在一行中输入 100 个命令。
~/$ cd client; npm start; cd server; npm run dev
您正在尝试一次性启动服务器和客户端。我猜他们不能同时使用同一个终端来启动和运行他们的实例。如果终端运行一个没有结束的程序,比如服务器,在服务器运行完成之前终端将无法启动另一个程序,因此您需要打开另一个终端来运行您的客户端。