-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
107 lines (87 loc) · 2.19 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
var mapStream = require('map-stream')
var colors = require('ansi-colors')
var jsonlint = require('jsonlint')
var through = require('through2')
var PluginError = require('plugin-error')
var log = require('fancy-log')
var formatOutput = function(msg) {
var output = {}
if (msg) {
output.message = msg
}
output.success = msg ? false : true
return output
}
var jsonLintPlugin = function(options) {
options = options || {}
return mapStream(function(file, cb) {
var errorMessage = ''
try {
jsonlint.parse(String(file.contents))
} catch (err) {
errorMessage = err.message
}
file.jsonlint = formatOutput(errorMessage)
cb(null, file)
})
}
var defaultReporter = function(file) {
log(colors.yellow('Error on file ') + colors.magenta(file.path))
log(colors.red(file.jsonlint.message))
}
jsonLintPlugin.reporter = function(customReporter) {
var reporter = defaultReporter
if (typeof customReporter === 'function') {
reporter = customReporter
}
return mapStream(function(file, cb) {
if (file.jsonlint && !file.jsonlint.success) {
reporter(file)
}
return cb(null, file)
})
}
/**
* Fail when an jsonlint error is found in jsonlint results.
*/
jsonLintPlugin.failOnError = function() {
return through.obj(function(file, enc, cb) {
if (file.jsonlint.success === false) {
var error = new PluginError('gulp-jsonlint', {
name: 'JSONLintError',
filename: file.path,
message: file.jsonlint.message
})
}
return cb(error, file)
})
}
/**
* Fail when the stream ends if any jsonlint error(s) occurred
*/
jsonLintPlugin.failAfterError = function() {
var errorCount = 0
return through.obj(
function(file, enc, cb) {
errorCount += file.jsonlint.success === false
cb(null, file)
},
function(cb) {
if (errorCount > 0) {
this.emit(
'error',
new PluginError('gulp-jsonlint', {
name: 'JSONLintError',
message:
'Failed with ' +
errorCount +
(errorCount === 1 ? ' error' : ' errors')
})
)
}
cb()
}
)
}
module.exports = jsonLintPlugin