-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_ts_function
More file actions
executable file
·85 lines (70 loc) · 1.58 KB
/
Copy pathgen_ts_function
File metadata and controls
executable file
·85 lines (70 loc) · 1.58 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
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
#!/usr/bin/env bash
open_in_editor=false
update_barrel=false
gen_spec=false
options=$(getopt -o b,g,s,o --long update-barrel,git-add,spec,open -- "$@") || exit
eval set -- "$options"
while true; do
case "$1" in
-b | --update-barrel)
update_barrel=true
shift 1
;;
-g | --git-add)
repo_path=$(git rev-parse --show-toplevel) || exit
shift 1
;;
-s | --spec)
gen_spec=true
shift 1
;;
-o | --open)
open_in_editor=true
shift 1
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
function_full=${1:?"function name is required"}
function_path="$(dirname "$function_full")"
function_name="$(basename "$function_full")"
add_if_in_repo() {
local file_name="$1"
if [ "$repo_path" ]; then
git add "$file_name"
fi
}
mkdir -p "$function_path"
cat <<TS_FUNCTION >"$function_full.ts"
export const $function_name = () => {
//
}
TS_FUNCTION
add_if_in_repo "$function_full.ts"
if [ "$update_barrel" = true ]; then
index_file="$function_path/index.ts"
if [ -f "$index_file" ]; then
export_text="export * from './$function_name'"
if ! grep -qF "$export_text" "$index_file"; then
echo "$export_text;" >>"$index_file"
fi
fi
fi
if [ "$gen_spec" = true ]; then
if [ "$repo_path" ]; then
add_flag="-g"
fi
repo_path=$(git rev-parse --show-toplevel) || exit
test_dir="$repo_path/test/$(realpath "$PWD" --relative-to="$repo_path")"
(cd "$test_dir" && gen_ts_function_spec $add_flag "$function_full")
fi
if [ "$open_in_editor" = true ]; then
idea64.exe "$function_full.ts"
fi