UNPKG

3.43 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path'),
4 esutil = require('eslint/lib/util'),
5 estream = require('event-stream'),
6 Config = require('eslint/lib/config');
7
8/**
9 * Optional import, if not found, returns null.
10 */
11function optional(path) {
12 try {
13 return require(path);
14 } catch (error) {
15 return null;
16 }
17}
18
19/**
20 * Variation on event-stream's "wait" method that returns a "reset" stream.
21 */
22exports.wait = function (cb) {
23 var content = '';
24 return estream.through(function (data) {
25 content += data;
26 this.queue(data);
27 }, function () {
28 process.nextTick(this.resume);
29 this.queue(null);
30 cb.call(this, content);
31 }).pause();
32};
33
34/**
35 * Check if file should be excluded from eslint processing
36 */
37exports.checkForExclusion = function (file, config) {
38 // Ignore null and non-js files
39 var exclude;
40
41 if (file.isDirectory()) {
42 exclude = true;
43 config.cacheExclusions(file.path);
44
45 } else {
46 exclude = file.isNull()
47 || path.extname(file.path).toLowerCase() !== '.js';
48 }
49
50 if (!exclude) {
51 // Support for .eslintignore (https://github.com/eslint/eslint/commit/cdcba8941b51176e6f998eb07fca1cb93dabe391)
52 exclude = config.checkForExclusion(file.path);
53 }
54
55 return exclude;
56};
57
58/**
59 * Create config helper to merge various config sources
60 */
61exports.readOptions = function (options) {
62
63 if (!options || typeof options === 'string') {
64 // load external config data (if a string)
65 options = {
66 config: options
67 };
68 }
69
70 var helper = new Config(options);
71
72 if (options.rules || options.globals || options.env) {
73 // inline definitions
74 helper.useSpecificConfig = esutil.mergeConfigs(
75 helper.useSpecificConfig || {},
76 {
77 rules: options.rules || {},
78 globals: options.globals || {},
79 env: options.env || {}
80 }
81 );
82 }
83
84 if (options.rulesdir) {
85 // rulesdir: Load additional rules from this directory
86 require('eslint/lib/rules').load(options.rulesdir);
87 }
88
89 return helper;
90};
91
92/**
93 * Resolve formatter from unknown type (accepts string or function)
94 * @exception TypeError thrown if unable to resolve the formatter type
95 */
96exports.resolveFormatter = function (formatter) {
97 if (!formatter) {
98 // default formatter
99 formatter = 'stylish';
100 }
101
102 if (typeof formatter === 'string') {
103 // load formatter (module, relative to cwd, eslint formatter)
104 formatter = optional(formatter)
105 || optional(require('path').resolve(process.cwd(), formatter))
106 || optional('eslint/lib/formatters/' + formatter);
107
108 if (typeof formatter === 'string') {
109 // certain formatter modules return a path to the formatter
110 formatter = optional(formatter);
111 }
112 }
113
114 if (typeof formatter !== 'function') {
115 throw new TypeError('Invalid Formatter');
116 }
117
118 return formatter;
119};
120
121/**
122 * Resolve writable
123 */
124exports.resolveWritable = function (writable) {
125 if (!writable) {
126 writable = require('gulp-util').log;
127 } else if (typeof writable.write === 'function') {
128 writable = writable.write.bind(writable);
129 }
130 return writable;
131};
132
133/**
134 * Write formatter results to writable/output
135 */
136exports.writeResults = function (results, formatter, writable) {
137 var config;
138 if (!results) {
139 results = [];
140 }
141 // get the first result config
142 results.some(function (result) {
143 config = result && result.config;
144 return config;
145 });
146 // formatters typically receive a list of results. We have but one
147 var message = formatter(results, config);
148 if (writable && message != null && message !== '') {
149 writable(message);
150 }
151};