Monorepo for @ducky.ws's experiments and scripts ducky.ws
at main 86 lines 2.1 kB view raw
1#!/usr/bin/env bash 2 3_me_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 4_runtime_dir="$_me_dir/runtimes" 5_platform="$1" 6_script="$2" 7 8set -o errtrace 9 10function check_path_exists() { 11 local path="$1" 12 local path_rel="./$(realpath --relative-to="$_me_dir" "$path")" 13 14 if [[ "$path" == *"/" ]]; then 15 [[ ! -d "$path" ]] && die "Path '$path_rel' does not exist" 16 else 17 [[ ! -f "$path" ]] && die "Path '$path_rel' does not exist" 18 fi 19} 20 21function check_prog() { 22 local prog="$1" 23 local asdf_plugin="$2" 24 25 local is_asdf_installed="$([ -x "$(command -v "asdf")" ] && echo 1 || echo 0)" 26 local is_prog_installed="$([ -x "$(command -v "$prog")" ] && echo 1 || echo 0)" 27 [[ -z "$asdf_plugin" ]] && asdf_plugin="$prog" 28 29 if [[ $is_prog_installed == 0 ]]; then 30 if [[ $is_asdf_installed == 1 ]]; then 31 asdf plugin add $asdf_plugin 32 asdf install $asdf_plugin latest 33 [[ $? != 0 ]] && die "Unable to install '$prog'" 34 asdf set -u $asdf_plugin "$(asdf latest $asdf_plugin)" 35 echo "---" >&1 36 else 37 die "'asdf' not installed, cannot install '$prog'" 38 fi 39 else 40 return 1 41 fi 42} 43 44function die() { 45 echo -e "\033[1;31mError: $1\033[0m" >&2 46 exit 225 47} 48 49function get_code_path() { 50 local platform="$1" 51 local script="$2" 52 local suffix="$3" 53 54 echo "$_me_dir/code/$platform/$script$suffix" 55} 56 57function run_bash() { 58 local path="$(get_code_path "bash" "$1" ".sh")" 59 local args=("$@") 60 check_path_exists "$path" 61 62 bash "$path" "${args[@]}" 63} 64 65function run_deno() { 66 local path="$(get_code_path "deno" "$1" ".ts")" 67 check_path_exists "$path" 68 check_prog "deno" 69 70 deno run --unstable-cron -A "$path" 71} 72 73function run_html() { 74 local path="$(get_code_path "html" "$1" "/")" 75 check_path_exists "$path" 76 check_prog "caddy" 77 78 caddy file-server --browse --listen 0.0.0.0:8080 -root "$path" 79 #xdg-open "http://0.0.0.0:8080" 80} 81 82case "$_platform" in 83 "bash") run_bash "$_script" "$@" ;; 84 "deno") run_deno "$_script" ;; 85 "html") run_html "$_script" ;; 86esac