1 | #!/bin/sh
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | if [ -z "$JSHINT" ]; then
|
11 | JSHINT="jshint"
|
12 | fi
|
13 | name="node-datetime";
|
14 | cwd=`pwd`;
|
15 |
|
16 | list=();
|
17 | defaultDirList="index.js src/";
|
18 |
|
19 |
|
20 | dirList=$1;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | indexOf() {
|
27 | pos="${1%%$2*}";
|
28 | [[ $pos = $1 ]] && echo -1 || echo ${#pos};
|
29 | }
|
30 |
|
31 | echoGreen() {
|
32 | echo -en '\E[32m'"\033[1m`checkMark` $1\033[0m\n\r";
|
33 | }
|
34 |
|
35 | echoYellow() {
|
36 | echo -en '\E[33m'"\033[1m$1\033[0m\n\r";
|
37 | }
|
38 |
|
39 | echoBlue() {
|
40 | echo -en '\E[34m'"\033[1m`mark` $1\033[0m\n\r";
|
41 | }
|
42 |
|
43 | echoRed() {
|
44 | echo -en '\E[31m'"\033[1m`errorMark` $1\033[0m\n\r";
|
45 | }
|
46 |
|
47 | arrowMark() {
|
48 | echo -e "\xe2\x86\x92";
|
49 | }
|
50 |
|
51 | checkMark() {
|
52 | echo -e "\xE2\x9C\x93";
|
53 | }
|
54 |
|
55 | errorMark() {
|
56 | echo -e "\xC7\x83";
|
57 | }
|
58 |
|
59 | mark() {
|
60 | echo -e "\xCB\x83 ";
|
61 | }
|
62 |
|
63 |
|
64 | lintTreeObj() {
|
65 |
|
66 | if git rev-parse --verify HEAD >/dev/null 2>&1
|
67 | then
|
68 | against=HEAD;
|
69 | else
|
70 |
|
71 | against=4b825dc642cb6eb9a060e54bf8d69288fbee4904;
|
72 | fi
|
73 |
|
74 | toBeCommited=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$");
|
75 |
|
76 | echoBlue "linting added files...";
|
77 |
|
78 |
|
79 | for file in ${toBeCommited}; do
|
80 | echo "linting $path$file";
|
81 | failed=`$JSHINT "$path$file"`;
|
82 | if [ "$failed" ]; then
|
83 | echoRed "[error] line error(s) in $1";
|
84 | echoRed "$failed";
|
85 | exit 1;
|
86 | else
|
87 | echoGreen "Passed [OK]";
|
88 | fi
|
89 | done
|
90 | }
|
91 |
|
92 | lint() {
|
93 |
|
94 | targetPath="$path$1";
|
95 |
|
96 | if [ -d "$targetPath" ] || [ -f "$targetPath" ]; then
|
97 |
|
98 | echo "linting $targetPath";
|
99 |
|
100 | failed=`$JSHINT "$targetPath"`;
|
101 | if [ "$failed" ]; then
|
102 | echoRed "[error] lint error(s) in $1";
|
103 | echoRed "$failed";
|
104 | exit 1;
|
105 | else
|
106 | echoGreen "Passed [OK]";
|
107 | fi
|
108 |
|
109 | else
|
110 | echoRed "[error] $targetPath";
|
111 | echoRed "No such file or directory ($targetPath)";
|
112 | exit 1;
|
113 | fi
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | if ! type "$JSHINT" > /dev/null; then
|
122 | echoRed "[error] jshint command is not available";
|
123 | exit 1;
|
124 | fi
|
125 |
|
126 |
|
127 | index=`indexOf "$cwd" "$name"`;
|
128 | if [ "$index" -ne -1 ]; then
|
129 | path=`expr substr $cwd 1 $index`"$name/";
|
130 | else
|
131 | path="./";
|
132 | fi
|
133 |
|
134 | echoBlue "Current working directory: $cwd";
|
135 |
|
136 | echoBlue "Root path: $path";
|
137 |
|
138 |
|
139 | if [ "$dirList" ]; then
|
140 | list=($dirList);
|
141 | else
|
142 | list=($defaultDirList);
|
143 | fi
|
144 |
|
145 | echoYellow "directories/files to lint:";
|
146 |
|
147 | for item in "${list[@]}"; do
|
148 | echoBlue "${item}";
|
149 | done
|
150 |
|
151 |
|
152 | echoYellow "Executing jshint...";
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | echoBlue "lint files in specified directories...";
|
158 |
|
159 |
|
160 | for item in "${list[@]}"; do
|
161 | lint "${item}";
|
162 | done
|
163 |
|
164 | echoYellow "Done";
|
165 |
|
166 | exit 0;
|