-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
htmlhint
executable file
·196 lines (175 loc) · 5.44 KB
/
htmlhint
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
var program = require('commander');
var fs = require('fs');
var path = require('path');
var stripJsonComments = require('strip-json-comments');
var HTMLHint = require("../index").HTMLHint;
var pkg = require('../package.json');
require('colors');
function map(val) {
var objMap = {};
val.split(',').forEach(function(item){
var arrItem = item.split(/\s*=\s*/);
objMap[arrItem[0]] = arrItem[1]?arrItem[1]:true;
});
return objMap;
}
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' htmlhint -l');
console.log(' htmlhint -r tag-pair,id-class-value=underline test.html');
console.log(' htmlhint -c .htmlhintrc test.html');
console.log('');
});
program
.version(pkg.version)
.usage('[options] <file ...>')
.option('-l, --list', 'show all of the rules available.')
.option('-c, --config <file>', 'custom configuration file.')
.option('-r, --rules <ruleid, ruleid=value ...>', 'set all of the rules available.', map)
.option('-j, --json', 'output messages as raw JSON')
.parse(process.argv);
if(program.list){
listRules();
quit(0);
}
var arrAllFiles = getAllFiles(program.args);
var ruleset = program.rules;
if(ruleset === undefined){
ruleset = getConfig(program.config);
}
var jsonOutput = program.json && [];
quit(processFiles(arrAllFiles, ruleset, jsonOutput));
function listRules(){
var rules = HTMLHint.rules;
var rule;
console.log('\r\nAll rules:');
console.log('======================================');
for (var id in rules){
rule = rules[id];
console.log('\r\n'+rule.id+' :');
console.log(' '+rule.description);
}
}
function getConfig(configFile){
if(configFile === undefined){
// find default config file in parent directory
var curDir = process.cwd();
while(curDir){
var tmpConfigFile = path.resolve(curDir+path.sep, '.htmlhintrc');
if(fs.existsSync(tmpConfigFile)){
configFile = tmpConfigFile;
break;
}
curDir = curDir.substring(0,curDir.lastIndexOf(path.sep));
}
}
if(fs.existsSync(configFile)){
var config = fs.readFileSync(configFile, 'utf-8'),
ruleset;
try{
ruleset = JSON.parse(stripJsonComments(config));
}
catch(e){}
return ruleset;
}
}
function getAllFiles(arrTargets){
var arrAllFiles = [];
if(arrTargets.length > 0){
for(var i=0,l=arrTargets.length;i<l;i++){
var filepath = path.resolve(process.cwd(), arrTargets[i]);
if(fs.existsSync(filepath) !== false){
if(fs.statSync(filepath).isFile()) {
arrAllFiles.push(filepath);
} else {
getFiles(arrTargets[i], arrAllFiles);
}
} else {
console.log('File %s does not exist'.red, arrTargets[i]);
}
}
}
else{
getFiles(process.cwd(), arrAllFiles);
}
return arrAllFiles;
}
function getFiles(filepath, arrFiles){
if(fs.existsSync(filepath) === false){
return;
}
filepath = path.resolve(process.cwd(), filepath);
var stat = fs.statSync(filepath);
if(stat.isFile() && /\.html?$/i.test(filepath)){
arrFiles.push(filepath);
}
else if(stat.isDirectory()){
fs.readdirSync(filepath).forEach(function(filename){
getFiles(filepath + '/' + filename, arrFiles);
});
}
}
function processFiles(arrFiles, ruleset, jsonOutput){
var exitcode = 0;
var allFileCount = 0;
var allHintCount = 0;
console.log('');
arrFiles.forEach(function(filepath){
var hintCount = hintFile(filepath, ruleset, jsonOutput);
if(hintCount > 0){
exitcode = 1;
allFileCount ++;
allHintCount += hintCount;
}
});
if(jsonOutput){
console.log(JSON.stringify(jsonOutput));
}
else{
if(allHintCount > 0){
console.log('%d errors in %d files'.red, allHintCount, allFileCount);
}
else{
console.log('Done, without errors.'.green);
}
}
return exitcode;
}
function hintFile(filepath, ruleset, jsonOutput){
var html = fs.readFileSync(filepath, 'utf-8');
var messages = HTMLHint.verify(html, ruleset);
if(messages.length > 0){
if(jsonOutput){
logJson(filepath, messages, jsonOutput);
}
else{
logPretty(filepath, messages);
}
}
return messages.length;
}
function logPretty(filepath, messages){
console.log(' '+path.relative(process.cwd(), filepath));
messages.forEach(function(hint){
console.log(' L%d |%s', hint.line, hint.evidence.replace(/\t/g, ' ').gray);
console.log(' %s^ %s', repeatStr(String(hint.line).length + 3 + hint.col - 1), (hint.message + ' (' + hint.rule.id+')')[hint.type === 'error'?'red':'yellow']);
});
console.log('');
}
function repeatStr(n, str){
return new Array(n + 1).join(str || ' ');
}
function logJson(filepath, messages, jsonOutput){
jsonOutput.push({'file': filepath, 'messages': messages});
}
function quit(code){
if ((!process.stdout.flush || !process.stdout.flush()) && (parseFloat(process.versions.node) < 0.5)) {
process.once("drain", function () {
process.exit(code || 0);
});
} else {
process.exit(code || 0);
}
}