概要
こちらの記事を参考にして、Firebase FunctionsをVSCodeで開発している時に、
デバッグできるようにしたのですが、デバッグに行き着くまでのコマンドが多いので、
PowerShell一発実行でできるようにしてみたという話です。
※ Windows限定です。
コード
functions/debug.ps1
$ErrorActionPreference = "silentlycontinue"
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
# 起動中のエミュレータを破棄
functions delete helloWorld
functions kill
# ビルド実行
npm run build
# エミュレータとデバッガ起動
functions start
functions deploy helloWorld --trigger-http
functions inspect helloWorld
# VSCodeのウィンドウをアクティブにする。
$codeProcesses = [System.Diagnostics.Process]::GetProcessesByName('code')
foreach ($process in $codeProcesses) {
[Microsoft.VisualBasic.Interaction]::AppActivate($process.Id) > $null
}
# ここでF5キーを送信してVSCodeのデバッガと接続
[void][System.Windows.Forms.SendKeys]::SendWait('{F5}')
# callしたいfunctionを呼出す。
functions call helloWorld
ポイント
VSCodeを開いた状態で、F5キー送信してデバッグ開始するとこまでをPowerShellで実現したかったんですが、
以下が実行されたタイミングでnode.jsのプロセスが立ち上がり、VSCodeのウィンドウは非アクティブになってしまいます。
その状態でF5キーを送信しても、node.jsのプロセスに対してF5キーを押す事になるのでデバッガが起動しません。
functions inspect helloWorld
そこで、以下のコードで無理やりVSCodeのウィンドウをアクティブ化します。
Microsoft.VisualBasic
とか使ってるあたりに闇を感じますね。
# VSCodeのウィンドウをアクティブにする。
$codeProcesses = [System.Diagnostics.Process]::GetProcessesByName('code')
foreach ($process in $codeProcesses) {
[Microsoft.VisualBasic.Interaction]::AppActivate($process.Id) > $null
}
補足
ちなみに、F5キー送信時は[void]
不要なんですが、
QiitaのMarkdownの記法のせいなのか、付けないと行ごと表示されなかったです。
なんか::
がダメっぽいですね。
# ここでF5キーを送信してVSCodeのデバッガと接続
[void][System.Windows.Forms.SendKeys]::SendWait('{F5}')
最後に
VSCode複数起動してても行けるのかな?