-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_json.sh
86 lines (76 loc) · 2.52 KB
/
generate_json.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
#!/bin/bash
# File path
FILE_PATH="structure.json"
ROOT_DIR="src"
# Function to format the name
format_name() {
local name="$1"
name="${name%.md}" # Remove .md extension
name=$(echo "$name" | sed -r 's/([a-z])([A-Z])/\1 \2/g') # Add space before capital letters
name=$(echo "$name" | sed -r 's/([A-Z])([A-Z][a-z])/\1 \2/g') # Add space between consecutive capital letters
echo "$name"
}
# Function to recursively generate JSON structure from directory
generate_json() {
local dir_path="$1"
local indent="$2"
local json=""
local has_md_file=false
json+="${indent}{\n"
json+="${indent} \"name\": \"$(basename "$dir_path")\",\n"
json+="${indent} \"isDirectory\": true,\n"
json+="${indent} \"children\": [\n"
local first=true
for entry in "$dir_path"/*; do
if [ -d "$entry" ]; then
local child_json=$(generate_json "$entry" " $indent")
if [ -n "$child_json" ]; then
if [ "$first" = true ]; then
first=false
else
json+=",\n"
fi
json+="$child_json"
fi
elif [[ "$entry" == *.md ]]; then
if [ "$first" = true ]; then
first=false
else
json+=",\n"
fi
local formatted_name=$(format_name "$(basename "$entry")")
json+="${indent} {\n"
json+="${indent} \"name\": \"$formatted_name\",\n"
json+="${indent} \"isDirectory\": false,\n"
json+="${indent} \"path\": \"$ROOT_DIR/${entry/$ROOT_DIR\//}\"\n"
json+="${indent} }"
has_md_file=true
fi
done
json+="\n${indent} ]\n"
json+="${indent}}"
if [ "$has_md_file" = true ]; then
echo -e "$json"
else
echo ""
fi
}
# Generate JSON structure from the contents of the "src" directory
json_structure=""
first=true
for entry in "$ROOT_DIR"/*; do
if [ -d "$entry" ] || [[ "$entry" == *.md ]]; then
entry_json=$(generate_json "$entry" " ")
if [ -n "$entry_json" ]; then
if [ "$first" = true ]; then
first=false
else
json_structure+=",\n"
fi
json_structure+="$entry_json"
fi
fi
done
# Write the updated JSON to the file
echo -e "{\n \"name\": \"root\",\n \"isDirectory\": true,\n \"children\": [\n$json_structure\n ]\n}" | jq '.' > "$FILE_PATH"
echo "JSON structure updated successfully."