Skip to content

Commit

Permalink
add hint stdin for cli
Browse files Browse the repository at this point in the history
fix #97
  • Loading branch information
yaniswang committed May 1, 2016
1 parent 67c8922 commit 8560332
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add:
4. add cli parameter:`--nocolor`, disable color in cli
5. space-tab-mixed-disabled plugin: add space length require
6. add empty elements: track,command,source,keygen,wbr
7. add hint stdin for cli

fix:

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Quick start
htmlhint www/test.html
htmlhint www/**/*.xhtml
htmlhint www/**/*.{htm,html}
cat test.html | htmlhint stdin

2. results

Expand Down Expand Up @@ -73,7 +74,7 @@ HTMLHint is released under the MIT license:

> The MIT License
>
> Copyright (c) 2014-2015 Yanis Wang \< yanis.wang@gmail.com \>
> Copyright (c) 2014-2016 Yanis Wang \< yanis.wang@gmail.com \>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
80 changes: 55 additions & 25 deletions bin/htmlhint
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ program.on('--help', function(){
console.log(' htmlhint www/test.html');
console.log(' htmlhint www/**/*.xhtml');
console.log(' htmlhint www/**/*.{htm,html}');
console.log(' cat test.html | htmlhint stdin');
console.log(' htmlhint --list');
console.log(' htmlhint --rules tag-pair,id-class-value=underline test.html');
console.log(' htmlhint --config .htmlhintrc test.html');
Expand Down Expand Up @@ -190,36 +191,37 @@ function hintAllFiles(target, options, onFinised){
// hint queue
var hintQueue = async.queue(function (filepath, next) {
var startTime = new Date().getTime();
var messages = hintFile(filepath, ruleset);
var spendTime = new Date().getTime() - startTime;
var hintCount = messages.length;
if(hintCount > 0){
formatter.emit('file', {
'file': filepath,
'messages': messages,
'time': spendTime
});
arrTargetMessages.push({
'file': filepath,
'messages': messages,
'time': spendTime
});
targetHintFileCount ++;
targetHintCount += hintCount;
if(filepath === 'stdin'){
hintStdin(ruleset, hintNext);
}
else{
var messages = hintFile(filepath, ruleset);
hintNext(messages);
}
function hintNext(messages){
var spendTime = new Date().getTime() - startTime;
var hintCount = messages.length;
if(hintCount > 0){
formatter.emit('file', {
'file': filepath,
'messages': messages,
'time': spendTime
});
arrTargetMessages.push({
'file': filepath,
'messages': messages,
'time': spendTime
});
targetHintFileCount ++;
targetHintCount += hintCount;
}
targetFileCount ++;
setImmediate(next);
}
targetFileCount ++;
setImmediate(next);
}, 10);
// start hint
var isWalkDone = false;
var isHintDone = true;
walkPath(globInfo, function(filepath){
isHintDone = false;
hintQueue.push(filepath);
}, function(){
isWalkDone = true;
checkAllHinted();
});
hintQueue.drain = function() {
isHintDone = true;
checkAllHinted();
Expand All @@ -234,6 +236,19 @@ function hintAllFiles(target, options, onFinised){
});
}
}
if(target === 'stdin'){
isWalkDone = true;
hintQueue.push(target);
}
else{
walkPath(globInfo, function(filepath){
isHintDone = false;
hintQueue.push(filepath);
}, function(){
isWalkDone = true;
checkAllHinted();
});
}
}

// split target to base & glob
Expand Down Expand Up @@ -337,3 +352,18 @@ function hintFile(filepath, ruleset){
catch(e){}
return HTMLHint.verify(content, ruleset);
}

// hint stdin
function hintStdin(ruleset, callback){
process.stdin.setEncoding('utf8');
var buffers = [];
process.stdin.on('data', function(text){
buffers.push(text);
});

process.stdin.on('end', function(){
var content = buffers.join('');
var messages = HTMLHint.verify(content, ruleset);
callback(messages);
});
}

0 comments on commit 8560332

Please sign in to comment.