-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathconfiguration.js
545 lines (492 loc) · 14.1 KB
/
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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
* @import {PluginTuple, Plugin, Preset, Settings} from 'unified'
*/
/**
* @callback Callback
* Callback called when loading a config.
* @param {Error | undefined} error
* Error if something happened.
* @param {ConfigResult | undefined} [result]
* Result.
* @returns {undefined}
* Nothing.
*
* @typedef ConfigResult
* Resolved configuration.
* @property {string | undefined} filePath
* File path of found configuration.
* @property {Array<PluginTuple<Array<unknown>>>} plugins
* Resolved plugins.
* @property {Settings} settings
* Resolved settings.
*
* @callback ConfigTransform
* Transform arbitrary configs to our format.
* @param {any} config
* Arbitrary config.
* @param {string} filePath
* File path of config file.
* @returns {PresetSupportingSpecifiers}
* Our config format.
*
* @typedef ImportResult
* Result from an `import` call.
* @property {unknown} [default]
* Default field.
*
* Note: we can’t use `@-callback` because TS doesn’t deal with `this` correctly.
* @typedef {(this: Configuration, buf: Buffer, filePath: string) => Promise<PresetSupportingSpecifiers | undefined>} Loader
* Loader for different config files.
*
*
* @typedef MergeConfiguration
* How to merge.
* @property {string | undefined} prefix
* Plugin prefix.
* @property {string} root
* File path to merge from.
*
* Used to resolve plugins.
*
* @typedef {Array<PluggableSupportingSpecifiers>} PluggableListSupportingSpecifiers
* List of plugins and configuration, with support for specifiers.
*
* @typedef {Record<string, unknown>} PluggableMap
* Map where each key is a plugin specifier and each value is its primary parameter.
*
* @typedef {PluginSupportingSpecifiers | PluginTupleSupportingSpecifiers | Preset} PluggableSupportingSpecifiers
* Usable values, with support for specifiers.
*
* @typedef {Plugin<Array<unknown>> | string} PluginSupportingSpecifiers
* A plugin, or a specifier to one.
*
* @typedef {[plugin: string, ...parameters: Array<unknown>] | PluginTuple<Array<unknown>>} PluginTupleSupportingSpecifiers
* A plugin with configuration, with support for specifiers.
*
* @typedef PresetSupportingSpecifiers
* Sharable configuration, with support for specifiers.
*
* Specifiers should *not* be used in actual presets (because they can’t be
* used by regular unified), but they can be used in config files locally,
* as those are only for the engine.
*
* They can contain plugins and settings.
* @property {PluggableListSupportingSpecifiers | PluggableMap | undefined} [plugins]
* List of plugins and presets (optional).
* @property {Settings | undefined} [settings]
* Shared settings for parsers and compilers (optional).
*/
import assert from 'node:assert/strict'
import path from 'node:path'
import {fileURLToPath, pathToFileURL} from 'node:url'
import extend from 'extend'
import createDebug from 'debug'
import isPlainObj from 'is-plain-obj'
import {resolvePlugin} from 'load-plugin'
import parseJson from 'parse-json'
import yaml from 'yaml'
import {FindUp} from './find-up.js'
const debug = createDebug('unified-engine:configuration')
const own = {}.hasOwnProperty
/** @type {Record<string, Loader>} */
const loaders = {
'.json': loadJson,
'.cjs': loadScriptOrModule,
'.mjs': loadScriptOrModule,
'.js': loadScriptOrModule,
'.yaml': loadYaml,
'.yml': loadYaml
}
const defaultLoader = loadJson
/**
* @typedef Options
* Configuration.
* @property {ConfigTransform | undefined} [configTransform]
* Transform config files from a different schema (optional).
* @property {string} cwd
* Base (required).
* @property {PresetSupportingSpecifiers | undefined} [defaultConfig]
* Default configuration to use if no config file is given or found
* (optional).
* @property {boolean | undefined} [detectConfig]
* Whether to search for configuration files.
* @property {string | undefined} [packageField]
* Field where configuration can be found in `package.json` files
* (optional).
* @property {string | undefined} [pluginPrefix]
* Prefix to use when searching for plugins (optional).
* @property {PluggableListSupportingSpecifiers | PluggableMap | undefined} [plugins]
* Plugins to use (optional).
* @property {string | undefined} [rcName]
* Name of configuration files to load (optional).
* @property {string | undefined} [rcPath]
* Filepath to a configuration file to load (optional).
* @property {Settings | undefined} [settings]
* Configuration for the parser and compiler of the processor (optional).
*/
export class Configuration {
/**
* Internal class to load configuration files.
*
* Exposed to build more complex integrations.
*
* @param {Options} options
* Configuration (required).
* @returns
* Self.
*/
constructor(options) {
/** @type {Array<string>} */
const names = []
/** @type {string} */
this.cwd = options.cwd
/** @type {string | undefined} */
this.packageField = options.packageField
/** @type {string | undefined} */
this.pluginPrefix = options.pluginPrefix
/** @type {ConfigTransform | undefined} */
this.configTransform = options.configTransform
/** @type {PresetSupportingSpecifiers | undefined} */
this.defaultConfig = options.defaultConfig
if (options.rcName) {
names.push(
options.rcName,
...Object.keys(loaders).map(function (d) {
return options.rcName + d
})
)
debug('Looking for `%s` configuration files', names)
}
if (options.packageField) {
names.push('package.json')
debug(
'Looking for `%s` fields in `package.json` files',
options.packageField
)
}
/** @type {PresetSupportingSpecifiers} */
this.given = {plugins: options.plugins, settings: options.settings}
this.create = this.create.bind(this)
/** @type {FindUp<ConfigResult>} */
this.findUp = new FindUp({
create: this.create,
cwd: options.cwd,
detect: options.detectConfig,
filePath: options.rcPath,
names
})
}
/**
* Get the config for a file.
*
* @param {string} filePath
* File path to load.
* @param {Callback} callback
* Callback.
* @returns {undefined}
* Nothing.
*/
load(filePath, callback) {
const self = this
this.findUp.load(
filePath || path.resolve(this.cwd, 'stdin.js'),
function (error, file) {
if (error || file) {
return callback(error, file)
}
self.create(undefined, undefined).then(function (result) {
callback(undefined, result)
}, callback)
}
)
}
/**
* This is an internal method, consider it private.
*
* @param {Buffer | undefined} buf
* File value.
* @param {string | undefined} filePath
* File path.
* @returns {Promise<ConfigResult | undefined>}
* Result.
*/
async create(buf, filePath) {
const options = {cwd: this.cwd, prefix: this.pluginPrefix}
/** @type {ConfigResult} */
const result = {filePath: undefined, plugins: [], settings: {}}
const extname = filePath ? path.extname(filePath) : undefined
const loader =
extname && extname in loaders ? loaders[extname] : defaultLoader
/** @type {PresetSupportingSpecifiers | undefined} */
let value
if (filePath && buf) {
value = await loader.call(this, buf, filePath)
if (this.configTransform && value !== undefined) {
value = this.configTransform(value, filePath)
}
}
// Exit if we did find a `package.json`, but it does not have configuration.
if (
filePath &&
path.basename(filePath) === 'package.json' &&
value === undefined
) {
filePath = undefined
}
if (value === undefined) {
if (this.defaultConfig) {
await merge(result, this.defaultConfig, {...options, root: this.cwd})
}
} else {
assert(typeof filePath === 'string', 'Expected `filePath` to be set')
await merge(result, value, {...options, root: path.dirname(filePath)})
}
await merge(result, this.given, {...options, root: this.cwd})
result.filePath = filePath
return result
}
}
/**
* @this {Configuration}
* Class.
* @type {Loader}
* Loader.
*/
async function loadScriptOrModule(_, filePath) {
// Assume it’s a config.
const result = /** @type {ConfigResult} */ (
await loadFromAbsolutePath(pathToFileURL(filePath).href, this.cwd)
)
return result
}
/** @type {Loader} */
async function loadYaml(buf) {
return yaml.parse(String(buf))
}
/**
* @this {Configuration}
* Class.
* @type {Loader}
* Loader.
*/
async function loadJson(buf, filePath) {
/** @type {Record<string, unknown>} */
const data = parseJson(String(buf), filePath)
// Assume it’s a config.
const result = /** @type {ConfigResult} */ (
this.packageField && path.basename(filePath) === 'package.json'
? data[this.packageField]
: data
)
return result
}
/**
* @param {ConfigResult} target
* Result to merge into.
* @param {PresetSupportingSpecifiers} raw
* Raw found config.
* @param {MergeConfiguration} options
* Configuration.
* @returns {Promise<undefined>}
* Nothing.
*/
async function merge(target, raw, options) {
if (raw !== null && typeof raw === 'object') {
await addPreset(raw)
} else {
throw new Error('Expected preset, not `' + raw + '`')
}
/**
* @param {PresetSupportingSpecifiers} result
* Configuration file.
* @returns {Promise<undefined>}
* Nothing.
*/
async function addPreset(result) {
const plugins = result.plugins
if (plugins === null || plugins === undefined) {
// Empty.
} else if (plugins !== null && typeof plugins === 'object') {
await (Array.isArray(plugins) ? addEach(plugins) : addIn(plugins))
} else {
throw new Error(
'Expected a list or object of plugins, not `' + plugins + '`'
)
}
target.settings = extend(true, target.settings, result.settings)
}
/**
* @param {PluggableListSupportingSpecifiers} result
* List of plugins.
* @returns {Promise<undefined>}
* Nothing.
*/
async function addEach(result) {
let index = -1
while (++index < result.length) {
const value = result[index]
// Keep order sequential instead of parallel.
if (Array.isArray(value)) {
const [plugin, primaryValue] = value
await use(plugin, primaryValue)
} else {
await use(value, undefined)
}
}
}
/**
* @param {PluggableMap} result
* Map of plugins.
* @returns {Promise<undefined>}
* Nothing.
*/
async function addIn(result) {
/** @type {string} */
let key
for (key in result) {
if (own.call(result, key)) {
// Keep order sequential instead of parallel.
await use(key, result[key])
}
}
}
/**
* @param {PluginSupportingSpecifiers | Preset} usable
* Usable value.
* @param {unknown} value
* Primary parameter.
* @returns {Promise<undefined>}
* Nothing.
*/
async function use(usable, value) {
if (typeof usable === 'string') {
await addModule(usable, value)
} else if (typeof usable === 'function') {
addPlugin(usable, value)
} else {
await merge(target, usable, options)
}
}
/**
* @param {string} id
* Specifier.
* @param {unknown} value
* Primary parameter.
* @returns {Promise<undefined>}
* Nothing.
*/
async function addModule(id, value) {
/** @type {string} */
let fileUrl
try {
fileUrl = await resolvePlugin(id, {
from: pathToFileURL(options.root) + '/',
prefix: options.prefix
})
} catch (error) {
addPlugin(function () {
throw new Error('Cannot find module `' + id + '`', {cause: error})
}, value)
return
}
const result = await loadFromAbsolutePath(fileUrl, options.root)
try {
if (typeof result === 'function') {
// Assume plugin.
const plugin = /** @type {Plugin<Array<unknown>>} */ (result)
addPlugin(plugin, value)
} else {
// Assume preset.
const preset = /** @type {Preset} */ (result)
await merge(target, preset, {
...options,
root: path.dirname(fileURLToPath(fileUrl))
})
}
} catch (error) {
throw new Error(
'Expected preset or plugin, not `' +
result +
'`, at `' +
path.relative(options.root, fileURLToPath(fileUrl)) +
'`',
{cause: error}
)
}
}
/**
* @param {Plugin<Array<unknown>>} plugin
* Plugin.
* @param {unknown} value
* Primary parameter.
* @returns {undefined}
* Nothing.
*/
function addPlugin(plugin, value) {
const entry = find(target.plugins, plugin)
if (value === null) {
value = undefined
}
if (entry) {
reconfigure(entry, value)
} else {
target.plugins.push([plugin, value])
}
}
}
/**
* @param {PluginTuple<Array<unknown>>} entry
* Tuple.
* @param {unknown} value
* Primary value.
* @returns {undefined}
* Nothing.
*/
function reconfigure(entry, value) {
if (isPlainObj(entry[1]) && isPlainObj(value)) {
value = extend(true, entry[1], value)
}
entry[1] = value
}
/**
* @param {Array<PluginTuple<Array<unknown>>>} entries
* Tuples.
* @param {Plugin<Array<unknown>>} plugin
* Plugin.
* @returns {PluginTuple<Array<unknown>> | undefined}
* Tuple.
*/
function find(entries, plugin) {
let index = -1
while (++index < entries.length) {
const entry = entries[index]
if (entry[0] === plugin) {
return entry
}
}
}
/**
* @param {string} fileUrl
* Specifier.
* @param {string} base
* Base.
* @returns {Promise<unknown>}
* Result.
*/
async function loadFromAbsolutePath(fileUrl, base) {
try {
/** @type {ImportResult} */
const result = await import(fileUrl)
if (!('default' in result)) {
throw new Error(
'Expected a plugin or preset exported as the default export'
)
}
return result.default
} catch (error) {
throw new Error(
'Cannot import `' + path.relative(base, fileURLToPath(fileUrl)) + '`',
{cause: error}
)
}
}