-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (54 loc) · 1.66 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
var gutil = require('gulp-util'),
PJV = require('package-json-validator').PJV,
through = require('through2');
function printErrors(results) {
if (results) {
if (results.critical) {
gutil.log(gutil.colors.red('Critical Error: ' + results.critical));
}
if (results.errors) {
gutil.log(gutil.colors.underline.red('Errors:'));
results.errors.forEach(function (error) {
gutil.log(' ' + gutil.colors.red(error));
});
}
if (results.warnings) {
gutil.log(gutil.colors.underline.yellow('Warnings:'));
results.warnings.forEach(function (warn) {
gutil.log(' ' + gutil.colors.yellow(warn));
});
}
if (results.recommendations) {
gutil.log(gutil.colors.underline('Recommendations:'));
results.recommendations.forEach(function (rec) {
gutil.log(' ' + rec);
});
}
if (results.valid) {
gutil.log(gutil.colors.green('package.json is valid'));
} else {
gutil.log(gutil.colors.red('package.json is INVALID'));
}
} else {
throw new gutil.PluginError('gulp-nice-package', 'Failed to get results from validator');
}
}
module.exports = function (spec, options) {
function validate(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-nice-package', 'Streaming not supported'));
return cb();
}
var results = PJV.validate(file.contents.toString(), spec, options);
printErrors.call(this, results);
file.nicePackage = results;
this.push(file);
cb();
}
return through.obj(validate);
};