Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding options for logging errors to a file #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ module.exports = function(grunt) {
all: {
options: {
'tag-pair': true,
'htmlhintrc': 'test/.htmlhintrc'
'htmlhintrc': 'test/.htmlhintrc',
'force': true
},
src: 'test/fixtures/*.html'
},
file: {
options: {
'tag-pair': true,
'htmlhintrc': 'test/.htmlhintrc',
'reporter': 'reporters/file.js',
'reporterOutput': 'tmp/htmlhint.txt',
'force': true
},
src: 'test/fixtures/*.html'
}
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ Default value: `false`

Report HTMLHint errors but dont fail the task

#### options.reporter
Type: `string`
Default value: '/reporters/console.js'
Available values: '/reporters/console.js', '/reporters/file.js', 'path/to/your/custom/reporter.js'

Using the default value will print errors to the console.

The `reporters/file.js` option will log errors to a file.

#### options.reporterOutput
Type: `string`
Default value: undefined

The path path to the log file you would like errors to be logged to.

This is required if you are using the `/reporters/file.js` option.

### Usage Examples

#### Direct options
Expand Down Expand Up @@ -83,6 +100,20 @@ htmlhint: {
}
```

#### Using a Custom Reporter

```js
htmlhint: {
options: {
'reporter': 'reporters/file.js',
'reporterOutput': 'tmp/htmlhint.txt',
},
html1: {
src: ['path/to/**/*.html']
}
}
```

## Release History

* 2013-4-6   v0.4.0   First release
19 changes: 19 additions & 0 deletions reporters/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var grunt = require('grunt');

module.exports = {
reporter: function(message) {
grunt.log.writeln( "[".red + ( "L" + message.line ).yellow + ":".red + ( "C" + message.col ).yellow + "]".red + ' ' + message.message.yellow );
var evidence = message.evidence,
col = message.col;
if (col === 0) {
evidence = '?'.inverse.red + evidence;
} else if (col > evidence.length) {
evidence = evidence + ' '.inverse.red;
} else {
evidence = evidence.slice(0, col - 1) + evidence[col - 1].inverse.red + evidence.slice(col);
}
grunt.log.writeln(evidence);
}
};
15 changes: 15 additions & 0 deletions reporters/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var appendFileSync = require('fs').appendFileSync,
grunt = require('grunt');

module.exports = {
reporter: function(message, reporterOutput) {

var err = "[" + ( "L" + message.line ) + ":" + ( "C" + message.col ) + "]" + ' ' + message.message,
evidence = message.evidence;

appendFileSync(reporterOutput, '\n\t' + err + '\n\t' + evidence + '\n');

}
};
24 changes: 10 additions & 14 deletions tasks/htmlhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ module.exports = function(grunt) {

var HTMLHint = require("htmlhint").HTMLHint;
var options = this.options({
force: false
}),
force: false,
reporter: 'reporters/console.js'
}),
arrFilesSrc = this.filesSrc,
verbose = grunt.verbose;

Expand All @@ -39,22 +40,17 @@ module.exports = function(grunt) {
if (messages.length > 0) {
verbose.or.write( msg );
grunt.log.error();
if (options.reporterOutput) {
var appendFileSync = require('fs').appendFileSync;
appendFileSync(options.reporterOutput, '[file] ' + filepath + '\n');
}
} else {
verbose.ok();
}
messages.forEach(function( message ) {
grunt.log.writeln( "[".red + ( "L" + message.line ).yellow + ":".red + ( "C" + message.col ).yellow + "]".red + ' ' + message.message.yellow );
var evidence = message.evidence,
col = message.col;
if (col === 0) {
evidence = '?'.inverse.red + evidence;
} else if (col > evidence.length) {
evidence = evidence + ' '.inverse.red;
} else {
evidence = evidence.slice(0, col - 1) + evidence[col - 1].inverse.red + evidence.slice(col);
}
grunt.log.writeln(evidence);
hintCount ++;
var output = require(options.reporter);
output.reporter(message, options.reporterOutput);
hintCount++;
});
}
else{
Expand Down