Last active
September 15, 2016 15:02
-
-
Save atsu85/9889fed776f4f6301278fcd05d2f9727 to your computer and use it in GitHub Desktop.
gulp task `lint-templates` to lint Aurelia templates (also shows UI notification when there is a problem or when all problems are fixed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var linter = require('gulp-aurelia-template-lint'); | |
var config = new (require('aurelia-template-lint').Config); | |
var fail = require('gulp-fail'); | |
var gulpIf = require('gulp-if'); | |
var gutil = require('gulp-util'); | |
var notify = require("gulp-notify"); | |
var paths = require('../paths'); | |
config.useRuleAureliaBindingAccess = true; | |
config.reflectionOpts.sourceFileGlob = "aurelia/src/**/*.ts"; | |
config.reflectionOpts.typingsFileGlob = "typings/**/*.d.ts"; | |
config.aureliaBindingAccessOpts.restrictedAccess = ["private"]; // using "protected" for fields that are used in html | |
/* | |
config.debug = true; | |
*/ | |
var hadErrors = false; // used when task is executed many times (`gulp watch`) | |
gulp.task('lint-templates', function() { | |
var errorsCount = 0; | |
var reporter = (error, file) => { | |
if (error.severity > 1) { | |
errorsCount++; | |
} | |
var [severity, color] = getSeverityText(error); | |
var msg = `${severity}: ${error.message} - ${file}(${error.line}:${error.column})`; | |
gutil.log(color(msg)); | |
}; | |
return gulp.src(paths.src.html) | |
.pipe(linter(config, reporter)) | |
.pipe( | |
gulpIf( | |
() => { | |
return errorsCount !== 0; | |
}, | |
fail( | |
() => { | |
hadErrors = true; | |
const msg = `Found ${errorsCount} errors when linting HTML templates`; | |
notify({ | |
title: "gulp lint-templates", | |
message: msg, | |
}).write(''); | |
return msg; | |
}, | |
true | |
) | |
) | |
) | |
.on('end', function() { | |
if (hadErrors && errorsCount === 0) { | |
hadErrors = false; | |
notify({ | |
title: "gulp lint-templates", | |
message: "all errors fixed", | |
}).write(''); | |
hadErrors = false; | |
} | |
}); | |
}); | |
function getSeverityText(issue) { | |
if (issue.severity === 0) { | |
return ["INFO", gutil.colors.green]; | |
} else if (issue.severity === 1) { | |
return ["WARNING", gutil.colors.yellow]; | |
} else if (issue.severity === 2) { | |
return ["ERROR", gutil.colors.red]; | |
} else if (issue.severity === 3) { | |
return ["FATAL", gutil.colors.magneta]; | |
} else { | |
return ["SEVERITY " + issue.severity, gutil.colors.red]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment