Last active
February 19, 2022 01:55
-
-
Save awerlang/8be432083f2bf943ec2113f021fbb961 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
{ | |
"scripts": { | |
"pretest": "exit 1", | |
"test": "exit 1", | |
"posttest": "echo post-test" | |
} | |
} |
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
#!/bin/bash | |
set -o nounset -o noclobber -o pipefail -e | |
get_script() { | |
if [[ $1 == "run" ]]; then | |
shift | |
fi | |
echo "$1" | |
shift | |
} | |
get_cmd() { | |
local cmd=$1 | |
<./package.json jq -r ".scripts[\"$cmd\"] | select (.!=null)" | |
} | |
run_cmd() { | |
local cmd=$1 | |
echo "\$ $cmd" | |
sh -c "$cmd" | |
} | |
run_prepost() { | |
local prefixed | |
prefixed=$1 | |
local cmd | |
cmd=$(get_cmd "$prefixed") | |
if [[ -n "$cmd" ]]; then | |
run_cmd "$cmd" | |
fi | |
} | |
main() { | |
local script | |
script=$(get_script "$@") | |
local cmd | |
cmd=$(get_cmd "$script") | |
if [[ -z "$cmd" ]]; then | |
echo "error Command \"$script\" not found." | grep --color 'error' | |
exit 1 | |
fi | |
run_prepost "pre$script" | |
run_cmd "$cmd" "$@" | |
run_prepost "post$script" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment