1 |
|
2 | import { resolve } from 'path'
|
3 | import matchRange from 'version-range'
|
4 | import { errtion, Errtion } from './util.js'
|
5 | import { versions as processVersions } from 'process'
|
6 | import { readFileSync } from 'fs'
|
7 |
|
8 | export type Range = string | boolean
|
9 | export type Engines = false | { [engine: string]: Range }
|
10 | export type Versions = { [engine: string]: string }
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export interface Edition {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | description: string
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | directory: string
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | entry: string
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | tags?: string[]
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | engines: Engines
|
85 |
|
86 |
|
87 | debugging?: Errtion
|
88 | }
|
89 |
|
90 |
|
91 | export type Editions = Array<Edition>
|
92 |
|
93 | export interface PathOptions {
|
94 |
|
95 | entry: string
|
96 |
|
97 |
|
98 | cwd: string
|
99 | }
|
100 |
|
101 | export interface LoaderOptions extends Partial<PathOptions> {
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | loader: <T>(this: Edition, path: string) => T
|
108 | }
|
109 |
|
110 | export interface RangeOptions {
|
111 |
|
112 | broadenRange?: boolean
|
113 | }
|
114 |
|
115 | export interface VersionOptions extends RangeOptions {
|
116 |
|
117 | versions: Versions
|
118 | }
|
119 |
|
120 | export interface SolicitOptions extends LoaderOptions, VersionOptions {}
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | export function loadEdition<T>(edition: Edition, opts: LoaderOptions): T {
|
128 | const entry = resolve(
|
129 | opts.cwd || '',
|
130 | edition.directory,
|
131 | opts.entry || edition.entry || ''
|
132 | )
|
133 |
|
134 | if (opts.loader == null) {
|
135 | throw errtion({
|
136 | message: `Could not load the edition [${edition.description}] as no loader was specified. This is probably due to a testing misconfiguration.`,
|
137 | code: 'editions-autoloader-loader-missing',
|
138 | level: 'fatal',
|
139 | })
|
140 | }
|
141 |
|
142 | try {
|
143 | return opts.loader.call(edition, entry) as T
|
144 | } catch (loadError) {
|
145 |
|
146 | throw errtion(
|
147 | {
|
148 | message: `Failed to load the entry [${entry}] of edition [${edition.description}].`,
|
149 | code: 'editions-autoloader-loader-failed',
|
150 | level: 'fatal',
|
151 | },
|
152 | loadError
|
153 | )
|
154 | }
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | export function isValidEdition(edition: Edition): true {
|
163 | if (
|
164 | !edition.description ||
|
165 | !edition.directory ||
|
166 | !edition.entry ||
|
167 | edition.engines == null
|
168 | ) {
|
169 | throw errtion({
|
170 | message: `An edition must have its [description, directory, entry, engines] fields defined, yet all this edition defined were [${Object.keys(
|
171 | edition
|
172 | ).join(', ')}]`,
|
173 | code: 'editions-autoloader-invalid-edition',
|
174 | level: 'fatal',
|
175 | })
|
176 | }
|
177 |
|
178 |
|
179 | return true
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | export function isCompatibleVersion(
|
188 | range: Range,
|
189 | version: string,
|
190 | opts: RangeOptions
|
191 | ): true {
|
192 |
|
193 | const { broadenRange } = opts
|
194 |
|
195 | if (!version)
|
196 | throw errtion({
|
197 | message: `No version was specified to compare the range [${range}] against`,
|
198 | code: 'editions-autoloader-engine-version-missing',
|
199 | level: 'fatal',
|
200 | })
|
201 |
|
202 | if (range == null || range === '')
|
203 | throw errtion({
|
204 | message: `The edition range was not specified, so unable to compare against the version [${version}]`,
|
205 | code: 'editions-autoloader-engine-range-missing',
|
206 | })
|
207 |
|
208 | if (range === false)
|
209 | throw errtion({
|
210 | message: `The edition range does not support this engine`,
|
211 | code: 'editions-autoloader-engine-unsupported',
|
212 | })
|
213 |
|
214 | if (range === true) return true
|
215 |
|
216 |
|
217 | try {
|
218 | if (matchRange(version, range)) return true
|
219 | } catch (error) {
|
220 | throw errtion(
|
221 | {
|
222 | message: `The range [${range}] was invalid, something is wrong with the Editions definition.`,
|
223 | code: 'editions-autoloader-invalid-range',
|
224 | level: 'fatal',
|
225 | },
|
226 | error
|
227 | )
|
228 | }
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | if (broadenRange === true) {
|
239 |
|
240 | const broadenedRangeRegex = /^\s*([0-9.]+)\s*(\|\|\s*[0-9.]+\s*)*$/
|
241 | const broadenedRangeMatch = range.match(broadenedRangeRegex)
|
242 | const lowestVersion: string =
|
243 | (broadenedRangeMatch && broadenedRangeMatch[1]) || ''
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | if (lowestVersion === '')
|
249 | throw errtion({
|
250 | message: `The range [${range}] is not able to be broadened, only ranges in format of [lowest] or [lowest || ... || ... ] can be broadened. Update the Editions definition and try again.`,
|
251 | code: 'editions-autoloader-unsupported-broadened-range',
|
252 | level: 'fatal',
|
253 | })
|
254 |
|
255 |
|
256 | const broadenedRange = `>= ${lowestVersion}`
|
257 | try {
|
258 | if (matchRange(version, broadenedRange)) return true
|
259 | } catch (error) {
|
260 | throw errtion(
|
261 | {
|
262 | message: `The broadened range [${broadenedRange}] was invalid, something is wrong within Editions.`,
|
263 | code: 'editions-autoloader-invalid-broadened-range',
|
264 | level: 'fatal',
|
265 | },
|
266 | error
|
267 | )
|
268 | }
|
269 |
|
270 |
|
271 | throw errtion({
|
272 | message: `The edition range [${range}] does not support this engine version [${version}], even when broadened to [${broadenedRange}]`,
|
273 | code: 'editions-autoloader-engine-incompatible-broadened-range',
|
274 | })
|
275 | }
|
276 |
|
277 |
|
278 | throw errtion({
|
279 | message: `The edition range [${range}] does not support this engine version [${version}]`,
|
280 | code: 'editions-autoloader-engine-incompatible-original',
|
281 | })
|
282 | }
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | export function isCompatibleEngines(
|
290 | engines: Engines,
|
291 | opts: VersionOptions
|
292 | ): true {
|
293 |
|
294 | const { versions } = opts
|
295 |
|
296 |
|
297 | if (!engines) {
|
298 | throw errtion({
|
299 | message: `The edition had no engines to compare against the environment`,
|
300 | code: 'editions-autoloader-invalid-engines',
|
301 | })
|
302 | }
|
303 |
|
304 |
|
305 | if (!versions) {
|
306 | throw errtion({
|
307 | message: `No versions were supplied to compare the engines against`,
|
308 | code: 'editions-autoloader-invalid-versions',
|
309 | level: 'fatal',
|
310 | })
|
311 | }
|
312 |
|
313 |
|
314 | let compatible = false
|
315 | for (const key in engines) {
|
316 | if (engines.hasOwnProperty(key)) {
|
317 |
|
318 |
|
319 | if (key === 'node' && versions.deno) continue
|
320 |
|
321 |
|
322 | const engine = engines[key]
|
323 | const version = versions[key]
|
324 |
|
325 |
|
326 | if (version == null) continue
|
327 |
|
328 |
|
329 | try {
|
330 | isCompatibleVersion(engine, version, opts)
|
331 | compatible = true
|
332 |
|
333 |
|
334 | } catch (rangeError) {
|
335 | throw errtion(
|
336 | {
|
337 | message: `The engine [${key}] range of [${engine}] was not compatible against version [${version}].`,
|
338 | code: 'editions-autoloader-engine-error',
|
339 | },
|
340 | rangeError
|
341 | )
|
342 | }
|
343 | }
|
344 | }
|
345 |
|
346 |
|
347 | if (!compatible) {
|
348 | throw errtion({
|
349 | message: `There were no supported engines in which this environment provides.`,
|
350 | code: 'editions-autoloader-engine-mismatch',
|
351 | })
|
352 | }
|
353 |
|
354 |
|
355 | return true
|
356 | }
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | export function isCompatibleEdition(
|
364 | edition: Edition,
|
365 | opts: VersionOptions
|
366 | ): true {
|
367 | try {
|
368 | return isCompatibleEngines(edition.engines, opts)
|
369 | } catch (compatibleError) {
|
370 | throw errtion(
|
371 | {
|
372 | message: `The edition [${edition.description}] is not compatible with this environment.`,
|
373 | code: 'editions-autoloader-edition-incompatible',
|
374 | },
|
375 | compatibleError
|
376 | )
|
377 | }
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | export function determineEdition(
|
387 | editions: Editions,
|
388 | opts: VersionOptions
|
389 | ): Edition {
|
390 |
|
391 | const { broadenRange } = opts
|
392 |
|
393 |
|
394 | if (!editions || editions.length === 0) {
|
395 | throw errtion({
|
396 | message: 'No editions were specified.',
|
397 | code: 'editions-autoloader-editions-missing',
|
398 | })
|
399 | }
|
400 |
|
401 |
|
402 | let failure: Errtion | null = null
|
403 | for (let i = 0; i < editions.length; ++i) {
|
404 | const edition = editions[i]
|
405 | try {
|
406 | isValidEdition(edition)
|
407 | isCompatibleEdition(edition, opts)
|
408 |
|
409 |
|
410 | return edition
|
411 | } catch (error) {
|
412 | if (error.level === 'fatal') {
|
413 | throw errtion(
|
414 | {
|
415 | message: `Unable to determine a suitable edition due to failure.`,
|
416 | code: 'editions-autoloader-fatal',
|
417 | level: 'fatal',
|
418 | },
|
419 | error
|
420 | )
|
421 | } else if (failure) {
|
422 | failure = errtion(error, failure)
|
423 | } else {
|
424 | failure = error
|
425 | }
|
426 | }
|
427 | }
|
428 |
|
429 |
|
430 | if (failure) {
|
431 |
|
432 | if (broadenRange == null)
|
433 | try {
|
434 |
|
435 | const broadenedEdition = determineEdition(editions, {
|
436 | ...opts,
|
437 | broadenRange: true,
|
438 | })
|
439 | return {
|
440 | ...broadenedEdition,
|
441 |
|
442 | debugging: errtion({
|
443 | message: `The edition ${broadenedEdition.description} was selected to be force loaded as its range was broadened.`,
|
444 | code: 'editions-autoloader-attempt-broadened',
|
445 | }),
|
446 | }
|
447 | } catch (error) {
|
448 | throw errtion(
|
449 | {
|
450 | message: `Unable to determine a suitable edition, even after broadening.`,
|
451 | code: 'editions-autoloader-none-broadened',
|
452 | },
|
453 | error
|
454 | )
|
455 | }
|
456 |
|
457 |
|
458 | throw errtion(
|
459 | {
|
460 | message: `Unable to determine a suitable edition, as none were suitable.`,
|
461 | code: 'editions-autoloader-none-suitable',
|
462 | },
|
463 | failure
|
464 | )
|
465 | }
|
466 |
|
467 |
|
468 | throw errtion({
|
469 | message: `Unable to determine a suitable edition, as an unexpected pathway occurred.`,
|
470 | code: 'editions-autoloader-never',
|
471 | })
|
472 | }
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | export function solicitEdition<T>(editions: Editions, opts: SolicitOptions): T {
|
480 | const edition = determineEdition(editions, opts)
|
481 | try {
|
482 | return loadEdition<T>(edition, opts)
|
483 | } catch (error) {
|
484 | throw errtion(error, edition.debugging)
|
485 | }
|
486 | }
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | export function requirePackage<T>(
|
494 | cwd: PathOptions['cwd'],
|
495 | loader: LoaderOptions['loader'],
|
496 | entry: PathOptions['entry']
|
497 | ): T {
|
498 | const packagePath = resolve(cwd || '', 'package.json')
|
499 | try {
|
500 |
|
501 | const { editions } = JSON.parse(readFileSync(packagePath, 'utf8'))
|
502 |
|
503 |
|
504 | return solicitEdition<T>(editions, {
|
505 | versions: processVersions as any as Versions,
|
506 | cwd,
|
507 | loader,
|
508 | entry,
|
509 | })
|
510 | } catch (error) {
|
511 | throw errtion(
|
512 | {
|
513 | message: `Unable to determine a suitable edition for the package [${packagePath}] and entry [${entry}]`,
|
514 | code: 'editions-autoloader-package',
|
515 | },
|
516 | error
|
517 | )
|
518 | }
|
519 | }
|