-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeno-script.sh
executable file
·134 lines (115 loc) · 2.88 KB
/
deno-script.sh
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
#!/bin/bash
#======================
# Constants declaration
#======================
BUFIO_LIB="'https://deno.land/[email protected]/io/bufio.ts'"
#======================
# Arguments Handler
#======================
permissionPattern="^(--allow(-[a-z]+)+|-A)$"
optionPattern="^(--inline|-i|--pipe|-p|--read-file-line|--read-file|--read-text-line|--read-text)$"
handleArguments() {
argCount=$#
argArray=("$@")
for ((j = 0; j < argCount; j++)); do
if [[ ${argArray[j]} =~ $permissionPattern ]]; then
# good for the deno installer
permissionCatch="${permissionCatch:+$permissionCatch }${argArray[j]}"
fi
if [[ ${argArray[j]} =~ $optionPattern ]]; then
optionDetected=1
fi
if [[ $optionDetected ]]; then
tailedArg+=("${argArray[j]}")
fi
done
tailedArgArray=("${tailedArg[@]}")
}
#======================
# Functionalities
#======================
readText() {
deno run $permissionCatch <(echo "
const decoder = new TextDecoder('utf-8');
const lines = Deno.args[0].split('\n');
$2
") "$3"
}
readTextLine() {
deno run $permissionCatch <(echo "
import {readLines} from $BUFIO_LIB;
const encoder = new TextEncoder()
const data = encoder.encode(Deno.args[0])
const reader = new Deno.Buffer(data);
for await (const line of readLines(reader)) {
$2
}
") "$3"
}
readFile() {
# shellcheck disable=SC2028
deno run --allow-read $permissionCatch <(echo "
const decoder = new TextDecoder('utf-8');
const data = await Deno.readFile(Deno.args[0]);
const lines = decoder.decode(data).split('\n');
$2
") "$3"
}
readFileLine() {
deno run --allow-read $permissionCatch <(echo "
import {readLines} from $BUFIO_LIB;
const data = await Deno.readFile(Deno.args[0]);
const reader = new Deno.Buffer(data);
for await (const line of readLines(reader)) {
$2
}
") "$3"
}
inline() {
deno run $permissionCatch <(echo "$2") "${@:3}"
}
pipe() {
deno run $permissionCatch <(echo "
import {readLines} from $BUFIO_LIB;
for await (const line of readLines(Deno.stdin)) {
$1
}
")
}
#======================
# Options handler
#======================
handleOptions() {
if [[ $1 =~ (--inline|-i) ]]; then
inline "$@"
elif [[ $1 =~ (--pipe|-p) ]]; then
pipe "$2"
elif [[ $1 =~ --read-file-line ]]; then
readFileLine "$@"
elif [[ $1 =~ --read-file ]]; then
readFile "$@"
elif [[ $1 =~ --read-text-line ]]; then
readTextLine "$@"
elif [[ $1 =~ --read-text ]]; then
readText "$@"
fi
}
#============================
# Main function
#============================
main() {
handleArguments "$@"
if [[ $optionDetected ]]; then
handleOptions "${tailedArgArray[@]}"
else
deno run "$@"
fi
}
#============================
# Main script
#============================
if [[ $@ =~ --test ]]; then
echo "[INFO] deno-script test mode was activated"
else
main "$@"
fi