-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateIndices.sh
executable file
·49 lines (38 loc) · 1.22 KB
/
generateIndices.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
#!/bin/bash
ROOT_DIR="./src"
generate_indices() {
local dir="$1"
local index_file="$dir/index.ts"
local -a ts_files
mapfile -t ts_files < <(find "$dir" -maxdepth 1 -type f -name "*.ts" ! -name "index.ts" | sort)
if [[ ${#ts_files[@]} -eq 0 ]]; then
return
fi
echo "Generating index.ts for $dir"
echo "/* Auto-generated by $(basename "$(test -L "$0" && readlink "$0" || echo "$0")") */" > "$index_file"
for file in "${ts_files[@]}"; do
local module_name
module_name=$(basename "$file" .ts)
echo "export * from \"./$module_name.js\";" >> "$index_file"
done
local -a subdirs
mapfile -t subdirs < <(find "$dir" -mindepth 1 -maxdepth 1 -type d | sort)
for subdir in "${subdirs[@]}"; do
generate_indices "$subdir"
done
if [[ "$(realpath "$dir")" == "$(realpath "$ROOT_DIR")" ]]; then
return
fi
local parent_dir
parent_dir=$(dirname "$dir")
local parent_index_file="$parent_dir/index.ts"
local module_name
module_name=$(basename "$dir")
echo "export * from \"./$module_name/index.js\";" >> "$parent_index_file"
}
if [[ ! -d "$ROOT_DIR" ]]; then
echo "Root directory ‘$ROOT_DIR’ not found."
exit 1
fi
generate_indices "$ROOT_DIR"
echo "Index files generated successfully."