-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.sh
More file actions
50 lines (37 loc) · 1.29 KB
/
tree.sh
File metadata and controls
50 lines (37 loc) · 1.29 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
#!/bin/bash
# Output JSON file
OUTPUT_FILE="repo_contents.json"
# Function to recursively list files and directories
generate_json() {
local path="$1"
local first_entry=true
echo "[" >> "$OUTPUT_FILE"
for entry in "$path"/*; do
[[ -e "$entry" ]] || continue # Skip if entry doesn't exist
if [ "$first_entry" = false ]; then
echo "," >> "$OUTPUT_FILE"
fi
first_entry=false
local filename=$(basename "$entry" | sed 's/"/\\"/g') # Escape quotes in filenames
local is_dir=false
if [ -d "$entry" ]; then
is_dir=true
fi
echo -n " { \"name\": \"$filename\", \"type\": \"$(if [ "$is_dir" = true ]; then echo "dir"; else echo "file"; fi)\"" >> "$OUTPUT_FILE"
if [ "$is_dir" = true ]; then
echo ", \"contents\": " >> "$OUTPUT_FILE"
generate_json "$entry"
else
echo " }" >> "$OUTPUT_FILE"
fi
done
echo "]" >> "$OUTPUT_FILE"
}
# Clear previous JSON file if exists
echo "Generating repository file structure..."
> "$OUTPUT_FILE"
# Start JSON generation from the current directory
generate_json "."
# Format JSON properly
jq '.' "$OUTPUT_FILE" > tmp.json && mv tmp.json "$OUTPUT_FILE"
echo "✅ Repository structure saved to $OUTPUT_FILE"