-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
options-manager.js
185 lines (152 loc) · 4.16 KB
/
options-manager.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
'use strict';
var path = require('path');
var arrify = require('arrify');
var pkgConf = require('pkg-conf');
var deepAssign = require('deep-assign');
var objectAssign = require('object-assign');
var homeOrTmp = require('home-or-tmp');
var multimatch = require('multimatch');
var resolveFrom = require('resolve-from');
var pathExists = require('path-exists');
var DEFAULT_IGNORE = [
'**/node_modules/**',
'**/bower_components/**',
'coverage/**',
'{tmp,temp}/**',
'**/*.min.js',
'**/bundle.js',
'fixture{-*,}.{js,jsx}',
'{test/,}fixture{s,}/**',
'vendor/**',
'dist/**'
];
var DEFAULT_CONFIG = {
useEslintrc: false,
cache: true,
cacheLocation: path.join(homeOrTmp, '.xo-cache/'),
baseConfig: {
extends: [
'xo',
path.join(__dirname, 'config/overrides.js'),
path.join(__dirname, 'config/plugins.js')
]
}
};
function normalizeOpts(opts) {
opts = objectAssign({}, opts);
// alias to help humans
[
'env',
'global',
'ignore',
'plugin',
'rule',
'extend'
].forEach(function (singular) {
var plural = singular + 's';
var value = opts[plural] || opts[singular];
delete opts[singular];
if (value === undefined) {
return;
}
if (singular !== 'rule') {
value = arrify(value);
}
opts[plural] = value;
});
return opts;
}
function mergeWithPkgConf(opts) {
opts = objectAssign({cwd: process.cwd()}, opts);
return objectAssign({}, pkgConf.sync('xo', opts.cwd), opts);
}
function buildConfig(opts) {
var config = deepAssign({}, DEFAULT_CONFIG, {
envs: opts.envs,
globals: opts.globals,
plugins: opts.plugins,
rules: {},
fix: opts.fix
});
if (opts.space) {
var spaces = typeof opts.space === 'number' ? opts.space : 2;
config.rules.indent = [2, spaces, {SwitchCase: 1}];
}
if (opts.semicolon === false) {
config.rules.semi = [2, 'never'];
config.rules['semi-spacing'] = [2, {before: false, after: true}];
}
if (opts.esnext) {
config.baseConfig.extends = ['xo/esnext', path.join(__dirname, 'config/plugins.js')];
}
if (opts.rules) {
objectAssign(config.rules, opts.rules);
}
if (opts.extends && opts.extends.length > 0) {
// TODO: this logic needs to be improved, preferably use the same code as ESLint
// user's configs must be resolved to their absolute paths
var configs = opts.extends.map(function (name) {
// don't do anything if it's a filepath
if (pathExists.sync(name)) {
return name;
}
if (name.indexOf('eslint-config-') === -1) {
name = 'eslint-config-' + name;
}
return resolveFrom(opts.cwd, name);
});
config.baseConfig.extends = config.baseConfig.extends.concat(configs);
}
return config;
}
// Builds a list of overrides for a particular path, and a hash value.
// The hash value is a binary representation of which elements in the `overrides` array apply to the path.
//
// If overrides.length === 4, and only the first and third elements apply, then our hash is: 1010 (in binary)
function findApplicableOverrides(path, overrides) {
var hash = 0;
var applicable = [];
overrides.forEach(function (override) {
hash <<= 1;
if (multimatch(path, override.files).length > 0) {
applicable.push(override);
hash |= 1;
}
});
return {
hash: hash,
applicable: applicable
};
}
// Creates grouped sets of merged options together with the paths they apply to.
function groupConfigs(paths, baseOptions, overrides) {
var map = {};
var arr = [];
paths.forEach(function (x) {
var data = findApplicableOverrides(x, overrides);
if (!map[data.hash]) {
var mergedOpts = deepAssign.apply(null, [{}, baseOptions].concat(data.applicable));
delete mergedOpts.files;
arr.push(map[data.hash] = {
opts: mergedOpts,
paths: []
});
}
map[data.hash].paths.push(x);
});
return arr;
}
function preprocess(opts) {
opts = mergeWithPkgConf(opts);
opts = normalizeOpts(opts);
opts.ignores = DEFAULT_IGNORE.concat(opts.ignores || []);
return opts;
}
exports.DEFAULT_IGNORE = DEFAULT_IGNORE;
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
exports.mergeWithPkgConf = mergeWithPkgConf;
exports.normalizeOpts = normalizeOpts;
exports.buildConfig = buildConfig;
exports.findApplicableOverrides = findApplicableOverrides;
exports.groupConfigs = groupConfigs;
exports.preprocess = preprocess;