Skip to content

Instantly share code, notes, and snippets.

@awerlang
Last active February 19, 2022 01:55
Show Gist options
  • Save awerlang/8be432083f2bf943ec2113f021fbb961 to your computer and use it in GitHub Desktop.
Save awerlang/8be432083f2bf943ec2113f021fbb961 to your computer and use it in GitHub Desktop.
{
"scripts": {
"pretest": "exit 1",
"test": "exit 1",
"posttest": "echo post-test"
}
}
#!/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