-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
.cmds
207 lines (190 loc) · 5.62 KB
/
.cmds
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
confirm() {
# https://djm.me/ask
local prompt default reply
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
printf "$1 [$prompt] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read reply </dev/tty
# Default?
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y* | y*) return 0 ;;
N* | n*) return 1 ;;
esac
done
}
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
DEV_ENV="$SCRIPT_DIR/.devenv"
if [[ -f $DEV_ENV ]]; then
MODULE=$(npx json "module" <"$DEV_ENV")
FPATH=$(npx json "foundry.path" <"$DEV_ENV")
FDATA=$(npx json "foundry.data" <"$DEV_ENV")
fi
set_dev_env() {
if [[ ! -f $DEV_ENV ]]; then
touch $DEV_ENV
fi
printf '{
"module": "%s",
"foundry": {
"path": "%s",
"data": "%s"
}
}' "$1" "$2" "$3" >$DEV_ENV
}
set_module() {
# If there is no target passed, then print the current target
if [[ $# == 0 ]]; then
echo "Current Module Target: $MODULE"
return
fi
if [[ -d "$SCRIPT_DIR/$1" ]]; then
set_dev_env "$1" "$FPATH" "$FDATA"
echo -n "$(npx json -0 -f .vscode/settings.template.json -e "this.TARGET='$1'")" >.vscode/settings.json
else
echo "The module directory '$1' does not exist"
fi
echo "Module Target is now \"$1\""
}
set_foundry() {
# If there is no target passed, then print the current target
if [[ $# == 0 ]]; then
printf "Current Environment\n\tFoundry Path: %s\n\tFoundry Data: %s\n" $FPATH $FDATA
return
fi
if [[ ! "$1" = "" ]]; then
if [[ ! -d "$1" ]]; then
printf 'The FoundryVTT Program directory does not exist at\n\t%s\n' $1
return
fi
fi
if [[ ! -d "$2" ]]; then
printf 'The FoundryVTT Data directory does not exist at\n\t%s\n' $2
return
fi
set_dev_env $MODULE "$1" "$2"
printf 'Updated Foundry Reference
Foundry Path: %s
Foundry Data: %s\n' "$1" "$2"
}
launch_foundry() {
sudo $(which node) "$FPATH/resources/app/main.js" --dataPath="$FDATA"
}
run_linter() {
echo "Linting $MODULE"
if [[ "$1" == "fix" ]]; then
ESLFIX="--fix"
JSLFIX="-i --enforce-double-quotes --trim-trailing-commas"
fi
npx eslint --color --ext .ts,.tsx $ESFIX "$SCRIPT_DIR/$MODULE/src" | sed 's|'"$SCRIPT_DIR"'/||g'
echo "$JSLFIX"
npx jsonlint -Dpq $JSLFIX -t $'\t' $(realpath "$SCRIPT_DIR/$MODULE/lang/*.json")
}
gulp_build() {
echo "Building $MODULE"
DIR="$SCRIPT_DIR/$MODULE"
if [[ ! -d "$DIR" ]]; then
echo "Could not navigate to '$DIR'"
echo "Please make sure the module directory exists and execute the following"
echo " > npm run target df-module-name"
return
fi
# Go to module directory
cd $DIR
# Execute the gulp commands
npx gulp --color --gulpfile "${SCRIPT_DIR}/gulpfile.js" --cwd="$DIR" "$@" |& sed 's|'"$SCRIPT_DIR"'/||g'
}
git_tag() {
MANIFEST="$SCRIPT_DIR/$MODULE/module.json"
MODULE_NAME=$(npx json "id" <"$MANIFEST")
MODULE_VERS=$(npx json "version" <"$MANIFEST")
echo "Tagging ${MODULE_NAME}_${MODULE_VERS}"
git tag -d ${MODULE_NAME}_${MODULE_VERS} 2>/dev/null
git tag ${MODULE_NAME}_${MODULE_VERS} &&
git push -f origin ${MODULE_NAME}_${MODULE_VERS}
echo "Tagged current commit as \"${MODULE_NAME}_${MODULE_VERS}\""
}
open_bundle() {
if command -v explorer.exe &>/dev/null; then
confirm "Do you want to open the bundle in explorer?" Y &&
explorer.exe $(wslpath -w "$SCRIPT_DIR/$MODULE/bundle/")
elif command -v browse &>/dev/null; then
confirm "Do you want to open the bundle in file browser?" Y &&
browse "$SCRIPT_DIR/$MODULE/bundle/"
elif command -v open &>/dev/null; then
confirm "Do you want to open the bundle in finder?" Y &&
open "$SCRIPT_DIR/$MODULE/bundle/"
fi
return 0
}
build_shims() {
if [[ -d "$SCRIPT_DIR/bundle" ]]; then
rm -R "$SCRIPT_DIR/bundle"
fi
mkdir "$SCRIPT_DIR/bundle"
echo "Building Shims"
# copy the shim raw
for FILE in common/*.shim.ts; do
cp "$FILE" "$SCRIPT_DIR/bundle/"
done
# compile the shims to tabbed JS
tsc $SCRIPT_DIR/bundle/*.shim.ts --outDir "$SCRIPT_DIR/bundle" --target es6 --sourceMap &&
npx spaces-to-tabs -s 4 bundle/*.shim.js ||
return
# minify the shim JS files
for FILE in $SCRIPT_DIR/bundle/*.shim.js; do
[ -e "$FILE" ] || continue
MIN_FILE="$SCRIPT_DIR/bundle/$(basename "$FILE" .js).min.js"
# uglify the code to a *.min.js file
npx uglifyjs --mangle --keep-fnames "$FILE" --output "$MIN_FILE"
# append the source map link to the minified file
HEADER_LENGTH=$(cat $FILE | grep '*/' -n -m 1 | sed 's/\([0-9]*\).*/\1/')
echo -n -e "$(head -$HEADER_LENGTH "$FILE")\n$(cat "$MIN_FILE")\n$(tail -1 "$FILE")" > "$MIN_FILE"
done
}
swap_env() {
if [[ -e "$DEV_ENV-swap" ]] ; then
echo "Swapping dev environment but preserving module target."
else
echo "There is no swap environment available.\nPlease create a .devenv-swap file."
exit
fi
TARGET_MODULE=$(npx json "module" <"$DEV_ENV")
NEWP=$(npx json "foundry.path" <"$DEV_ENV-swap")
NEWD=$(npx json "foundry.data" <"$DEV_ENV-swap")
OLDP=$(npx json "foundry.path" <"$DEV_ENV")
OLDD=$(npx json "foundry.data" <"$DEV_ENV")
rm "$DEV_ENV-swap" && cp "$DEV_ENV" "$DEV_ENV-swap"
set_dev_env "$TARGET_MODULE" "$NEWP" "$NEWD"
printf "Environment Swapped\n\t%s -> %s\n\t%s -> %s\n" $OLDP $NEWP $OLDD $NEWD
}
COMMAND="$1"
shift
case $COMMAND in
"target") set_module "$@" ;;
"foundry") set_foundry "$@" ;;
"launch") launch_foundry "$@" ;;
"lint") run_linter "$@" ;;
"build") gulp_build "$@" ;;
"tag") git_tag ;;
"bundle") open_bundle ;;
"shims") build_shims ;;
"swap") swap_env ;;
*) echo "Unknown Command: $COMMAND" ;;
esac
exit 0