-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-piped.ts
executable file
·142 lines (120 loc) · 4.17 KB
/
example-piped.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
/* 2>/dev/null
DENO_VERSION_RANGE="^1.42.0"
DENO_RUN_ARGS=""
# DENO_RUN_ARGS="--quiet --allow-all --unstable" # <-- depending on what you need
set -e
has_command() {
[ -x "$(command -v "$1" 2>&1)" ]
}
needs_sudo() {
[ "$(id -u)" != 0 ]
}
get_package_install_command() {
package_name="$1"
# shellcheck disable=SC2015
any_sudo="$(needs_sudo && echo sudo || true)"
if has_command brew; then
echo "brew install ${package_name}"
elif has_command apt; then
echo "(${any_sudo} apt update && ${any_sudo} DEBIAN_FRONTEND=noninteractive apt install -y ${package_name})"
elif has_command yum; then
echo "${any_sudo} yum install -y ${package_name}"
elif has_command pacman; then
echo "${any_sudo} pacman -yS --noconfirm ${package_name}"
elif has_command opkg-install; then
echo "${any_sudo} opkg-install ${package_name}"
fi
}
install_package() {
package_name="$1"
installer="$(get_package_install_command "${package_name}")"
if [ -z "${installer}" ]; then
echo "Please install '${package_name}' manually, then try again." >&2
exit 1
fi
eval "saved_opts=\"\$(set +o)\"; set -x; ${installer} >&2; set +x; eval \"\${saved_opts}\"" >&2
}
ensure_command_installed() {
if ! has_command "$1"; then
install_package "$1"
fi
}
uri_encode() {
str="$1"
len=$(printf "%s" "$str" | wc -c)
for i in $(seq 1 $len); do
char=$(printf "%s" "$str" | cut -c $i)
printf '%%%02X' "'$char"
done
}
does_deno_work() {
deno="$1"
[ -n "${deno}" ] && "${deno}" --version >/dev/null 2>&1
}
DENO_VERSION_RANGE_URL_ENCODED="$(uri_encode "${DENO_VERSION_RANGE}")"
DEFAULT_DENO="$(does_deno_work "$(command -v deno)" ||:)"
get_tmp_dir() {
tmp_dir="$(
# for each tmpfs filesystem, sort by available space. for each line, read available space and target mount point, filtering out any trailing whitespace from each variable. if the available bytes is at least 150000000, check if the mount point is a directory. if so, use it.
if has_command findmnt; then
findmnt -Ononoexec,noro -ttmpfs -nboAVAIL,TARGET | sort -rn | \
while IFS=$'\n\t ' read -r avail target; do
if [ "${avail}" -ge 150000000 ] && [ -d "${target}" ]; then
printf "%s" "${target}"
break
fi
done
fi)"
printf "%s" "${tmp_dir:-"${TMPDIR:-/tmp}"}"
}
is_run_from_file(){
line2="/* 2>/dev/null"
me="$(command -v "$0" || true)"
! [ -z $me ] \
&& [ -r $me ] \
&& [ "$(head -c 3 "$me")" = '#!/' ] \
&& (read x && read y && [ "$x" = "#!/bin/sh" ] && [ "$line2" != "${y%"$line2"*}" ]) < "${me}"
}
does_deno_on_path_satisfy() {
deno eval "import{satisfies as e}from'https://deno.land/x/semver@v1.4.1/mod.ts';Deno.exit(e(Deno.version.deno,'${DENO_VERSION_RANGE}')?0:1);" >/dev/null 2>&1
}
get_satisfying_version() {
ensure_command_installed curl
curl -sSfL "https://semver-version.deno.dev/api/github/denoland/deno/${DENO_VERSION_RANGE_URL_ENCODED}"
}
ensure_deno_installed_and_first_on_path() {
DENO_RANGE_DIR="$(get_tmp_dir)/deno-range-${DENO_VERSION_RANGE}/bin"
mkdir -p "${DENO_RANGE_DIR}"
export PATH="${DENO_RANGE_DIR}:${PATH}"
does_deno_on_path_satisfy && return
DENO_VERSION="$(get_satisfying_version)"
DENO_INSTALL="$(get_tmp_dir)/deno-${DENO_VERSION}"
ln -fs "${DENO_INSTALL}/bin/deno" "${DENO_RANGE_DIR}/deno"
does_deno_on_path_satisfy && return
ensure_command_installed unzip
ensure_command_installed curl
export DENO_INSTALL
(
if [ "${DENO_RUN_ARGS#*-q}" != "${DENO_RUN_ARGS}" ]; then
exec 2>/dev/null
fi
curl -fsSL https://deno.land/install.sh | sh -s ${DENO_INSTALL_ARGS} "${DENO_VERSION}" | grep -iv discord >&2
)
}
ensure_deno_installed_and_first_on_path
is_run_from_file && exec deno run ${DENO_RUN_ARGS} "$0" "$@"
exec deno run ${DENO_RUN_ARGS} - "$@" <<'//🔚'
//*/
import { readAll } from "https://deno.land/std@0.221.0/io/read_all.ts";
console.log(
`This 🦕 is deno ${Deno.version.deno}, called with args:\n${
JSON.stringify(Deno.args, null, 2)
}`,
);
if (Deno.stdin.isTerminal()) {
console.log("Type some text, then on a separate empty line, press ctrl+d.");
}
const stdin = new TextDecoder().decode(await readAll(Deno.stdin));
console.log(JSON.stringify({ stdin }, null, 2));
//🔚