-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
36 lines (31 loc) · 1.04 KB
/
index.js
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
var gulp = require('gulp')
var eslint = require('gulp-eslint')
var gulpif = require('gulp-if')
var path = require('path')
var watch = require('gulp-watch')
function handleResult(result) {
result.messages.forEach(function(message) {
if (message.fatal) {
console.error(message.message + ' on line ' + message.line + ' of ' + result.filePath)
}
})
}
function fixLintableFile(vinyl) {
if (vinyl.contents) {
gulp.src(vinyl.path)
.on('error', function() {}) // Prevent a crash. Let eslint.result report the error.
.pipe(eslint({fix: true}))
.pipe(eslint.result(handleResult))
.pipe(gulpif(wasFixedByEslint, gulp.dest(path.dirname(vinyl.path))))
}
}
function wasFixedByEslint(vinyl) {
return vinyl.eslint && vinyl.eslint.fixed
}
module.exports = function(taskName, globsToWatch) {
taskName = taskName || 'eslint-auto-fix'
globsToWatch = globsToWatch || ['**/*.js', '!**/node_modules', '!**/bower_components']
return gulp.task(taskName, function() {
return watch(globsToWatch, fixLintableFile)
})
}