grepath
よく個人的に利用するコマンドをOSSとして開発しちゃおうという魂胆の記事です。
パスだけを抜き出す
私は出力の中からエラーが発生しているファイルを抜き出してをエディター(Vim)で開いて修正するということを多用します。
例えば、以下のようなエラーメッセージが存在するとします。
❯ bun tsc --noEmitsrc/domains/test/components/TestCard.tsx:134:11 - error TS2322: Type 'boolean' is not assignable to type 'number'.134 isRemovable={isRemovable}~~~~~~~~~~~src/domains/test/components/TestCard.tsx:22:322 isRemovable: number;~~~~~~~~~~~The expected type comes from property 'isRemovable' which is declared here on type 'IntrinsicAttributes & Readonly<{ displayName: string }>'src/domains/test/components/TestCard.tsx:93:9 - error TS2322: Type 'number' is not assignable to type 'boolean'.93 isRemovable={isRemovable}~~~~~~~~~~~src/domains/test/components/TestCardContainer.tsx:16:316 isRemovable: boolean;~~~~~~~~~~~The expected type comes from property 'isRemovable' which is declared here on type 'IntrinsicAttributes & Props'Found 2 errors in 2 files.Errors Files1 src/domains/test/components/TestListCard.tsx:1341 src/domains/test/components/TestListCard.tsx:93error: "tsc" exited with code 2
このメッセージを見みて src/domains/test/components/TestListCard.tsx にエラーが発生していることがわかりますので、それを Vim を使って操作したいシチュエーションだとします。
そう言った状況下では以下を叩きます。これは TypeScriptのコンパイルエラーのパスネームだけうまくgrepし、それを重複排除した上でneovimのバッファーでそれぞれのファイルを開くというコマンドです。
bun tsc --noEmit | grep error | grep -E -i '.*\.tsx?' | awk -F '(' '{print "./"$1}' | uniq | xargs -o nvim -p
こういったコマンドは長ったらしいですし、正規表現でマッチさせるにも複雑性や利便性に課題があると思います。
そこで、様々なエラーメッセージに対して簡単にパス名を抜き出すことが できればちょっと役立つかもと思ったのでgrepathを作成してみました
インストール
HomeBrewを利用するか、Shell Scriptでダウンロード可能です
brew install kqito/tap/grepath
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/kqito/grepath/releases/latest/download/grepath-installer.sh | sh
使い方
以下のようにして error.log ファイル内部にあるパスを表示することが可能です
grepath error.log
cat error.log | grepath
先ほどの例では以下のようにして、簡単にエラーが発生しているファイルを開くことが可能です
bun tsc --noEmit | grepath | xargs -o nvim -p
もちろん、VSCodeでも以下のように利用可能です
bun tsc --noEmit | grepath | xargs -o code
さいごに
よかったら grepathを試してみてください!感想やFeature Request等も大歓迎です!