UNPKG

3.13 kBapplication/x-shView Raw
1#!/bin/sh
2
3###################
4# variables
5###################
6
7# You can customize the jshint location using the JSHINT variable.
8# e.g. JSHINT=./node_modules/.bin/jshint scripts/lint/lint.sh
9# if you want to use a local version of jshint.
10if [ -z "$JSHINT" ]; then
11 JSHINT="jshint"
12fi
13name="node-datetime";
14cwd=`pwd`;
15# list directories/files to lint
16list=();
17defaultDirList="index.js src/";
18# optional space separated list of directories/files to lint
19# Example: ./lint.sh "mydir/ myFile" > this will lint all files in mydir/ and lint myFile
20dirList=$1;
21
22##################
23# functions
24##################
25
26indexOf() {
27 pos="${1%%$2*}";
28 [[ $pos = $1 ]] && echo -1 || echo ${#pos};
29}
30
31echoGreen() {
32 echo -en '\E[32m'"\033[1m`checkMark` $1\033[0m\n\r";
33}
34
35echoYellow() {
36 echo -en '\E[33m'"\033[1m$1\033[0m\n\r";
37}
38
39echoBlue() {
40 echo -en '\E[34m'"\033[1m`mark` $1\033[0m\n\r";
41}
42
43echoRed() {
44 echo -en '\E[31m'"\033[1m`errorMark` $1\033[0m\n\r";
45}
46
47arrowMark() {
48 echo -e "\xe2\x86\x92";
49}
50
51checkMark() {
52 echo -e "\xE2\x9C\x93";
53}
54
55errorMark() {
56 echo -e "\xC7\x83";
57}
58
59mark() {
60 echo -e "\xCB\x83 ";
61}
62
63# probably pointless...
64lintTreeObj() {
65 # lint the code to be commited
66 if git rev-parse --verify HEAD >/dev/null 2>&1
67 then
68 against=HEAD;
69 else
70 # Initial commit: diff against an empty tree object
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 # lint JavaScript files only
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
92lint() {
93 # lint the code in the specified directories (this may not include added files to git)
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# procedural codes
118##########################
119
120# test if jshtin command is avialable
121if ! type "$JSHINT" > /dev/null; then
122 echoRed "[error] jshint command is not available";
123 exit 1;
124fi
125
126# find root path
127index=`indexOf "$cwd" "$name"`;
128if [ "$index" -ne -1 ]; then
129 path=`expr substr $cwd 1 $index`"$name/";
130else
131 path="./";
132fi
133
134echoBlue "Current working directory: $cwd";
135
136echoBlue "Root path: $path";
137
138# find directories/files to lint
139if [ "$dirList" ]; then
140 list=($dirList);
141else
142 list=($defaultDirList);
143fi
144
145echoYellow "directories/files to lint:";
146
147for item in "${list[@]}"; do
148 echoBlue "${item}";
149done
150
151# start linting
152echoYellow "Executing jshint...";
153
154# lint the files in git tree
155#lintTreeObj "";
156
157echoBlue "lint files in specified directories...";
158
159# lint
160for item in "${list[@]}"; do
161 lint "${item}";
162done
163
164echoYellow "Done";
165
166exit 0;