This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathnode-configuration.js
207 lines (174 loc) · 5.52 KB
/
node-configuration.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var path = require('path');
var util = require('util');
var fs = require('fs');
var assert = require('assert');
var glob = require('glob');
var resolve = require('resolve');
var utils = require('../utils');
var Configuration = require('./configuration');
var configFinder = require('../cli-config');
var OVERRIDE_OPTIONS = [
'fix',
'preset',
'maxErrors',
'errorFilter',
'es3',
'extract'
];
function req(entity, dir) {
return require(
resolve.sync(entity, { basedir: dir })
);
}
/**
* nodejs-compatible configuration module.
*
* @name NodeConfiguration
* @augments Configuration
* @constructor
*/
function NodeConfiguration() {
Configuration.call(this);
this._basePath = process.cwd();
}
util.inherits(NodeConfiguration, Configuration);
/**
* Overrides configuration with options specified by the CLI
*
* @param {Object} program
*/
NodeConfiguration.prototype.overrideFromCLI = function(program) {
var overrides = {};
OVERRIDE_OPTIONS.forEach(function(option) {
if (option in program) {
overrides[option] = program[option];
}
});
this.override(overrides);
};
/**
* Load external module.
*
* @param {String|null} external - path (relative or absolute) or name to the external module
* @param {String} type - type of the module
* @param {String} [config = _basePath] - path to config relative to which external entities will be loaded
* @returns {Module|null}
* @protected
*/
NodeConfiguration.prototype.loadExternal = function(external, type, config) {
assert(
typeof external === 'string' || external === null,
'"' + type + '" option requires a string or null value'
);
if (external === null) {
return null;
}
var dir = config ? path.dirname(config) : this._basePath;
var get = function(prefix, postfix) {
prefix = prefix || '';
postfix = postfix || '';
try {
return finder(
utils.normalizePath(prefix + external + postfix, dir),
dir
);
} catch (e) {}
return null;
}.bind(this);
var finder;
if (type === 'preset') {
finder = configFinder.getContent;
} else {
finder = req;
}
var content;
if (external.indexOf('jscs-') !== 0) {
content = get('jscs-');
if (!content && type === 'preset') {
content = get('jscs-preset-') || get('jscs-config-');
if (!content && external.indexOf('/') !== -1 && !external.split('.')[1]) {
content = get('jscs-', '.json') ||
get('jscs-', '.js') ||
get('jscs-preset-', '.json') ||
get('jscs-config-', '.json') ||
get('jscs-preset-', '.js') ||
get('jscs-config-', '.js');
}
}
}
return content || get();
};
/**
* Loads plugin data.
*
* @param {String|function(Configuration)} plugin
* @param {String} [config] - path to config relative to which plugin will be loaded
* @protected
*/
NodeConfiguration.prototype._loadPlugin = function(plugin, config) {
if (typeof plugin !== 'function') {
plugin = this.loadExternal(plugin, 'plugin', config);
}
return Configuration.prototype._loadPlugin.call(this, plugin);
};
/**
* Loads preset.
*
* @param {String|null} preset
* @param {String} config - path to config relative to which plugin will be loaded
* @protected
*/
NodeConfiguration.prototype._loadPreset = function(preset, config) {
var name = path.basename(preset).split('.')[0];
try {
this.registerPreset(name, this.loadExternal(preset, 'preset', config));
} catch (e) {
var registeredPresets = this.getRegisteredPresets();
if (preset in registeredPresets) {
Configuration.prototype._loadPreset.call(this, preset);
return;
}
}
// If preset is an external module, error will be thrown by the caller
Configuration.prototype._loadPreset.call(this, name);
};
/**
* Loads an error filter module.
*
* @param {String|null} filter
* @param {String} config - path to config relative to which plugin will be loaded
* @protected
*/
NodeConfiguration.prototype._loadErrorFilter = function(filter, config) {
Configuration.prototype._loadErrorFilter.call(
this,
this.loadExternal(filter, 'errorFilter', config)
);
};
/**
* Loads additional rule.
*
* @param {String|Rule} additionalRule
* @param {String} config - path to config relative to which plugin will be loaded
* @private
*/
NodeConfiguration.prototype._loadAdditionalRule = function(additionalRule, config) {
config = config || this._basePath;
if (typeof additionalRule === 'string') {
if (glob.hasMagic(additionalRule)) {
// In some cases there might not be a config
// like if options are defined through direct initialization (grunt plugin case)
config = fs.statSync(config).isDirectory() ? config : path.dirname(config);
glob.sync(path.resolve(config, additionalRule)).forEach(function(p) {
var Rule = require(p);
Configuration.prototype._loadAdditionalRule.call(this, new Rule());
}, this);
} else {
var Rule = this.loadExternal(additionalRule, 'rule', config);
Configuration.prototype._loadAdditionalRule.call(this, new Rule());
}
} else {
Configuration.prototype._loadAdditionalRule.call(this, additionalRule);
}
};
module.exports = NodeConfiguration;