-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathconfig.js
243 lines (216 loc) · 8.06 KB
/
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os from 'os';
import path from 'path';
import fs from 'fs/promises';
import camelCase from 'camelcase';
import decamelize from 'decamelize';
import parseJSON from 'parse-json';
import fileExists from './util/file-exists.js';
import { createLogger } from './util/logger.js';
import { UsageError, WebExtError } from './errors.js';
const log = createLogger(import.meta.url);
// NOTE: this error message is used in an interpolated string (while the other two help
// messages are being logged as is).
export const WARN_LEGACY_JS_EXT = [
'should be renamed to ".cjs" or ".mjs" file extension to ensure its format is not ambiguous.',
'Config files with the ".js" file extension are deprecated and will not be loaded anymore',
'in a future web-ext major version.',
].join(' ');
export const HELP_ERR_MODULE_FROM_ESM = [
'This config file belongs to a package.json file with "type" set to "module".',
'Change the file extension to ".cjs" or rewrite it as an ES module and use the ".mjs" file extension.',
].join(' ');
export const HELP_ERR_IMPORTEXPORT_CJS = [
'This config file is defined as an ES module, but it belongs to either a project directory',
'with a package.json file with "type" set to "commonjs" or one without any package.json file.',
'Change the file extension to ".mjs" to fix the config loading error.',
].join(' ');
const ERR_IMPORT_FROM_CJS = 'Cannot use import statement outside a module';
const ERR_EXPORT_FROM_CJS = "Unexpected token 'export'";
const ERR_MODULE_FROM_ESM = 'module is not defined in ES module scope';
export function applyConfigToArgv({
argv,
argvFromCLI,
configObject,
options,
configFileName,
}) {
let newArgv = { ...argv };
for (const option of Object.keys(configObject)) {
if (camelCase(option) !== option) {
throw new UsageError(
`The config option "${option}" must be ` +
`specified in camel case: "${camelCase(option)}"`,
);
}
// A config option cannot be a sub-command config
// object if it is an array.
if (
!Array.isArray(configObject[option]) &&
typeof options[option] === 'object' &&
typeof configObject[option] === 'object'
) {
// Descend into the nested configuration for a sub-command.
newArgv = applyConfigToArgv({
argv: newArgv,
argvFromCLI,
configObject: configObject[option],
options: options[option],
configFileName,
});
continue;
}
const decamelizedOptName = decamelize(option, { separator: '-' });
if (typeof options[decamelizedOptName] !== 'object') {
throw new UsageError(
`The config file at ${configFileName} specified ` +
`an unknown option: "${option}"`,
);
}
if (options[decamelizedOptName].type === undefined) {
// This means yargs option type wasn't not defined correctly
throw new WebExtError(`Option: ${option} was defined without a type.`);
}
const expectedType =
options[decamelizedOptName].type === 'count'
? 'number'
: options[decamelizedOptName].type;
const optionType = Array.isArray(configObject[option])
? 'array'
: typeof configObject[option];
if (optionType !== expectedType) {
throw new UsageError(
`The config file at ${configFileName} specified ` +
`the type of "${option}" incorrectly as "${optionType}"` +
` (expected type "${expectedType}")`,
);
}
let defaultValue;
if (options[decamelizedOptName]) {
if (options[decamelizedOptName].default !== undefined) {
defaultValue = options[decamelizedOptName].default;
} else if (expectedType === 'boolean') {
defaultValue = false;
}
}
// This is our best effort (without patching yargs) to detect
// if a value was set on the CLI instead of in the config.
// It looks for a default value and if the argv value is
// different, it assumes that the value was configured on the CLI.
const wasValueSetOnCLI =
typeof argvFromCLI[option] !== 'undefined' &&
argvFromCLI[option] !== defaultValue;
if (wasValueSetOnCLI) {
log.debug(
`Favoring CLI: ${option}=${argvFromCLI[option]} over ` +
`configuration: ${option}=${configObject[option]}`,
);
newArgv[option] = argvFromCLI[option];
continue;
}
newArgv[option] = configObject[option];
const coerce = options[decamelizedOptName].coerce;
if (coerce) {
log.debug(`Calling coerce() on configured value for ${option}`);
newArgv[option] = coerce(newArgv[option]);
}
newArgv[decamelizedOptName] = newArgv[option];
}
return newArgv;
}
export async function loadJSConfigFile(filePath) {
const resolvedFilePath = path.resolve(filePath);
log.debug(
`Loading JS config file: "${filePath}" ` +
`(resolved to "${resolvedFilePath}")`,
);
if (filePath.endsWith('.js')) {
log.warn(`WARNING: config file ${filePath} ${WARN_LEGACY_JS_EXT}`);
}
let configObject;
try {
const nonce = `${Date.now()}-${Math.random()}`;
let configModule;
if (resolvedFilePath.endsWith('package.json')) {
configModule = parseJSON(
await fs.readFile(resolvedFilePath, { encoding: 'utf-8' }),
);
} else {
configModule = await import(`file://${resolvedFilePath}?nonce=${nonce}`);
}
if (configModule.default) {
const { default: configDefault, ...esmConfigMod } = configModule;
// ES modules may expose both a default and named exports and so
// we merge the named exports on top of what may have been set in
// the default export.
configObject = { ...configDefault, ...esmConfigMod };
} else {
configObject = { ...configModule };
}
} catch (error) {
log.debug('Handling error:', error);
let errorMessage = error.message;
if (error.message.startsWith(ERR_MODULE_FROM_ESM)) {
errorMessage = HELP_ERR_MODULE_FROM_ESM;
} else if (
[ERR_IMPORT_FROM_CJS, ERR_EXPORT_FROM_CJS].includes(error.message)
) {
errorMessage = HELP_ERR_IMPORTEXPORT_CJS;
}
throw new UsageError(
`Cannot read config file: ${resolvedFilePath}\n` +
`Error: ${errorMessage}`,
);
}
if (filePath.endsWith('package.json')) {
log.debug('Looking for webExt key inside package.json file');
configObject = configObject.webExt || {};
}
if (Object.keys(configObject).length === 0) {
log.debug(
`Config file ${resolvedFilePath} did not define any options. ` +
'Did you set module.exports = {...}?',
);
}
return configObject;
}
export async function discoverConfigFiles({ getHomeDir = os.homedir } = {}) {
const magicConfigName = 'web-ext-config';
// Config files will be loaded in this order.
const possibleConfigs = [
// Look for a magic hidden config (preceded by dot) in home dir.
path.join(getHomeDir(), `.${magicConfigName}.mjs`),
path.join(getHomeDir(), `.${magicConfigName}.cjs`),
path.join(getHomeDir(), `.${magicConfigName}.js`),
// Look for webExt key inside package.json file
path.join(process.cwd(), 'package.json'),
// Look for a magic config in the current working directory.
path.join(process.cwd(), `${magicConfigName}.mjs`),
path.join(process.cwd(), `${magicConfigName}.cjs`),
path.join(process.cwd(), `${magicConfigName}.js`),
// Look for a magic hidden config (preceded by dot) the current working directory.
path.join(process.cwd(), `.${magicConfigName}.mjs`),
path.join(process.cwd(), `.${magicConfigName}.cjs`),
path.join(process.cwd(), `.${magicConfigName}.js`),
];
const configs = await Promise.all(
possibleConfigs.map(async (fileName) => {
const resolvedFileName = path.resolve(fileName);
if (await fileExists(resolvedFileName)) {
return resolvedFileName;
} else {
log.debug(
`Discovered config "${resolvedFileName}" does not ` +
'exist or is not readable',
);
return undefined;
}
}),
);
const existingConfigs = [];
configs.forEach((f) => {
if (typeof f === 'string') {
existingConfigs.push(f);
}
});
return existingConfigs;
}