-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
htmlhint
146 lines (130 loc) · 3.73 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
#!/usr/bin/env node
var program = require('commander'),
fs = require('fs'),
path = require('path'),
HTMLHint = require("../index").HTMLHint;
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('0.9.4')
.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)
.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);
}
quit(processFiles(arrAllFiles, ruleset));
function listRules(){
var rules = HTMLHint.rules,
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){
configFile = '.htmlhintrc';
}
if(fs.existsSync(configFile)){
var config = fs.readFileSync(configFile, 'utf-8'),
ruleset;
try{
ruleset = JSON.parse(config);
}
catch(e){}
return ruleset;
}
}
function getAllFiles(arrTargets){
var arrAllFiles = [];
if(arrTargets.length > 0){
for(var i=0,l=arrTargets.length;i<l;i++){
getFiles(arrTargets[i], arrAllFiles);
}
}
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){
var exitcode = 0,
allHintCount = 0;
arrFiles.forEach(function(filepath){
var hintCount = hintFile(filepath, ruleset);
if(hintCount > 0){
exitcode = 1;
allHintCount += hintCount;
}
});
if(allHintCount > 0){
console.log('\r\n%d problems.'.red, allHintCount);
}
else{
console.log('No problem.'.green);
}
return exitcode;
}
function hintFile(filepath, ruleset){
var html = fs.readFileSync(filepath, 'utf-8');
var messages = HTMLHint.verify(html, ruleset);
if(messages.length > 0){
console.log(filepath+':');
messages.forEach(function(hint){
console.log('\tline %d, col %d: %s', hint.line, hint.col, hint.message[hint.type === 'error'?'red':'yellow']);
});
console.log('');
}
return messages.length;
}
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);
}
}