grunt-closure-linter
Advanced tools
Comparing version 0.1.0 to 0.1.3
{ | ||
"name": "grunt-closure-linter", | ||
"description": "Google closure linting", | ||
"version": "0.1.0", | ||
"version": "0.1.3", | ||
"homepage": "https://github.com/wzr1337/grunt-closure-linter", | ||
@@ -10,2 +10,6 @@ "author": { | ||
}, | ||
"contributors": [{ | ||
"name": "Steven Lu", | ||
"email": "stevenlu443@gmail.com" | ||
}], | ||
"repository": { | ||
@@ -40,2 +44,2 @@ "type": "git", | ||
] | ||
} | ||
} |
@@ -23,3 +23,3 @@ # grunt-closure-linter | ||
### Overview | ||
In your project's Gruntfile, add a section named `closure_linter` to the data object passed into `grunt.initConfig()`. | ||
In your project's Gruntfile, add a section named `closureLint` and/or `closureFixStyle` to the data object passed into `grunt.initConfig()`. | ||
@@ -29,25 +29,25 @@ ```js | ||
closureLint: { | ||
app:{ | ||
closureLinterPath : '/path/to/closure_linter', | ||
src: [ 'app/scripts/controllers/**', | ||
'app/scripts/services/**', | ||
'app/scripts/app.js' ], | ||
options: { | ||
stdout: true, | ||
strict: true | ||
} | ||
app:{ | ||
closureLinterPath : '/path/to/closure_linter/folder', | ||
src: [ 'app/scripts/controllers/**', | ||
'app/scripts/services/**', | ||
'app/scripts/app.js' ], | ||
options: { | ||
stdout: true, | ||
strict: true | ||
} | ||
}, | ||
closureFixStyle: { | ||
app:{ | ||
closureLinterPath : '/path/to/closure_linter', | ||
src: [ 'app/scripts/controllers/**', | ||
'app/scripts/services/**', | ||
'app/scripts/app.js' ], | ||
options: { | ||
stdout: true, | ||
strict: true | ||
} | ||
} | ||
}, | ||
closureFixStyle: { | ||
app:{ | ||
closureLinterPath : '/path/to/closure_linter/folder', | ||
src: [ 'app/scripts/controllers/**', | ||
'app/scripts/services/**', | ||
'app/scripts/app.js' ], | ||
options: { | ||
stdout: true, | ||
strict: true | ||
} | ||
}, | ||
} | ||
} | ||
}) | ||
@@ -59,6 +59,8 @@ ``` | ||
#### options.stdout | ||
Type: `Boolean` | ||
Type: `mixed` | ||
Default value: `false` | ||
Pipe output to stdout. | ||
- `true` – write results to stdout; | ||
- `string` – write results to file specified; | ||
- `false` – do not write anything. | ||
@@ -77,3 +79,3 @@ #### options.stderr | ||
#### options.strickt | ||
#### options.strict | ||
Type: `Boolean` | ||
@@ -84,2 +86,8 @@ Default value: `false` | ||
#### options.reporter | ||
Type: `string` | ||
Default value: `'closure'` | ||
Reporter type. Possible values are: `'closure'`, `'jslint'`. | ||
## Contributing | ||
@@ -86,0 +94,0 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). |
@@ -20,3 +20,3 @@ /** | ||
grunt.registerMultiTask('closureFixStyle', 'Apply closure style fixes', | ||
function() {ctools.registerTool(this, 'fixjsstyle.py ');}); | ||
function() {ctools.registerTool(this, 'fixjsstyle.py');}); | ||
}; |
@@ -21,3 +21,3 @@ /** | ||
grunt.registerMultiTask('closureLint', 'Apply closure linting', | ||
function() {ctools.registerTool(this, 'gjslint.py ');}); | ||
function() {ctools.registerTool(this, 'gjslint.py');}); | ||
}; |
@@ -7,6 +7,7 @@ /** | ||
exec = require('child_process').exec, | ||
path = require('path'), | ||
_ = grunt.util._; | ||
module.exports.registerTool = function(task, toolname) { | ||
var callback = task.async(), | ||
var done = task.async(), | ||
files = [], | ||
@@ -16,7 +17,7 @@ closureLinterPath = task.data.closureLinterPath, | ||
{ | ||
stdout : false, | ||
stdout : true, | ||
stderr : false, | ||
failOnError : true, | ||
strict : false | ||
}); | ||
}); | ||
// Iterate over all src-dest file pairs. | ||
@@ -27,25 +28,97 @@ task.files.forEach(function(f) { | ||
//grunt.log.writeflags(options, 'options'); | ||
var cmd = closureLinterPath + '/' + toolname + files.join(' '); | ||
cmd += options.strict ? ' --strict' : ''; | ||
var cmd = closureLinterPath + '/' + toolname; | ||
cmd += options.strict ? ' --strict ' : ' '; | ||
// add commands to send to gjslint from option called opt | ||
cmd += options.opt + ' '; | ||
cmd += files.join(' '); | ||
//grunt.log.writeln(cmd); | ||
var task = exec(cmd, options.execOptions, function(err, stdout, stderr) { | ||
// Whether to output the report to a file or stdout | ||
var outputResults = options.stdout; | ||
delete options.stdout; | ||
var reportFormat = options.reporter || 'closure'; | ||
delete options.reporter; | ||
var convertResults; | ||
if (0 === toolname.indexOf('gjslint') && reportFormat) { | ||
convertResults = Converter[reportFormat.toUpperCase()]; | ||
} | ||
var linterCallback = function(error, stdout, stderr) { | ||
if (!error && outputResults) { | ||
writeResults(stdout, outputResults); | ||
} | ||
if (_.isFunction(options.callback)) { | ||
options.callback.call(task, err, stdout, stderr, callback); | ||
options.callback.call(task, error, stdout, stderr, done); | ||
} else { | ||
if (err && options.failOnError) { | ||
grunt.warn(err); | ||
if (error && options.failOnError) { | ||
writeResults(stdout, null); | ||
grunt.warn(error); | ||
} | ||
callback(); | ||
done(); | ||
} | ||
}; | ||
var child = exec(cmd, options.execOptions, function(error, stdout, stderr) { | ||
convertResults ? | ||
convertResults(stdout, linterCallback) : | ||
linterCallback.apply(null, arguments); | ||
}); | ||
if (options.stdout) { | ||
task.stdout.pipe(process.stdout); | ||
if (options.stderr) { | ||
child.stderr.pipe(process.stderr); | ||
} | ||
}; | ||
if (options.stderr) { | ||
task.stderr.pipe(process.stderr); | ||
/** | ||
* Writes linter |results| to file or stdout if file path is not provided. | ||
* @param {!string} results Linter results as a string. | ||
* @param {string=} opt_filePath File to write results into. Optional. | ||
*/ | ||
function writeResults(results, opt_filePath) { | ||
if (typeof opt_filePath === 'string') { | ||
var filePath = opt_filePath; | ||
filePath = grunt.template.process(filePath); | ||
var destDir = path.dirname(filePath); | ||
if (!grunt.file.exists(destDir)) { | ||
grunt.file.mkdir(destDir); | ||
} | ||
}; | ||
grunt.file.write(filePath, results); | ||
grunt.log.ok('File "' + filePath + '" created.'); | ||
} | ||
else { | ||
process.stdout.write(results); | ||
} | ||
} | ||
/** | ||
* Results format converters. | ||
* @enum {function|undefined} | ||
*/ | ||
var Converter = { | ||
CLOSURE: undefined, // Conversion is not needed. | ||
JSLINT: jslintConverter | ||
}; | ||
/** | ||
* Folder with converter scripts. | ||
* @const | ||
* @type {string} | ||
*/ | ||
var CONVERTERS_ROOT_DIR = path.join(__dirname, '..', '..', 'converters'); | ||
/** | ||
* Converts Closure Linter results to JSLint XML format. | ||
* @param {string} results Closure Linter results string. | ||
* @param {!function} callback Operation callback. | ||
*/ | ||
function jslintConverter(results, callback) { | ||
var cmd = path.join(CONVERTERS_ROOT_DIR, 'jslint.py'); | ||
var converterProcess = exec(cmd, null, callback); | ||
converterProcess.stdin.end(results); | ||
} |
13353
10
275
93