-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.js
121 lines (99 loc) · 2.97 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//////////////////////////////
// Sass Lint
// - A Gulp Plugin
//
// Lints Sass files
//////////////////////////////
'use strict';
//////////////////////////////
// Variables
//////////////////////////////
var through = require('through2'),
lint = require('sass-lint'),
path = require('path'),
PluginError = require('plugin-error'),
PLUGIN_NAME = 'sass-lint';
//////////////////////////////
// Export
//////////////////////////////
var sassLint = function (options) {
var userOptions = options || {};
var configFile = userOptions.configFile;
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();
}
// load our config from sassLint and the user provided options if available
file.sassConfig = lint.getConfig(userOptions, configFile);
// save the config file within the file object for access when this file is piped around
file.userOptions = userOptions;
file.configFile = configFile;
// lint the file and pass the user defined options and config path to sass lint to handle
try {
file.sassLint = [
lint.lintFileText({
'text': file.contents,
'format': path.extname(file.path).replace('.', ''),
'filename': path.relative(process.cwd(), file.path)
}, userOptions, configFile)];
} catch(e) {
this.emit('error', new PluginError(PLUGIN_NAME, e.message));
}
this.push(file);
cb();
});
return compile;
}
sassLint.format = function (writable) {
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();
}
if (writable) {
var result = lint.format(file.sassLint, file.userOptions, file.configFile);
writable.write(result);
}
else {
lint.outputResults(file.sassLint, file.userOptions, file.configFile);
}
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;