1 | 'use strict';
|
2 |
|
3 | var path = require('path'),
|
4 | esutil = require('eslint/lib/util'),
|
5 | estream = require('event-stream'),
|
6 | Config = require('eslint/lib/config');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function optional(path) {
|
12 | try {
|
13 | return require(path);
|
14 | } catch (error) {
|
15 | return null;
|
16 | }
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.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 |
|
36 |
|
37 | exports.checkForExclusion = function (file, config) {
|
38 |
|
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 |
|
52 | exclude = config.checkForExclusion(file.path);
|
53 | }
|
54 |
|
55 | return exclude;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | exports.readOptions = function (options) {
|
62 |
|
63 | if (!options || typeof options === 'string') {
|
64 |
|
65 | options = {
|
66 | config: options
|
67 | };
|
68 | }
|
69 |
|
70 | var helper = new Config(options);
|
71 |
|
72 | if (options.rules || options.globals || options.env) {
|
73 |
|
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 |
|
86 | require('eslint/lib/rules').load(options.rulesdir);
|
87 | }
|
88 |
|
89 | return helper;
|
90 | };
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | exports.resolveFormatter = function (formatter) {
|
97 | if (!formatter) {
|
98 |
|
99 | formatter = 'stylish';
|
100 | }
|
101 |
|
102 | if (typeof formatter === 'string') {
|
103 |
|
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 |
|
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 |
|
123 |
|
124 | exports.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 |
|
135 |
|
136 | exports.writeResults = function (results, formatter, writable) {
|
137 | var config;
|
138 | if (!results) {
|
139 | results = [];
|
140 | }
|
141 |
|
142 | results.some(function (result) {
|
143 | config = result && result.config;
|
144 | return config;
|
145 | });
|
146 |
|
147 | var message = formatter(results, config);
|
148 | if (writable && message != null && message !== '') {
|
149 | writable(message);
|
150 | }
|
151 | };
|