-
Notifications
You must be signed in to change notification settings - Fork 33
/
lint.sh
executable file
·98 lines (81 loc) · 2.67 KB
/
lint.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
#!/usr/bin/env bash
# Always run in dev runner (docker) to have the extra dependencies available (currently Python and Hadolint)
# If not running in Docker, start dev runner and run script again there
if [[ ! -f /.dockerenv ]]; then
# Make sure the docker network exists
docker network create wbs-dev > /dev/null 2>&1 || true
exec docker compose --progress quiet run --build --rm runner -c './lint.sh "$@"' -- "$@"
fi
[ -f "local.env" ] || touch local.env
source local.env
# Default values
path="."
fix=false
prettier=false
exit_code=0
# Function to display help message
usage() {
echo "Usage: lint.sh <path> [--fix, -f] [--prettier]"
exit 1
}
# Error handler to capture errors
error_handler() {
# shellcheck disable=SC2317
exit_code=1
}
# Trap ERR to handle errors
trap 'error_handler' ERR
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--fix|-f)
fix=true
shift
;;
--prettier)
prettier=true
shift
;;
*)
path="$1"
shift
;;
esac
done
# Ensure path is a directory
if [[ ! -d $path ]]; then
echo "Error: Path '$path' does not exist or is not a directory."
usage
fi
# Linting JS, YAML, Python scripts, and general whitespace
if $fix; then
echo "ℹ️ Fixing linting issues which can be fixed automatically"
if $prettier; then
echo "ℹ️ Pre-processing with Prettier"
prettier "$path/**/*.{cjs,js,mjs,ts,json}" --log-level error --write
prettier "$path/**/*.md" --log-level error --config .prettierrc.json --write
prettier "$path/**/*.{yml,yaml}" --log-level error --config .prettierrc.json --write
fi
echo "ℹ️ Running ESLint with fix"
eslint "$path" --fix
eslint "$path/**/**" --no-eslintrc --config .eslintrc-whitespace.json --fix
echo "ℹ️ Running Black for Python"
find "$path" -path "$path/node_modules" -prune -o -name '*.py' -print0 | xargs -0 -r \
python3 -m black --quiet
else
echo "ℹ️ Running ESLint (without --fix)"
eslint "$path"
eslint "$path/**/**" --no-eslintrc --config .eslintrc-whitespace.json
echo "ℹ️ Running Python Black on all *.py files (with --check)"
find "$path" -path "$path/node_modules" -prune -o -name '*.py' -print0 | xargs -0 -r \
python3 -m black --diff --quiet --check
fi
echo "ℹ️ Running shellcheck on *.sh files"
find "$path" -type d \( -name node_modules -o -name .git \) -prune -o -type f -name "*.sh" -print0 | xargs -0 \
shellcheck -x
# Always check nx script...
shellcheck -x ./nx
echo "ℹ️ Running hadolint on all Dockerfiles"
find "$path" -type d \( -name node_modules -o -name .git \) -prune -o -type f -name Dockerfile -print0 | xargs -0 -r \
hadolint --config .hadolint.yml
exit $exit_code