Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Commit caa125c

Browse files
committed
chore: improve install.sh script
1 parent 45a624f commit caa125c

File tree

1 file changed

+103
-29
lines changed

1 file changed

+103
-29
lines changed

src/public/install.sh

Lines changed: 103 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ GH_REPO="Barbapapazes-Sponsors/vue-ui"
77
GH_TOKEN=$GITHUB_TOKEN
88
RELATIONS_URL="https://vue-ui.barbapapazes.dev/relations.json"
99

10+
validate_dependencies() {
11+
for cmd in curl jq sed; do
12+
if ! command -v $cmd &>/dev/null; then
13+
echo "Error: $cmd is not installed. Please install it and try again."
14+
exit 1
15+
fi
16+
done
17+
18+
if [ -z "$GH_TOKEN" ]; then
19+
echo "Error: GH_TOKEN environment variable is not set."
20+
exit 1
21+
fi
22+
}
23+
24+
log_debug() {
25+
if [ "$DEBUG" = true ]; then
26+
echo "[DEBUG] $(date +'%Y-%m-%d %H:%M:%S') $1"
27+
fi
28+
}
29+
1030
fetch_directory_contents() {
1131
local repo_path=$1
1232
curl -sL \
@@ -19,8 +39,10 @@ fetch_directory_contents() {
1939
rewrite_path() {
2040
local local_path=$1
2141
local name=$2
22-
# If it's a component, we need to remove the subfolder references (components/ComponentName/ComponentName.vue -> components/ComponentName.vue)
23-
if [[ "$local_path" == *"/components/"* ]]; then
42+
43+
# If it's a component, remove the subfolder references (components/ComponentName/ComponentName.vue to components/ComponentName.vue or Components/ComponentName/ComponentName.vue to Components/ComponentName.vue)
44+
45+
if [[ "$local_path" == *"/components/"* || "$local_path" == *"/Components/"* ]]; then
2446
echo "${local_path%/*}/$name"
2547
else
2648
echo "$local_path/$name"
@@ -30,15 +52,17 @@ rewrite_path() {
3052
rewrite_imports() {
3153
local final_path=$1
3254
local mode=$2
33-
sed -i 's|@/ui/components\/\([^/]*\)\/\1.vue|@/ui/components/\1.vue|g' "$final_path"
55+
local case_option=$3
56+
57+
sed -i 's|@/ui/components/\([^/]*\)/\1.vue|@/ui/components/\1.vue|g' "$final_path"
58+
3459
if [[ "$mode" == "inertia" ]]; then
35-
sed -i 's|@/ui/components|@/UI/Components|g' "$final_path"
36-
sed -i 's|@/ui/composables|@/UI/Composables|g' "$final_path"
37-
sed -i 's|@/ui/theme|@/UI/Theme|g' "$final_path"
38-
sed -i 's|@/ui/utils|@/UI/Utils|g' "$final_path"
39-
sed -i 's|@/ui/types|@/UI/Types|g' "$final_path"
40-
sed -i 's|@/ui/icons|@/UI/Icons|g' "$final_path"
41-
sed -i 's|@/ui/keys|@/UI/Keys|g' "$final_path"
60+
if [[ "$case_option" == "capitalize" ]]; then
61+
for folder in components composables theme utils types icons keys; do
62+
capitalized_folder=$(echo "$folder" | sed -E 's/^(.)/\U\1/')
63+
sed -i "s|@/ui/$folder|@/UI/$capitalized_folder|g" "$final_path"
64+
done
65+
fi
4266
fi
4367
}
4468

@@ -47,10 +71,13 @@ download_files() {
4771
local repo_path=$1
4872
local local_path=$2
4973
local mode=$3
74+
local case_option=$4
75+
local max_jobs=5 # Limit the number of parallel jobs
76+
local job_count=0
5077

5178
response=$(fetch_directory_contents "$repo_path")
5279

53-
[ "$DEBUG" = true ] && echo "DEBUG: Processing $(echo "$response" | jq -r '. | length') items"
80+
log_debug "Processing $(echo "$response" | jq -r '. | length') items"
5481

5582
# Loop through each item in the directory
5683
for item in $(echo "$response" | jq -r '.[] | @base64'); do
@@ -63,19 +90,31 @@ download_files() {
6390
type=$(_jq '.type')
6491
download_url=$(_jq '.download_url')
6592

93+
# Skip the public directory
94+
if [[ "$path" == *"/public"* ]]; then
95+
log_debug "Skipping public directory: $path"
96+
continue
97+
fi
98+
6699
if [ "$type" == "dir" ]; then
67100
if [[ "$name" == "__snapshots__" ]]; then
68101
continue
69102
fi
70103

71-
if [[ "$local_path" != *"/components"* ]]; then
104+
if [[ "$case_option" == "capitalize" ]]; then
105+
# Capitalize the folder name
106+
capitalized_name=$(echo "$name" | sed -E 's/^(.)/\U\1/')
107+
mkdir -p "$local_path/$capitalized_name"
108+
next_local_path="$local_path/$capitalized_name"
109+
else
72110
mkdir -p "$local_path/$name"
111+
next_local_path="$local_path/$name"
73112
fi
74113

75-
[ "$DEBUG" = true ] && echo "DEBUG: Processing directory $name"
114+
log_debug "Processing directory $name"
76115

77116
# Recursively download the contents of the directory
78-
download_files "$path" "$local_path/$name" "$mode"
117+
download_files "$path" "$next_local_path" "$mode" "$case_option" &
79118
else
80119
if [[ "$name" == *.md ]]; then
81120
continue
@@ -85,30 +124,40 @@ download_files() {
85124
continue
86125
fi
87126

88-
[ "$DEBUG" = true ] && echo "DEBUG: Processing file $name"
127+
log_debug "Processing file $name"
128+
129+
# Ensure the directory exists before downloading the file
130+
mkdir -p "$local_path"
89131

90132
# Determine the final path by calling the new rewrite_path function
91133
final_path=$(rewrite_path "$local_path" "$name")
92134

93-
echo "Relations: ${relations[@]}" "$name"
94-
135+
echo "Processing file $name to $final_path"
95136
# for each relation, we check if the name of the file starts with the relation (only if the relations is not empty)
96137
if [ ${#relations[@]} -gt 0 ]; then
97138
for relation in "${relations[@]}"; do
98139
if [[ "$name" == "$relation"* ]]; then
99-
[ "$DEBUG" = true ] && echo "DEBUG: Downloading file $name to $final_path"
100-
download_file "$download_url" "$final_path"
101-
rewrite_imports "$final_path" "$mode"
140+
log_debug "Downloading file $name to $final_path"
141+
download_file "$download_url" "$final_path" && rewrite_imports "$final_path" "$mode" "$case_option" &
102142
fi
103143

104144
done
105145
else
106-
[ "$DEBUG" = true ] && echo "DEBUG: Downloading file $name to $final_path"
107-
download_file "$download_url" "$final_path"
108-
rewrite_imports "$final_path" "$mode"
146+
log_debug "Downloading file $name to $final_path"
147+
download_file "$download_url" "$final_path" && rewrite_imports "$final_path" "$mode" "$case_option" &
109148
fi
110149
fi
150+
151+
# Manage the job queue
152+
((job_count++))
153+
if (( job_count >= max_jobs )); then
154+
wait -n # Wait for at least one job to finish before continuing
155+
((job_count--))
156+
fi
111157
done
158+
159+
# Wait for all background processes to complete
160+
wait
112161
}
113162

114163
download_file() {
@@ -173,6 +222,7 @@ install_component() {
173222
local component=$1
174223
local local_path=$2
175224
local mode=$3
225+
local case_option=$4
176226
local repo_path="src"
177227

178228
all_relations=$(fetch_relations)
@@ -181,24 +231,26 @@ install_component() {
181231
relations=("$component")
182232
search_relations "components" "$component" "$all_relations"
183233

184-
download_files "$repo_path" "$local_path" "$mode"
234+
download_files "$repo_path" "$local_path" "$mode" "$case_option"
185235
}
186236

187237
install_components() {
188238
local local_path=$1
189239
local mode=$2
240+
local case_option=$3
190241
local repo_path="src"
191242

192-
download_files "$repo_path" "$local_path" "$mode"
243+
download_files "$repo_path" "$local_path" "$mode" "$case_option"
193244
}
194245

195246
parse_options() {
196-
while getopts "s:d:m:c:" opt; do
247+
while getopts "s:d:m:c:C:" opt; do
197248
case "$opt" in
198249
s) REPO_PATH="$OPTARG" ;;
199250
d) LOCAL_PATH="$OPTARG" ;;
200251
m) MODE="$OPTARG" ;;
201252
c) COMPONENT="$OPTARG" ;;
253+
C) CASE_OPTION="$OPTARG" ;;
202254
*) usage ;;
203255
esac
204256
done
@@ -217,24 +269,46 @@ validate_options() {
217269
echo "Error: mode must be either 'vite' or 'inertia'."
218270
usage
219271
fi
272+
# Set default case option if not provided
273+
if [ -z "$CASE_OPTION" ]; then
274+
CASE_OPTION="lower"
275+
fi
276+
# Validate case option value
277+
if [ "$CASE_OPTION" != "lower" ] && [ "$CASE_OPTION" != "capitalize" ]; then
278+
echo "Error: case must be either 'lower' or 'capitalize'."
279+
usage
280+
fi
220281
}
221282

222283
usage() {
223-
echo "Usage: $0 -s <repo_path> -d <local_path> [-m <mode>] [-c <component>]"
284+
echo "Usage: $0 -s <repo_path> -d <local_path> [-m <mode>] [-c <component>] [-C <case_option>]"
224285
echo " mode: vite (default) or inertia"
225286
echo " component: name of the component to install (optional)"
287+
echo " case_option: lower (default) or capitalize"
288+
echo " --help: display this help message"
226289
exit 1
227290
}
228291

229292
main() {
293+
if [[ "$1" == "--help" ]]; then
294+
usage
295+
fi
296+
297+
validate_dependencies
230298
parse_options "$@"
231299
validate_options
232300

233301
if [ -n "$COMPONENT" ]; then
234-
install_component "$COMPONENT" "$LOCAL_PATH" "$MODE"
302+
install_component "$COMPONENT" "$LOCAL_PATH" "$MODE" "$CASE_OPTION"
235303
else
236-
install_components "$LOCAL_PATH" "$MODE"
304+
install_components "$LOCAL_PATH" "$MODE" "$CASE_OPTION"
237305
fi
306+
307+
# Remove empty directories
308+
find "$LOCAL_PATH" -type d -empty -delete
309+
310+
echo "Installation completed successfully."
311+
echo "You can now use the components in your project."
238312
}
239313

240314
main "$@"

0 commit comments

Comments
 (0)