-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
resolve-config.js
175 lines (150 loc) · 4.69 KB
/
resolve-config.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
"use strict";
const thirdParty = require("../common/third-party");
const minimatch = require("minimatch");
const resolve = require("resolve");
const path = require("path");
const mem = require("mem");
const resolveEditorConfig = require("./resolve-config-editorconfig");
const loadToml = require("../utils/load-toml");
const getExplorerMemoized = mem(opts => {
const explorer = thirdParty.cosmiconfig("prettier", {
cache: opts.cache,
transform: result => {
if (result && result.config) {
if (typeof result.config === "string") {
const modulePath = resolve.sync(result.config, {
basedir: path.dirname(result.filepath)
});
result.config = eval("require")(modulePath);
}
if (typeof result.config !== "object") {
throw new Error(
`Config is only allowed to be an object, ` +
`but received ${typeof result.config} in "${result.filepath}"`
);
}
delete result.config.$schema;
}
return result;
},
searchPlaces: [
"package.json",
".prettierrc",
".prettierrc.json",
".prettierrc.yaml",
".prettierrc.yml",
".prettierrc.js",
"prettier.config.js",
".prettierrc.toml"
],
loaders: {
".toml": loadToml
}
});
const load = opts.sync ? explorer.loadSync : explorer.load;
const search = opts.sync ? explorer.searchSync : explorer.search;
return {
// cosmiconfig v4 interface
load: (searchPath, configPath) =>
configPath ? load(configPath) : search(searchPath)
};
});
/** @param {{ cache: boolean, sync: boolean }} opts */
function getLoadFunction(opts) {
// Normalize opts before passing to a memoized function
opts = Object.assign({ sync: false, cache: false }, opts);
return getExplorerMemoized(opts).load;
}
function _resolveConfig(filePath, opts, sync) {
opts = Object.assign({ useCache: true }, opts);
const loadOpts = {
cache: !!opts.useCache,
sync: !!sync,
editorconfig: !!opts.editorconfig
};
const load = getLoadFunction(loadOpts);
const loadEditorConfig = resolveEditorConfig.getLoadFunction(loadOpts);
const arr = [load, loadEditorConfig].map(l => l(filePath, opts.config));
const unwrapAndMerge = arr => {
const result = arr[0];
const editorConfigured = arr[1];
const merged = Object.assign(
{},
editorConfigured,
mergeOverrides(Object.assign({}, result), filePath)
);
["plugins", "pluginSearchDirs"].forEach(optionName => {
if (Array.isArray(merged[optionName])) {
merged[optionName] = merged[optionName].map(value =>
typeof value === "string" && value.startsWith(".") // relative path
? path.resolve(path.dirname(result.filepath), value)
: value
);
}
});
if (!result && !editorConfigured) {
return null;
}
return merged;
};
if (loadOpts.sync) {
return unwrapAndMerge(arr);
}
return Promise.all(arr).then(unwrapAndMerge);
}
const resolveConfig = (filePath, opts) => _resolveConfig(filePath, opts, false);
resolveConfig.sync = (filePath, opts) => _resolveConfig(filePath, opts, true);
function clearCache() {
mem.clear(getExplorerMemoized);
resolveEditorConfig.clearCache();
}
function resolveConfigFile(filePath) {
const load = getLoadFunction({ sync: false });
return load(filePath).then(result => {
return result ? result.filepath : null;
});
}
resolveConfigFile.sync = filePath => {
const load = getLoadFunction({ sync: true });
const result = load(filePath);
return result ? result.filepath : null;
};
function mergeOverrides(configResult, filePath) {
const options = Object.assign({}, configResult.config);
if (filePath && options.overrides) {
const relativeFilePath = path.relative(
path.dirname(configResult.filepath),
filePath
);
for (const override of options.overrides) {
if (
pathMatchesGlobs(
relativeFilePath,
override.files,
override.excludeFiles
)
) {
Object.assign(options, override.options);
}
}
}
delete options.overrides;
return options;
}
// Based on eslint: https://github.com/eslint/eslint/blob/master/lib/config/config-ops.js
function pathMatchesGlobs(filePath, patterns, excludedPatterns) {
const patternList = [].concat(patterns);
const excludedPatternList = [].concat(excludedPatterns || []);
const opts = { matchBase: true, dot: true };
return (
patternList.some(pattern => minimatch(filePath, pattern, opts)) &&
!excludedPatternList.some(excludedPattern =>
minimatch(filePath, excludedPattern, opts)
)
);
}
module.exports = {
resolveConfig,
resolveConfigFile,
clearCache
};