LoginSignup
51
39

More than 5 years have passed since last update.

pecoでnpm-scriptsを超簡単に実行したい

Last updated at Posted at 2018-05-28

npm scriptsの数が増えると、あのコマンドはなんだっけ?と package.json を開いて確認することも多くなると思います。

その作業がわずらわしくなったのでpecoを使って改善してみました。

前提

以下がインストールされている必要があります。

package.jsonをjqを使って整形しているため、別途インストールしています。

インストール

macOSでhomebrewを使用している場合は、以下でインストールできます。

$ brew install peco jq

設定

.bash_profile に以下のコードを貼り付けます。

.bash_profile
alias nrun='npm run $(commands | peco)'

function commands () {
  cat package.json | jq -r '.scripts | keys[]'
}

alias名はお好みのものを指定してください。

.bash_profileの保存後、 以下のように再読込してください。

$ source ~/.bash_profile # もしくは exec $SHELL -l

使い方

コマンドライン上で package.json ファイルの配置ディレクトリにて、 aliasに指定したコマンドを入力してください。

npm-scriptsのコマンドが一覧表示されるので、インクリメンタルサーチで絞り込みを行い、目的のコマンドを選択してEnterを押すと実行できます。

peco-nrun.gif

51
39
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
39