forked from adametry/gulp-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
153 lines (133 loc) · 3.35 KB
/
util.js
File metadata and controls
153 lines (133 loc) · 3.35 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
var path = require('path'),
gutil = require('gulp-util'),
through = require('through'),
Config = require('eslint/lib/config');
/**
* Optional import, if not found, returns null.
*/
function optional(name) {
try {
return require(name);
} catch (error) {
return null;
}
}
/**
* Variation on event-stream's "wait" method that returns a "reset" stream.
*/
exports.wait = function wait(cb) {
var content = new Buffer([]);
return through(function bufferData(data) {
content = Buffer.concat([content, data]);
this.queue(data);
}, function releaseData() {
cb(content.toString());
this.emit('end');
});
};
/**
* Create config helper to merge various config sources
*/
exports.migrateOptions = function migrateOptions(from) {
var globals, envs;
if (typeof from === 'string') {
// basic config path overload: gulpEslint('path/to/config.json')
from = {
configFile: from
};
}
var to = {};
for (var key in from) {
if (from.hasOwnProperty(key)) {
to[key] = from[key];
}
}
globals = to.globals || to.global;
if (globals != null) {
to.globals = Array.isArray(globals) ?
globals :
Object.keys(globals).map(function cliGlobal(key) {
return globals[key] ? key + ':true' : key;
});
}
envs = to.envs || to.env;
if (envs) {
to.envs = Array.isArray(envs) ?
envs :
Object.keys(envs).filter(function cliEnv(key) {
return envs[key];
});
}
if (to.config != null) {
// The "config" option has been deprecated. Use "configFile".
to.configFile = to.config;
}
if (to.rulesdir != null) {
// The "rulesdir" option has been deprecated. Use "rulesPaths".
to.rulesPaths = (typeof to.rulesdir === 'string') ? [to.rulesdir] : to.rulesdir;
}
if (to.eslintrc != null) {
// The "eslintrc" option has been deprecated. Use "useEslintrc".
to.useEslintrc = to.eslintrc;
}
return to;
};
/**
* Resolve formatter from unknown type (accepts string or function)
* @exception TypeError thrown if unable to resolve the formatter type
*/
exports.resolveFormatter = function (formatter) {
if (!formatter) {
// default formatter
formatter = 'stylish';
}
if (typeof formatter === 'string') {
// load formatter (module, relative to cwd, eslint formatter)
formatter = optional(formatter)
|| optional(path.resolve(process.cwd(), formatter))
|| optional('eslint/lib/formatters/' + formatter);
if (typeof formatter === 'string') {
// certain formatter modules return a path to the formatter
formatter = optional(formatter);
}
}
if (typeof formatter !== 'function') {
if (arguments[0] == null) {
// eslint@<0.3.0 default
return exports.resolveFormatter('compact');
} else {
throw new TypeError('Invalid Formatter');
}
}
return formatter;
};
/**
* Resolve writable
*/
exports.resolveWritable = function (writable) {
if (!writable) {
writable = gutil.log;
} else if (typeof writable.write === 'function') {
writable = writable.write.bind(writable);
}
return writable;
};
/**
* Write formatter results to writable/output
*/
exports.writeResults = function (results, formatter, writable) {
var config;
if (!results) {
results = [];
}
// get the first result config
results.some(function (result) {
config = result && result.config;
return config;
});
var message = formatter(results, config || {});
if (writable && message != null && message !== '') {
writable(message);
}
};