VSCode と Angular で Web アプリを開発していて、デバッグが終了する時に ng serve
を止めるという事がしたかったのだけど、Windows ではいろいろ面倒で諦めて(macOS ではできた)、ならばと実行中のタスクを全て停止する方法を探したらあった。
- Menu -> View -> Command Pallet -> "Open Keyboard Shortcuts"
-
keybindings.json
に次のように記述
keybindings.json
[
{
"key": "ctrl+alt+c",
"command": "workbench.action.tasks.terminate",
"args": "terminateAll"
}
]
ちょっと解説すると、workbench.action.tasks.terminate
は、「強制終了させるタスクを選択して実行する」というコマンドのようだけど、これに terminateAll
という文字列を与えると、何も聞かずに全ての実行中タスクを強制停止してくれる。
このためだけに VSCode のコードを辿った ので間違いないと思う。
キーボードショートカットはお好みで。
参考
- Terminate all tasks · Issue #27041 · microsoft/vscode
- VS CodeでTerminate Running Taskのキーボードショートカットを設定する - Qiita
- vscode debugger - Terminate another task from within a postDebugTask - VS Code - Stack Overflow
おまけ
そしてこの workbench.action.tasks.terminate terminateAll
コマンドを、タスクとして以下のように登録し、
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "ng-serve",
"type": "shell",
"isBackground": true,
"command": "ng serve",
"problemMatcher": {
"owner": "custom",
"pattern": {
"regexp": "^$"
},
"background": {
"activeOnStart": true,
"beginsPattern": "^.*Angular Live Development Server.*$",
"endsPattern": "^.*Compiled successfully.*$"
}
}
},
{
"label": "postdebugKill",
"type": "process",
"command":[
"${command:workbench.action.tasks.terminate}",
"terminateAll",
]
}
]
}
launch.json
で、デバッグ実行 -> 停止したときに「全てのタスクを終了」としたかった。。。
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Debug with Chrome",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}",
"preLaunchTask": "ng-serve",
"postDebugTask": "postdebugKill"
}
]
}
のだけど、この状態でデバッグ停止すると「終了するタスクを選択させるプロンプト」が表示されてしまう。
terminateAll
のパラメータがうまく渡せてないようだけど、よく分からない。。。