Skip to content

Instantly share code, notes, and snippets.

@griffgruff
Created April 16, 2024 13:33
Show Gist options
  • Save griffgruff/ae0188cb7bbff341dd378a4593d468e5 to your computer and use it in GitHub Desktop.
Save griffgruff/ae0188cb7bbff341dd378a4593d468e5 to your computer and use it in GitHub Desktop.

Revisions

  1. griffgruff created this gist Apr 16, 2024.
    32 changes: 32 additions & 0 deletions go.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/usr/bin/env bash

    # Getting tired of not knowing which npm command to run
    # Well this is the answer
    # It does use fzf.
    # First off, this script allow us to just type : run
    # Then you can just filter by command
    # I was using ntl, but it is/was super slow.

    # jq is another thing you might have to install
    # jq is a command line json processor
    # jq .script will give use the scripts section
    # sed then does some search and replace magic
    # It's then all just piped to fzf

    # Read the package.json file
    if cat package.json > /dev/null 2>&1; then

    scripts=$(cat package.json | jq .scripts | sed '1d;$d' | fzf --height 40%)

    if [[ -n $scripts ]]; then
    script_name=$(echo $scripts | awk -F ': ' '{gsub(/"/, "", $1); print $1}')
    echo -s "npm run "$script_name;
    npm run $script_name
    else
    echo "Exit: You haven't selected any script"
    fi

    else
    # Error if package, json not found
    echo "Error: There's no package.json"
    fi