forked from sasstools/gulp-sass-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (89 loc) · 2.42 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
//////////////////////////////
// Sass Lint
// - A Gulp Plugin
//
// Lints Sass files
//////////////////////////////
'use strict';
//////////////////////////////
// Variables
//////////////////////////////
var through = require('through2'),
gutil = require('gulp-util'),
lint = require('sass-lint'),
path = require('path'),
PluginError = gutil.PluginError,
PLUGIN_NAME = 'sass-lint';
//////////////////////////////
// Export
//////////////////////////////
var sassLint = function (options) {
options = options || {};
var compile = through.obj(function (file, encoding, cb) {
var config;
if (file.isNull()) {
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
config = lint.getConfig(options);
file.sassConfig = config;
try {
file.sassLint = [lint.lintText({
'text': file.contents,
'format': path.extname(file.path).replace('.', ''),
'filename': path.relative(process.cwd(), file.path)
}, config)];
} catch(e) {
this.emit('error', new PluginError(PLUGIN_NAME, e.message));
}
this.push(file);
cb();
});
return compile;
}
sassLint.format = function () {
var compile = through.obj(function (file, encoding, cb) {
if (file.isNull()) {
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
lint.outputResults(file.sassLint, file.sassConfig);
this.push(file);
cb();
});
return compile;
}
sassLint.failOnError = function () {
var filesWithErrors = [];
var compile = through({objectMode: true}, function (file, encoding, cb) {
if (file.isNull()) {
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
if (file.sassLint[0].errorCount > 0) {
filesWithErrors.push(file);
}
this.push(file);
cb();
}, function (cb) {
var errorMessage;
if (filesWithErrors.length > 0) {
errorMessage = filesWithErrors.map(function (file) {
return file.sassLint[0].errorCount + ' errors detected in ' + file.relative
}).join('\n');
this.emit('error', new PluginError(PLUGIN_NAME, errorMessage));
}
cb();
});
return compile;
}
module.exports = sassLint;