-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_fix.sh
executable file
·146 lines (139 loc) · 7.32 KB
/
export_fix.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
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
#!/usr/bin/env bash
# respository: https://github.com/ramirezjag00/group-exports-last-autofix
fix_sub_directories() {
if [[ ! -z $@ ]]; then
echo -e "\\n🗂 processing $@"
for FILE in $@/*; do ./export_fix.sh $FILE; done
echo -e "\\n $@ directory, DONE! 🚀"
else
echo "❌ Missing argument: Forgot to pass directory to expand
usage: ./export_fix_all.sh ./examples/src/sampleDir"
fi
}
needle_export_named="export const"
needle_export_named_single="export {"
needle_export_default_anonymous="export default ("
needle_existing_export_default_anonymous="export default "
needle_export_function="function"
needle_export_default_anonymous_async="export default async"
needle_export_default_aggregated="export { default as"
needle_export_default_aggregated_2="export { default }"
needle_export_default_object="export default {"
export_named_list=""
export_default=""
export_default_aggregate=""
export_default_aggregate_2=""
finished="✅ $1 DONE!"
file_name=$(echo ${1##*/})
sanitized_code=""
bottom_export=""
named_exports_length=0
group_exports_last() {
echo "$sanitized_code" > $@
echo -e "\\n$bottom_export" >> $@
echo $finished
}
scan_prefer_default() {
named_exports_length=$(echo $@ | tr -cd ',' | wc -c)
if [[ $named_exports_length -gt 1 ]]; then
bottom_export="export { $@ };"
else
parsed_export=$(echo "$@" | cut -d "," -f 1)
bottom_export="export default $parsed_export;"
fi
}
if [[ $file_name == *".js"* ]] || [[ $file_name == *".jsx"* ]] || [[ $file_name == *".ts"* ]] || [[ $file_name == *".tsx"* ]]; then
echo -e "\\n⚙️ processing $1"
while read -r line; do
if [[ ${line} == *"$needle_export_function"* ]]; then
if [[ ${line} == "export default $needle_export_function ("* ]]; then
if [[ $1 == *"/"* ]]; then
export_default=$(echo ${1##*/} | cut -d "." -f 1)
else
export_default=$(echo $1 | cut -d "." -f 1)
fi
sanitized_code=$(cat $1 | sed "s/export default $needle_export_function/const $export_default =/g" | sed "s/) {/) => {/g")
echo "$sanitized_code" > $1
elif [[ ${line} == "export default $needle_export_function "* ]]; then
parsed_export=$(echo "$line" | cut -d " " -f 4 | cut -d "(" -f 1)
sanitized_code=$(cat $1 | sed "s/export default $needle_export_function $parsed_export/const $parsed_export = /g" | sed "s/) {/) => {/g")
echo "$sanitized_code" > $1
export_default=$(echo $parsed_export)
elif [[ ${line} == "export $needle_export_function "* ]]; then
parsed_export=$(echo "$line" | cut -d " " -f 3 | cut -d "(" -f 1)
sanitized_code=$(cat $1 | sed "s/export $needle_export_function $parsed_export/const $parsed_export = /g" | sed "s/) {/) => {/g")
echo "$sanitized_code" > $1
export_named_list="$export_named_list $parsed_export,"
fi
elif ([[ ${line} != "$needle_export_default_anonymous_async"* ]] && [[ ${line} != "$needle_existing_export_default_anonymous"[a-z]*" => {" ]] && [[ ${line} != "$needle_existing_export_default_anonymous"[a-z]*" => ({" ]] && [[ ${line} == "$needle_existing_export_default_anonymous"[a-zA-Z]* ]]) && ([[ $(cat ${1}) == *"$needle_export_named"* ]] || [[ ! -z "$export_default_aggregate" ]] || ([[ $(cat ${1}) == *"$needle_export_named_single"* ]] || [[ ! -z "$export_named_list" ]])); then
export_default=$(echo $line | cut -d " " -f 3 | sed "s/.$//")
sanitized_code=$(cat $1 | sed "s~$line~~g")
echo "$sanitized_code" > $1
elif [[ ${line} == "$needle_export_default_anonymous"* ]] || [[ ${line} == "$needle_export_default_object"* ]] || [[ ${line} == "$needle_export_default_anonymous_async "* ]] || [[ ${line} == "$needle_existing_export_default_anonymous"[a-z]*" => {" ]] || [[ ${line} == "$needle_existing_export_default_anonymous"[a-z]*" => ({" ]]; then
if [[ $1 == *"/"* ]]; then
export_default=$(echo ${1##*/} | cut -d "." -f 1)
else
export_default=$(echo $1 | cut -d "." -f 1)
fi
elif [[ $line == *"$needle_export_default_aggregated"* ]]; then
parsed_export=$(echo "$line" | cut -d " " -f 5)
reshaped_import="import $parsed_export $(echo ${line##*\}})"
sanitized_code=$(cat $1 | sed "s~${line}~${reshaped_import}~g")
echo "$sanitized_code" > $1
export_default_aggregate="$export_default_aggregate $parsed_export,"
elif [[ $line == *"$needle_export_default_aggregated_2"* ]]; then
export_default_aggregate_2=$(echo ${line##*/} | cut -d "'" -f 1 | cut -d "\"" -f 1)
elif [[ $line == *"$needle_export_named"* ]]; then
parsed_export=$(echo "$line" | cut -d " " -f 3)
export_named_list="$export_named_list $parsed_export,"
elif [[ $line != *","* ]] && [[ $line != *"default"* ]] && [[ $line == "$needle_export_named_single "[a-zA-Z]* ]]; then
parsed_export=$(echo "$line" | cut -d " " -f 3)
export_named_list="$export_named_list $parsed_export,"
sanitized_code=$(cat $1 | sed "s~$line~~g")
echo "$sanitized_code" > $1
fi
done < <(grep "export" <<< cat $1)
if [[ ! -z "$export_default" ]] && [[ ! -z "$export_named_list" ]]; then
bottom_export="export { $export_default as default, $export_named_list };"
sanitized_code=$(cat $1 | sed "s/export default/const $export_default =/g" | sed "s/export //g")
group_exports_last $1
elif [[ ! -z "$export_default_aggregate_2" ]] && [[ ! -z "$export_default_aggregate" ]]; then
bottom_export="export { $export_default_aggregate_2 as default, $export_default_aggregate };"
sanitized_code=$(cat $1 | sed "s/export { default }/import $export_default_aggregate_2/g")
group_exports_last $1
elif [[ ! -z "$export_default" ]] && [[ ! -z "$export_default_aggregate" ]]; then
bottom_export="export { $export_default as default, $export_default_aggregate };"
sanitized_code=$(cat $1 | sed "s/export default/const $export_default =/g")
group_exports_last $1
elif [[ ! -z "$export_named_list" ]] && [[ ! -z "$export_default_aggregate" ]]; then
bottom_export="export {$export_named_list $export_default_aggregate };"
sanitized_code=$(cat $1 | sed "s/export //g")
group_exports_last $1
elif [[ ! -z "$export_default_aggregate_2" ]] && [[ ! -z "$export_named_list" ]]; then
bottom_export="export { $export_default_aggregate_2 as default, $export_named_list };"
sanitized_code=$(cat $1 | sed "s/export { default }/import $export_default_aggregate_2/g" | sed "s/export //g")
group_exports_last $1
elif [[ ! -z "$export_default_aggregate_2" ]]; then
bottom_export="export default $export_default_aggregate_2;"
sanitized_code=$(cat $1 | sed "s/export { default }/import $export_default_aggregate_2/g")
group_exports_last $1
elif [[ ! -z "$export_default" ]]; then
bottom_export="export default $export_default;"
sanitized_code=$(cat $1 | sed "s/export default/const $export_default =/g")
group_exports_last $1
elif [[ ! -z "$export_default_aggregate" ]]; then
scan_prefer_default $export_default_aggregate
echo -e "\\n$bottom_export" >> $1
echo $finished
elif [[ ! -z "$export_named_list" ]]; then
scan_prefer_default $export_named_list
sanitized_code=$(cat $1 | sed "s/export //g")
group_exports_last $1
else
echo -e "\\n⚠️ $1 is already sanitized ⚠️\\n"
fi
elif [[ file_name != *'.'* ]]; then
fix_sub_directories $1
else
echo -e "\\n⚠️ $1 is not a .js,.jsx,.ts,.tsx file ⚠️\\n"
fi