-
Notifications
You must be signed in to change notification settings - Fork 2
/
ts
executable file
·110 lines (99 loc) · 2.71 KB
/
ts
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
#!/usr/bin/env bash
#region config
BUILD=( yarn --silent --ignore-engines run build:ts );
PATH_REWRITE_FROM=src
PATH_REWRITE_TO=out
EXTRA_NODE_ARGS=( --enable-source-maps ) # --inspect
#endregion config
#region main
# template: ts.sh
# version: 2024-03-07
# description: Build (npm/yarn build) and execute TypeScript file
RED="\x1b[31m";BLUE="\x1b[34m";RESET="\x1b[0m";
usage() {
local bin; bin="$(basename "${BASH_SOURCE[0]}")";
echo "";
echo " Usage: $bin [...node-args] <ts-file> [...args]";
echo "";
exit 0;
}
throw() { echo -e "${RED}fatal: ${1}${RESET}" >&2; exit 1; }
execute() {
printf "${BLUE}\$${RESET} %s\n" "${*}";
if [ "$1" == "pushd" ] || [ "$1" == "popd" ]; then
"${@}" >/dev/null || throw "failed to execute '$1'";
else
"${@}" || throw "failed to execute '$1'";
fi
}
getJsPathFromTs() {
local envs;
envs=(
PATH_REWRITE_FROM="$PATH_REWRITE_FROM"
PATH_REWRITE_TO="$PATH_REWRITE_TO"
PATH_REWRITE="$1"
)
env "${envs[@]}" node -e '
const from = process.env.PATH_REWRITE_FROM + "/";
const to = process.env.PATH_REWRITE_TO + "/";
const path = process.env.PATH_REWRITE
.replace(/\.\w+$/, "")
.replace(from, to);
console.log(path);
';
}
# check environment
command -v node >/dev/null || throw "node is not installed!";
# test config
if [ "${#BUILD[@]}" -eq 0 ]; then
# `$NPM` existed in old config
if [ -z "$NPM" ]; then
command -v yarn >/dev/null && NPM=yarn || NPM=npm;
fi
BUILD=( "$NPM" --silent run build );
fi
tsFile=
tsArgs=()
nodeArgs=()
argForNode=1
for arg in "$@"; do
if test -n "$argForNode"; then
if [[ "${arg}" == "--" ]]; then argForNode=;
elif [[ "${arg}" == "--"* ]]; then nodeArgs+=("${arg}");
elif [[ "${arg}" == inspect ]] && [[ "${#nodeArgs[@]}" -eq 0 ]]; then
nodeArgs+=("${arg}");
else
argForNode=;
tsFile="${arg}";
fi
continue;
fi
if test -z "$tsFile"; then
tsFile="$arg";
else
tsArgs+=("$arg");
fi
done
[[ "${#EXTRA_NODE_ARGS[@]}" -gt 0 ]] && nodeArgs+=( "${EXTRA_NODE_ARGS[@]}" );
test -n "$tsFile" || usage;
test -f "$tsFile" || throw "'$tsFile' is not a file";
pkgDir="$(dirname "$tsFile")";
pkgJSON=;
while test -z "$pkgJSON"; do
_pkgJSON="${pkgDir}/package.json";
if test -f "$_pkgJSON"; then
pkgJSON="$_pkgJSON";
break;
fi
_pkgDir="$(dirname "$pkgDir")";
[ "$_pkgDir" == "$pkgDir" ] && break;
pkgDir="$_pkgDir";
done
test -n "$pkgJSON" || throw "the project file of the '$tsFile' can't be found";
jsFile="$(getJsPathFromTs "$tsFile")";
test -n "$jsFile" || throw "failed to get js file path";
execute pushd "$pkgDir";
execute "${BUILD[@]}";
execute popd;
execute node "${nodeArgs[@]}" "$jsFile" "${tsArgs[@]}";
#endregion main