2024-08-26 12:02:35 +02:00
|
|
|
#!/bin/sh
|
|
|
|
# usage: jsh <function> [arg1] [arg2] ...
|
|
|
|
#
|
|
|
|
# 'jsh prompt question answer' executes: js prompt('question','answer') )
|
|
|
|
|
2024-09-02 15:05:47 +02:00
|
|
|
source /mnt/profile.sh
|
|
|
|
|
2024-08-26 12:02:35 +02:00
|
|
|
to_js(){
|
|
|
|
printf "%s(" "$1"
|
|
|
|
shift
|
|
|
|
for arg in "$@"; do
|
2024-08-27 20:40:05 +02:00
|
|
|
case "$arg" in
|
|
|
|
(*[\.0-9]*)
|
|
|
|
printf '%s,' "$arg"
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
printf '"%s",' "$arg"
|
|
|
|
;;
|
|
|
|
esac
|
2024-08-26 12:02:35 +02:00
|
|
|
done
|
|
|
|
printf ")\n"
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:40:05 +02:00
|
|
|
# run argument as js
|
2024-08-29 17:18:14 +02:00
|
|
|
test -z "$1" || {
|
2024-08-27 20:40:05 +02:00
|
|
|
func=$(to_js "$@")
|
|
|
|
func=${func/,)/)}
|
2024-09-05 19:09:40 +02:00
|
|
|
js "return $func"
|
2024-08-29 17:18:14 +02:00
|
|
|
hook "$@"
|
|
|
|
exit 0
|
2024-08-27 20:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# otherwise start repl
|
2024-08-29 17:18:14 +02:00
|
|
|
echo "jsh> type 'exit' or CTRL-C to quit"
|
|
|
|
echo "jsh> HINT: to run alert('foo') outside this REPL, run 'jsh alert foo'"
|
|
|
|
echo "jsh>"
|
2024-08-27 20:40:05 +02:00
|
|
|
while true; do
|
2024-09-05 19:09:40 +02:00
|
|
|
echo -n -e "\n$(printf "\033[0m")jsh> $(printf "\033[0m")"
|
2024-08-29 17:18:14 +02:00
|
|
|
read line
|
|
|
|
test "$line" = exit && exit
|
|
|
|
js "$line"
|
2024-08-27 20:40:05 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
|