gulp-html-vnu-validator
Version:
Validate HTML with vnu.jar locally
70 lines (55 loc) • 1.86 kB
JavaScript
;
/**
* Created by g.yanev on 03.02.2017.
*/
const vnu = require('vnu-jar');
const gutil = require("gulp-util");
const PluginError = gutil.PluginError;
const through = require("through2");
const path = require("path");
const sh = require("shelljs");
const PLUGIN_NAME = "gulp-html-vnu-validator";
function colorizeType(type) {
switch (type) {
case "info" :
return gutil.colors.blue(type);
break;
default:
return gutil.colors.red(type);
}
}
function validate(config) {
var options = config || {};
return through.obj(function (file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isBuffer()) {
var stackSize = options.stackSize || "-Xss1m";
try {
var output = JSON.parse(sh.exec("java " + stackSize + " -jar \"" + vnu + "\" --format json \"" + file.path + "\"", {silent: true}).stderr);
} catch (Exception) {
gutil.log(gutil.colors.red("The JSON couldn't be parsed. Please try to increase the stackSize option."));
return false;
}
if (output.messages.length > 0) {
gutil.log(gutil.colors.red("[HTML5 Validator - Problem(s) in"), file.path, "]");
var errors = output.messages;
for (var error in errors) {
var err_info = errors[error];
gutil.log(colorizeType(err_info.type), err_info.message, "Line: " + err_info.lastLine);
}
} else {
gutil.log(gutil.colors.green("[HTML5 Validator - No problems]"), file.path);
}
if (options.newLines) {
console.log("\n");
}
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, "Streams are not yet supported");
}
callback(null, file);
});
}
module.exports = validate;