-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (109 loc) · 3.39 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
122
123
124
125
126
const through = require("through2");
const PluginError = require("plugin-error");
const Solium = require("solium");
const path = require("path");
const process = require("process");
const flatten = require("array-flatten");
const fs = require("fs");
const PLUGIN_NAME = "gulp-solium";
/**
* Append Solium result to each file
*
* @param {Object} [userSettings] Configure rules for running Solium
* @returns {Stream} gulp file stream
*/
function gulpSolium(userSettings) {
return through.obj((file, encoding, callback) => {
if (file.isNull()) {
// nothing to do
return callback(null, file);
}
if (file.isStream()) {
callback(new PluginError(PLUGIN_NAME, `${PLUGIN_NAME} doesn't support vinyl files with Stream contents.`));
}
if (file.isBuffer()) {
const soliumrcPath = path.join(process.cwd(), ".soliumrc.json");
let options = {};
if (userSettings) {
options = userSettings;
} else if (fs.existsSync(soliumrcPath)) {
options = fs.readFileSync(soliumrcPath, { encoding: "UTF-8" });
} else {
options = {
"extends": "solium:recommended",
"options": { "returnInternalIssues": true }
};
}
try {
file.solium = Solium.lint(file.contents, options);
} catch (err) {
callback(new PluginError(PLUGIN_NAME, err));
}
}
callback(null, file);
});
}
/**
* Handle all Solium results at the end of the stream.
*
* @param {Function} action - A function to handle all Solium results
* @returns {stream} gulp file stream
*/
gulpSolium.results = (action) => {
if (typeof action !== "function") {
throw new Error("Expected callable argument");
}
let results = [];
let errorCount = 0;
let warningCount = 0;
return through.obj((file, enc, done) => {
if (file.solium) {
results.push(file.solium);
// count error & warning
file.solium.forEach(value => {
switch (value.type) {
case "error":
errorCount++;
break;
case "warning":
warningCount++;
break;
default:
break;
}
});
}
results = flatten(results);
results.errorCount = errorCount;
results.warningCount = warningCount;
done(null, file);
}, (done) => {
try {
if (action.length > 1) {
// async action
action.call(this, results, done);
} else {
// sync action
action.call(this, results);
done();
}
} catch (error) {
done(error == null ? new Error("Unknown Error") : error);
}
});
};
/**
* Fail when the stream ends if any Solium error(s) occurred
*
* @returns {stream} gulp file stream
*/
gulpSolium.failAfterError = () => {
return gulpSolium.results((results) => {
const count = results.errorCount;
if (!count) {
return;
}
throw new PluginError(PLUGIN_NAME, `Failed with ${count} error(s)`);
});
};
module.exports = gulpSolium;