-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.sh
More file actions
146 lines (131 loc) · 3.48 KB
/
cli.sh
File metadata and controls
146 lines (131 loc) · 3.48 KB
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
143
144
145
146
# SPDX-License-Identifier: MIT
# Licensed under the MIT License. See LICENSE file in the project root.
# Separator for chaining multiple commands (e.g., cli lint __ build __ test)
CLI_COMMAND_SEPARATOR="__"
function cli {
# Check for multi-command separator
local has_separator=false
for arg in "$@"; do
if [ "$arg" = "$CLI_COMMAND_SEPARATOR" ]; then
has_separator=true
break
fi
done
if [ "$has_separator" = true ]; then
local cmd_args=()
for arg in "$@"; do
if [ "$arg" = "$CLI_COMMAND_SEPARATOR" ]; then
if [ ${#cmd_args[@]} -gt 0 ]; then
_cli_single "${cmd_args[@]}" || return $?
cmd_args=()
fi
else
cmd_args+=("$arg")
fi
done
# Execute final command group
if [ ${#cmd_args[@]} -gt 0 ]; then
_cli_single "${cmd_args[@]}" || return $?
fi
return 0
fi
_cli_single "$@"
}
function _cli_single {
local cmd="$1"
# Collect scripts, Deno tasks, NPM tasks and Makefile tasks
local scripts=()
if [ -d "scripts" ]; then
setopt local_options nullglob 2>/dev/null || shopt -s nullglob 2>/dev/null
for f in scripts/*.ts scripts/*.sh; do
[ -e "$f" ] && scripts+=("$(basename "$f")")
done
fi
local deno_tasks=()
if [ -f "deno.json" ]; then
while IFS= read -r task; do
[ -n "$task" ] && deno_tasks+=("$task")
done < <(jq -r '.tasks // {} | keys | .[]' deno.json 2>/dev/null)
fi
local node_tasks=()
if [ -f "package.json" ]; then
while IFS= read -r task; do
[ -n "$task" ] && node_tasks+=("$task")
done < <(jq -r '.scripts // {} | keys | .[]' package.json 2>/dev/null)
fi
local makefile_tasks=()
if
command -v make &>/dev/null && make -q 2>/dev/null
[ $? -ne 2 ]
then
while IFS= read -r task; do
[ -n "$task" ] && makefile_tasks+=("$task")
done < <(make -p 2>/dev/null | grep -E '^[a-zA-Z0-9_-]+:' | sed 's/://')
fi
# Check if anything is available
if [ ${#scripts[@]} -eq 0 ] && [ ${#deno_tasks[@]} -eq 0 ] && [ ${#node_tasks[@]} -eq 0 ] && [ ${#makefile_tasks[@]} -eq 0 ]; then
echo "No deno.json, package.json, ./scripts/ folder or makefile found" >&2
return 1
fi
# Show help
if [ -z "$cmd" ] || [[ "$cmd" =~ ^(help|--help|-h)$ ]]; then
echo "Available Commands:"
for name in "${scripts[@]}"; do
local base="${name%.*}"
printf " cli %-12s (scripts/%s)\n" "$base" "$name"
done
for t in "${deno_tasks[@]}"; do
printf " cli %-12s (deno.json)\n" "$t"
done
for t in "${node_tasks[@]}"; do
printf " cli %-12s (package.json)\n" "$t"
done
for t in "${makefile_tasks[@]}"; do
printf " cli %-12s (make)\n" "$t"
done
return 0
fi
# Try scripts first
for ext in ts sh; do
local script="scripts/${cmd}.${ext}"
[ -f "$script" ] || continue
shift
if [ "$ext" = "sh" ]; then
bash "$script" "$@"
else
local shebang
shebang=$(sed -n '1s/^#![[:space:]]*//p' "$script")
shebang="${shebang#/usr/bin/env }"
shebang="${shebang#-S }"
local -a interp
read -rA interp <<<"${shebang:-deno run -A}" 2>/dev/null || read -ra interp <<<"${shebang:-deno run -A}"
"${interp[@]}" "$script" "$@"
fi
return $?
done
# Try Makefile tasks next
for t in "${makefile_tasks[@]}"; do
if [ "$cmd" = "$t" ]; then
shift
make "$cmd" "$@"
return $?
fi
done
# Try Deno/NPM tasks last
for t in "${deno_tasks[@]}"; do
if [ "$cmd" = "$t" ]; then
shift
deno task "$cmd" "$@"
return $?
fi
done
for t in "${node_tasks[@]}"; do
if [ "$cmd" = "$t" ]; then
shift
npm run "$cmd" -- "$@"
return $?
fi
done
echo "Unknown command: $cmd (run 'cli' for help)" >&2
return 1
}