1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
14 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
15 | };
|
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
17 | exports.requirePackage = exports.solicitEdition = exports.determineEdition = exports.isCompatibleEdition = exports.isCompatibleEngines = exports.isCompatibleVersion = exports.isValidEdition = exports.loadEdition = void 0;
|
18 |
|
19 | var path_1 = require("path");
|
20 | var version_range_1 = __importDefault(require("version-range"));
|
21 | var util_js_1 = require("./util.js");
|
22 | var process_1 = require("process");
|
23 | var fs_1 = require("fs");
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function loadEdition(edition, opts) {
|
30 | var entry = path_1.resolve(opts.cwd || '', edition.directory, opts.entry || edition.entry || '');
|
31 | if (opts.loader == null) {
|
32 | throw util_js_1.errtion({
|
33 | message: "Could not load the edition [" + edition.description + "] as no loader was specified. This is probably due to a testing misconfiguration.",
|
34 | code: 'editions-autoloader-loader-missing',
|
35 | level: 'fatal',
|
36 | });
|
37 | }
|
38 | try {
|
39 | return opts.loader.call(edition, entry);
|
40 | }
|
41 | catch (loadError) {
|
42 |
|
43 | throw util_js_1.errtion({
|
44 | message: "Failed to load the entry [" + entry + "] of edition [" + edition.description + "].",
|
45 | code: 'editions-autoloader-loader-failed',
|
46 | level: 'fatal',
|
47 | }, loadError);
|
48 | }
|
49 | }
|
50 | exports.loadEdition = loadEdition;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function isValidEdition(edition) {
|
57 | if (!edition.description ||
|
58 | !edition.directory ||
|
59 | !edition.entry ||
|
60 | edition.engines == null) {
|
61 | throw util_js_1.errtion({
|
62 | message: "An edition must have its [description, directory, entry, engines] fields defined, yet all this edition defined were [" + Object.keys(edition).join(', ') + "]",
|
63 | code: 'editions-autoloader-invalid-edition',
|
64 | level: 'fatal',
|
65 | });
|
66 | }
|
67 |
|
68 | return true;
|
69 | }
|
70 | exports.isValidEdition = isValidEdition;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function isCompatibleVersion(range, version, opts) {
|
77 |
|
78 | var broadenRange = opts.broadenRange;
|
79 | if (!version)
|
80 | throw util_js_1.errtion({
|
81 | message: "No version was specified to compare the range [" + range + "] against",
|
82 | code: 'editions-autoloader-engine-version-missing',
|
83 | level: 'fatal',
|
84 | });
|
85 | if (range == null || range === '')
|
86 | throw util_js_1.errtion({
|
87 | message: "The edition range was not specified, so unable to compare against the version [" + version + "]",
|
88 | code: 'editions-autoloader-engine-range-missing',
|
89 | });
|
90 | if (range === false)
|
91 | throw util_js_1.errtion({
|
92 | message: "The edition range does not support this engine",
|
93 | code: 'editions-autoloader-engine-unsupported',
|
94 | });
|
95 | if (range === true)
|
96 | return true;
|
97 |
|
98 | try {
|
99 | if (version_range_1.default(version, range))
|
100 | return true;
|
101 | }
|
102 | catch (error) {
|
103 | throw util_js_1.errtion({
|
104 | message: "The range [" + range + "] was invalid, something is wrong with the Editions definition.",
|
105 | code: 'editions-autoloader-invalid-range',
|
106 | level: 'fatal',
|
107 | }, error);
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | if (broadenRange === true) {
|
118 |
|
119 | var broadenedRangeRegex = /^\s*([0-9.]+)\s*(\|\|\s*[0-9.]+\s*)*$/;
|
120 | var broadenedRangeMatch = range.match(broadenedRangeRegex);
|
121 | var lowestVersion = (broadenedRangeMatch && broadenedRangeMatch[1]) || '';
|
122 |
|
123 |
|
124 |
|
125 | if (lowestVersion === '')
|
126 | throw util_js_1.errtion({
|
127 | 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.",
|
128 | code: 'editions-autoloader-unsupported-broadened-range',
|
129 | level: 'fatal',
|
130 | });
|
131 |
|
132 | var broadenedRange = ">= " + lowestVersion;
|
133 | try {
|
134 | if (version_range_1.default(version, broadenedRange))
|
135 | return true;
|
136 | }
|
137 | catch (error) {
|
138 | throw util_js_1.errtion({
|
139 | message: "The broadened range [" + broadenedRange + "] was invalid, something is wrong within Editions.",
|
140 | code: 'editions-autoloader-invalid-broadened-range',
|
141 | level: 'fatal',
|
142 | }, error);
|
143 | }
|
144 |
|
145 | throw util_js_1.errtion({
|
146 | message: "The edition range [" + range + "] does not support this engine version [" + version + "], even when broadened to [" + broadenedRange + "]",
|
147 | code: 'editions-autoloader-engine-incompatible-broadened-range',
|
148 | });
|
149 | }
|
150 |
|
151 | throw util_js_1.errtion({
|
152 | message: "The edition range [" + range + "] does not support this engine version [" + version + "]",
|
153 | code: 'editions-autoloader-engine-incompatible-original',
|
154 | });
|
155 | }
|
156 | exports.isCompatibleVersion = isCompatibleVersion;
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function isCompatibleEngines(engines, opts) {
|
163 |
|
164 | var versions = opts.versions;
|
165 |
|
166 | if (!engines) {
|
167 | throw util_js_1.errtion({
|
168 | message: "The edition had no engines to compare against the environment",
|
169 | code: 'editions-autoloader-invalid-engines',
|
170 | });
|
171 | }
|
172 |
|
173 | if (!versions) {
|
174 | throw util_js_1.errtion({
|
175 | message: "No versions were supplied to compare the engines against",
|
176 | code: 'editions-autoloader-invalid-versions',
|
177 | level: 'fatal',
|
178 | });
|
179 | }
|
180 |
|
181 | var compatible = false;
|
182 | for (var key in engines) {
|
183 | if (engines.hasOwnProperty(key)) {
|
184 |
|
185 |
|
186 | if (key === 'node' && versions.deno)
|
187 | continue;
|
188 |
|
189 | var engine = engines[key];
|
190 | var version = versions[key];
|
191 |
|
192 | if (version == null)
|
193 | continue;
|
194 |
|
195 | try {
|
196 | isCompatibleVersion(engine, version, opts);
|
197 | compatible = true;
|
198 |
|
199 | }
|
200 | catch (rangeError) {
|
201 | throw util_js_1.errtion({
|
202 | message: "The engine [" + key + "] range of [" + engine + "] was not compatible against version [" + version + "].",
|
203 | code: 'editions-autoloader-engine-error',
|
204 | }, rangeError);
|
205 | }
|
206 | }
|
207 | }
|
208 |
|
209 | if (!compatible) {
|
210 | throw util_js_1.errtion({
|
211 | message: "There were no supported engines in which this environment provides.",
|
212 | code: 'editions-autoloader-engine-mismatch',
|
213 | });
|
214 | }
|
215 |
|
216 | return true;
|
217 | }
|
218 | exports.isCompatibleEngines = isCompatibleEngines;
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | function isCompatibleEdition(edition, opts) {
|
225 | try {
|
226 | return isCompatibleEngines(edition.engines, opts);
|
227 | }
|
228 | catch (compatibleError) {
|
229 | throw util_js_1.errtion({
|
230 | message: "The edition [" + edition.description + "] is not compatible with this environment.",
|
231 | code: 'editions-autoloader-edition-incompatible',
|
232 | }, compatibleError);
|
233 | }
|
234 | }
|
235 | exports.isCompatibleEdition = isCompatibleEdition;
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function determineEdition(editions, opts) {
|
243 |
|
244 | var broadenRange = opts.broadenRange;
|
245 |
|
246 | if (!editions || editions.length === 0) {
|
247 | throw util_js_1.errtion({
|
248 | message: 'No editions were specified.',
|
249 | code: 'editions-autoloader-editions-missing',
|
250 | });
|
251 | }
|
252 |
|
253 | var failure = null;
|
254 | for (var i = 0; i < editions.length; ++i) {
|
255 | var edition = editions[i];
|
256 | try {
|
257 | isValidEdition(edition);
|
258 | isCompatibleEdition(edition, opts);
|
259 |
|
260 | return edition;
|
261 | }
|
262 | catch (error) {
|
263 | if (error.level === 'fatal') {
|
264 | throw util_js_1.errtion({
|
265 | message: "Unable to determine a suitable edition due to failure.",
|
266 | code: 'editions-autoloader-fatal',
|
267 | level: 'fatal',
|
268 | }, error);
|
269 | }
|
270 | else if (failure) {
|
271 | failure = util_js_1.errtion(error, failure);
|
272 | }
|
273 | else {
|
274 | failure = error;
|
275 | }
|
276 | }
|
277 | }
|
278 |
|
279 | if (failure) {
|
280 |
|
281 | if (broadenRange == null)
|
282 | try {
|
283 |
|
284 | var broadenedEdition = determineEdition(editions, __assign(__assign({}, opts), { broadenRange: true }));
|
285 | return __assign(__assign({}, broadenedEdition), {
|
286 |
|
287 | debugging: util_js_1.errtion({
|
288 | message: "The edition " + broadenedEdition.description + " was selected to be force loaded as its range was broadened.",
|
289 | code: 'editions-autoloader-attempt-broadened',
|
290 | }) });
|
291 | }
|
292 | catch (error) {
|
293 | throw util_js_1.errtion({
|
294 | message: "Unable to determine a suitable edition, even after broadening.",
|
295 | code: 'editions-autoloader-none-broadened',
|
296 | }, error);
|
297 | }
|
298 |
|
299 | throw util_js_1.errtion({
|
300 | message: "Unable to determine a suitable edition, as none were suitable.",
|
301 | code: 'editions-autoloader-none-suitable',
|
302 | }, failure);
|
303 | }
|
304 |
|
305 | throw util_js_1.errtion({
|
306 | message: "Unable to determine a suitable edition, as an unexpected pathway occurred.",
|
307 | code: 'editions-autoloader-never',
|
308 | });
|
309 | }
|
310 | exports.determineEdition = determineEdition;
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | function solicitEdition(editions, opts) {
|
317 | var edition = determineEdition(editions, opts);
|
318 | try {
|
319 | return loadEdition(edition, opts);
|
320 | }
|
321 | catch (error) {
|
322 | throw util_js_1.errtion(error, edition.debugging);
|
323 | }
|
324 | }
|
325 | exports.solicitEdition = solicitEdition;
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | function requirePackage(cwd, loader, entry) {
|
332 | var packagePath = path_1.resolve(cwd || '', 'package.json');
|
333 | try {
|
334 |
|
335 | var editions = JSON.parse(fs_1.readFileSync(packagePath, 'utf8')).editions;
|
336 |
|
337 | return solicitEdition(editions, {
|
338 | versions: process_1.versions,
|
339 | cwd: cwd,
|
340 | loader: loader,
|
341 | entry: entry,
|
342 | });
|
343 | }
|
344 | catch (error) {
|
345 | throw util_js_1.errtion({
|
346 | message: "Unable to determine a suitable edition for the package [" + packagePath + "] and entry [" + entry + "]",
|
347 | code: 'editions-autoloader-package',
|
348 | }, error);
|
349 | }
|
350 | }
|
351 | exports.requirePackage = requirePackage;
|