Last active
February 26, 2022 18:05
-
-
Save lgersman/8350160ca18000e732b3ea6742d00638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Script will open up fzf to select a monorepo package and show the git commit log for that monorepo package. | |
# (Start the script in your monorepo root directory) | |
# Requires: | |
# - git | |
# - pnpm | |
# - fzf >= 0.29.0 | |
# | |
# Author: Lars Gersmann <[email protected]> | |
# Created: 2022-02-24 | |
set -Eeuo pipefail | |
trap cleanup SIGINT SIGTERM ERR EXIT | |
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) | |
usage() { | |
cat << EOF # remove the space between << and EOF, this is due alue arg1 | |
Script description here. | |
Available options: | |
-h, --help Print this help and exit | |
-v, --verbose Print script debug info | |
-f, --flag Some flag description | |
-p, --param Some param description | |
EOF | |
exit | |
} | |
cleanup() { | |
trap - SIGINT SIGTERM ERR EXIT | |
# script cleanup here | |
} | |
WORKSPACE_PACKAGES=$(pnpm -r exec pwd | xargs realpath --relative-to="$script_dir/..") | |
_package2path() { | |
local package="$1" | |
local path="$(pnpm --filter="$package" exec pwd | xargs realpath --relative-to="$script_dir/..")" | |
echo $path | |
} | |
_command_info() { | |
local package="$1" | |
local path="$(_package2path "$package")" | |
echo "git log for package $package ($path) | |
$(git --no-pager log --color -- $path) | |
" | |
exit 0 | |
} | |
parse_params() { | |
# default values of variables set from params | |
flag=0 | |
param='' | |
while :; do | |
case "${1-}" in | |
-h | --help) usage ;; | |
-v | --verbose) set -x ;; | |
-f | --flag) flag=1 ;; # example flag | |
-p | --param) # example named parameter | |
param="${2-}" | |
shift | |
;; | |
_command_info) | |
shift | |
_command_info "$@" | |
;; | |
-?*) die "Unknown option: $1" ;; | |
*) break ;; | |
esac | |
shift | |
done | |
args=("$@") | |
# check required params and arguments | |
#[[ -z "${param-}" ]] && die "Missing required parameter: param" | |
#[[ ${#args[@]} -eq 0 ]] && die "Missing script arguments" | |
} | |
parse_params "$@" | |
PREVIEW_CMD="'${BASH_SOURCE[0]}' _command_info '{}'" | |
package=$("$script_dir/fzf" \ | |
--reverse \ | |
--no-sort \ | |
--select-1 \ | |
--disabled \ | |
--no-multi \ | |
--border=rounded \ | |
--no-info \ | |
--exit-0 \ | |
--prompt='' \ | |
--header-lines=3 \ | |
--ansi \ | |
--bind 'esc:execute(echo "$1" && exit)' \ | |
--preview-window=80% \ | |
--preview="$PREVIEW_CMD" \ | |
< <(echo " | |
git commits by monorepo package | |
$(pnpm list --recursive '@*/*' --json | jq -r '.[].name | select( . != null )')") | |
) | |
echo "git log --color -- $(_package2path $package)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment