UNPKG

grunt-yamllint

Version:

Linting YAML files for correctness using JS-YAML.

70 lines (63 loc) 1.87 kB
'use strict'; module.exports = function(grunt) { var yaml = require('js-yaml'); var os = require('os'); var fs = require('fs'); var async = require('async'); var chalk = require('chalk'); grunt.registerMultiTask('yamllint', 'Linting YAML files for correctness.',function(){ var done = this.async(); var files = this.filesSrc; var options = this.options({ force: false, filename: null, onWarning: null, schema: null, reporterOutput: null }); var pass = true; var force = options.force; if(yaml[options.schema]) { grunt.verbose.writeln(chalk.yellow('Using ' + options.schema + ' schema')); options.schema = yaml[options.schema]; } else { grunt.verbose.writeln(chalk.yellow('Using default schema')); options.schema = yaml.DEFAULT_SAFE_SCHEMA; } async.eachLimit(files, os.cpus().length, function(file,next) { fs.readFile(file,'utf8',function(err, content) { if(err) { grunt.log.writeln('err'); grunt.error(err); done(force); return process.nextTick(next); } var current_pass = true; var doc; try { // console.log(JSON.stringify(options)); yaml.loadAll(content, function(content) { doc += content; },options); } catch(e) { grunt.log.writeln(chalk.yellow('An error has occured in:' + file)); grunt.log.writeln(chalk.red(e)); pass = false; current_pass = false; } if(current_pass) { grunt.verbose.writeln(chalk.green('✔ ') + file + chalk.gray(' ( valid YAML )')); } return process.nextTick(next); }); }, function(err){ if(err) { grunt.warn(err); } if(pass) { grunt.log.writeln(chalk.green('✔ ') + chalk.gray(' All files valid YAML ')); } done(force?true:pass); }); }); };