/
get-options-for-file.js
139 lines (120 loc) · 3.72 KB
/
get-options-for-file.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
"use strict";
const dashify = require("dashify");
// eslint-disable-next-line no-restricted-modules
const prettier = require("../../index.js");
const { optionsNormalizer } = require("../prettier-internal.js");
const minimist = require("./minimist.js");
const createMinimistOptions = require("./create-minimist-options.js");
const normalizeCliOptions = require("./normalize-cli-options.js");
function getOptions(argv, detailedOptions) {
return Object.fromEntries(
detailedOptions
.filter(({ forwardToApi }) => forwardToApi)
.map(({ forwardToApi, name }) => [forwardToApi, argv[name]])
);
}
function cliifyOptions(object, apiDetailedOptionMap) {
return Object.fromEntries(
Object.entries(object || {}).map(([key, value]) => {
const apiOption = apiDetailedOptionMap[key];
const cliKey = apiOption ? apiOption.name : key;
return [dashify(cliKey), value];
})
);
}
function createApiDetailedOptionMap(detailedOptions) {
return Object.fromEntries(
detailedOptions
.filter(
(option) => option.forwardToApi && option.forwardToApi !== option.name
)
.map((option) => [option.forwardToApi, option])
);
}
function parseArgsToOptions(context, overrideDefaults) {
const minimistOptions = createMinimistOptions(context.detailedOptions);
const apiDetailedOptionMap = createApiDetailedOptionMap(
context.detailedOptions
);
return getOptions(
normalizeCliOptions(
minimist(context.rawArguments, {
string: minimistOptions.string,
boolean: minimistOptions.boolean,
default: cliifyOptions(overrideDefaults, apiDetailedOptionMap),
}),
context.detailedOptions,
{ logger: false }
),
context.detailedOptions
);
}
async function getOptionsOrDie(context, filePath) {
try {
if (context.argv.config === false) {
context.logger.debug(
"'--no-config' option found, skip loading config file."
);
return null;
}
context.logger.debug(
context.argv.config
? `load config file from '${context.argv.config}'`
: `resolve config from '${filePath}'`
);
const options = await prettier.resolveConfig(filePath, {
editorconfig: context.argv.editorconfig,
config: context.argv.config,
});
context.logger.debug("loaded options `" + JSON.stringify(options) + "`");
return options;
} catch (error) {
context.logger.error(
`Invalid configuration file \`${filePath}\`: ` + error.message
);
process.exit(2);
}
}
function applyConfigPrecedence(context, options) {
try {
switch (context.argv.configPrecedence) {
case "cli-override":
return parseArgsToOptions(context, options);
case "file-override":
return { ...parseArgsToOptions(context), ...options };
case "prefer-file":
return options || parseArgsToOptions(context);
}
} catch (error) {
/* istanbul ignore next */
context.logger.error(error.toString());
/* istanbul ignore next */
process.exit(2);
}
}
async function getOptionsForFile(context, filepath) {
const options = await getOptionsOrDie(context, filepath);
const hasPlugins = options && options.plugins;
if (hasPlugins) {
context.pushContextPlugins(options.plugins);
}
const appliedOptions = {
filepath,
...applyConfigPrecedence(
context,
options &&
optionsNormalizer.normalizeApiOptions(options, context.supportOptions, {
logger: context.logger,
})
),
};
context.logger.debug(
`applied config-precedence (${context.argv.configPrecedence}): ` +
`${JSON.stringify(appliedOptions)}`
);
if (hasPlugins) {
context.popContextPlugins();
}
return appliedOptions;
}
module.exports = getOptionsForFile;