-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_watched_file.sh
executable file
·80 lines (66 loc) · 1.59 KB
/
handle_watched_file.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
#!/usr/bin/env bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "${SCRIPT_DIR}/boilerplate.sh"
function now {
::now
}
function print {
::log "$@"
}
function run_all_tests {
print "Running all tests."
local exit_code=0
for test_file in ./tests/**/*test.*js; do
run_test "${test_file}"
! [[ $? -eq 0 ]] && exit_code=1
done
print "Done running all tests."
return ${exit_code}
}
function run_test {
local test_file=$1
print "Running test: ${test_file}"
node "${test_file}"
return $?
}
function get_test_file {
local src_file=$1
local filename=$(basename ${src_file})
local dirpath=$(dirname ${src_file})
local test_dirpath=${dirpath/power/power\/tests}
local test_filename=
if [[ "${src_file}" =~ \.js ]]; then
test_filename=${filename/\.js/.test.mjs}
elif [[ "${src_file}" =~ \.mjs ]]; then
test_filename=${filename/\.mjs/.test.mjs}
fi
echo ${test_dirpath}/${test_filename}
}
function rebuild_app {
print "Rebuilding app"
npm run build &>/tmp/power-npm-run-build.log &
}
function main {
local file=$1
local test_file=
clear
if ! [[ "${file}" =~ /tests/ ]]; then
# regenerate app if changed file isn't a test file
rebuild_app
fi
if [[ "${file}" =~ test\.m?js ]]; then
run_test "${file}"
elif [[ "${file}" =~ \.m?js ]]; then
test_file=$(get_test_file "${file}")
if [[ -e "${test_file}" ]]; then
run_test "${test_file}"
else
if ! [[ "${file}" =~ /ui/ ]]; then
# /ui/ files do not have tests
run_all_tests
fi
fi
fi
exit $?
}
main "$@"