-
Notifications
You must be signed in to change notification settings - Fork 5
/
lint.sh
executable file
·166 lines (131 loc) · 3.06 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
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
#!/bin/sh
###################
# variables
###################
# You can customize the jshint location using the JSHINT variable.
# e.g. JSHINT=./node_modules/.bin/jshint scripts/lint/lint.sh
# if you want to use a local version of jshint.
if [ -z "$JSHINT" ]; then
JSHINT="jshint"
fi
name="node-datetime";
cwd=`pwd`;
# list directories/files to lint
list=();
defaultDirList="index.js src/";
# optional space separated list of directories/files to lint
# Example: ./lint.sh "mydir/ myFile" > this will lint all files in mydir/ and lint myFile
dirList=$1;
##################
# functions
##################
indexOf() {
pos="${1%%$2*}";
[[ $pos = $1 ]] && echo -1 || echo ${#pos};
}
echoGreen() {
echo -en '\E[32m'"\033[1m`checkMark` $1\033[0m\n\r";
}
echoYellow() {
echo -en '\E[33m'"\033[1m$1\033[0m\n\r";
}
echoBlue() {
echo -en '\E[34m'"\033[1m`mark` $1\033[0m\n\r";
}
echoRed() {
echo -en '\E[31m'"\033[1m`errorMark` $1\033[0m\n\r";
}
arrowMark() {
echo -e "\xe2\x86\x92";
}
checkMark() {
echo -e "\xE2\x9C\x93";
}
errorMark() {
echo -e "\xC7\x83";
}
mark() {
echo -e "\xCB\x83 ";
}
# probably pointless...
lintTreeObj() {
# lint the code to be commited
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD;
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904;
fi
toBeCommited=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$");
echoBlue "linting added files...";
# lint JavaScript files only
for file in ${toBeCommited}; do
echo "linting $path$file";
failed=`$JSHINT "$path$file"`;
if [ "$failed" ]; then
echoRed "[error] line error(s) in $1";
echoRed "$failed";
exit 1;
else
echoGreen "Passed [OK]";
fi
done
}
lint() {
# lint the code in the specified directories (this may not include added files to git)
targetPath="$path$1";
if [ -d "$targetPath" ] || [ -f "$targetPath" ]; then
echo "linting $targetPath";
failed=`$JSHINT "$targetPath"`;
if [ "$failed" ]; then
echoRed "[error] lint error(s) in $1";
echoRed "$failed";
exit 1;
else
echoGreen "Passed [OK]";
fi
else
echoRed "[error] $targetPath";
echoRed "No such file or directory ($targetPath)";
exit 1;
fi
}
##########################
# procedural codes
##########################
# test if jshtin command is avialable
if ! type "$JSHINT" > /dev/null; then
echoRed "[error] jshint command is not available";
exit 1;
fi
# find root path
index=`indexOf "$cwd" "$name"`;
if [ "$index" -ne -1 ]; then
path=`expr substr $cwd 1 $index`"$name/";
else
path="./";
fi
echoBlue "Current working directory: $cwd";
echoBlue "Root path: $path";
# find directories/files to lint
if [ "$dirList" ]; then
list=($dirList);
else
list=($defaultDirList);
fi
echoYellow "directories/files to lint:";
for item in "${list[@]}"; do
echoBlue "${item}";
done
# start linting
echoYellow "Executing jshint...";
# lint the files in git tree
#lintTreeObj "";
echoBlue "lint files in specified directories...";
# lint
for item in "${list[@]}"; do
lint "${item}";
done
echoYellow "Done";
exit 0;