locter 1.2.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +2 -1
- package/dist/index.cjs +171 -97
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +169 -98
- package/dist/index.mjs.map +1 -1
- package/dist/loader/built-in/module/type.d.ts +0 -1
- package/dist/loader/built-in/module/utils.d.ts +8 -3
- package/dist/loader/built-in/yaml/index.d.ts +1 -0
- package/dist/loader/built-in/yaml/module.d.ts +5 -0
- package/dist/loader/constants.d.ts +2 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/runtime.d.ts +2 -0
- package/dist/utils/typescript.d.ts +1 -0
- package/package.json +8 -7
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { destr } from 'destr';
|
|
2
2
|
import flat from 'flat';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
|
-
import
|
|
4
|
+
import fg from 'fast-glob';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { BaseError } from 'ebec';
|
|
7
7
|
import createJITI from 'jiti';
|
|
8
8
|
import { pathToFileURL } from 'node:url';
|
|
9
|
+
import { parse } from 'yaml';
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
// -- Shims --
|
|
@@ -84,6 +85,30 @@ function isFilePath(input) {
|
|
|
84
85
|
return extension && extension !== '';
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
/*
|
|
89
|
+
* Copyright (c) 2024.
|
|
90
|
+
* Author Peter Placzek (tada5hi)
|
|
91
|
+
* For the full copyright and license information,
|
|
92
|
+
* view the LICENSE file that was distributed with this source code.
|
|
93
|
+
*/ function isJestRuntimeEnvironment() {
|
|
94
|
+
return process.env && process.env.JEST_WORKER_ID !== undefined;
|
|
95
|
+
}
|
|
96
|
+
function isTsNodeRuntimeEnvironment() {
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
return !!process[Symbol.for('ts-node.register.instance')];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function isTypeScriptError(input) {
|
|
103
|
+
if (!isObject(input)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (typeof input.diagnosticCodes !== 'undefined') {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
return typeof input.message === 'string' && /TS\d+/.test(input.message);
|
|
110
|
+
}
|
|
111
|
+
|
|
87
112
|
function buildLocatorOptions(options) {
|
|
88
113
|
var _options;
|
|
89
114
|
options = options || {};
|
|
@@ -133,14 +158,20 @@ async function locateMany(pattern, options) {
|
|
|
133
158
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
134
159
|
pattern
|
|
135
160
|
];
|
|
161
|
+
let ignore;
|
|
162
|
+
if (options.ignore) {
|
|
163
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
164
|
+
options.ignore
|
|
165
|
+
];
|
|
166
|
+
}
|
|
136
167
|
const items = [];
|
|
137
168
|
for(let i = 0; i < patterns.length; i++){
|
|
138
169
|
for(let j = 0; j < options.path.length; j++){
|
|
139
|
-
const files = await
|
|
170
|
+
const files = await fg(patterns[i], {
|
|
140
171
|
absolute: true,
|
|
141
172
|
cwd: options.path[j],
|
|
142
|
-
|
|
143
|
-
|
|
173
|
+
ignore,
|
|
174
|
+
onlyFiles: true
|
|
144
175
|
});
|
|
145
176
|
for(let k = 0; k < files.length; k++){
|
|
146
177
|
items.push(pathToLocatorInfo(files[k], true));
|
|
@@ -154,13 +185,19 @@ async function locate(pattern, options) {
|
|
|
154
185
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
155
186
|
pattern
|
|
156
187
|
];
|
|
188
|
+
let ignore;
|
|
189
|
+
if (options.ignore) {
|
|
190
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
191
|
+
options.ignore
|
|
192
|
+
];
|
|
193
|
+
}
|
|
157
194
|
for(let i = 0; i < patterns.length; i++){
|
|
158
195
|
for(let j = 0; j < options.path.length; j++){
|
|
159
|
-
const files = await
|
|
196
|
+
const files = await fg(patterns[i], {
|
|
160
197
|
absolute: true,
|
|
161
198
|
cwd: options.path[j],
|
|
162
|
-
|
|
163
|
-
|
|
199
|
+
ignore,
|
|
200
|
+
onlyFiles: true
|
|
164
201
|
});
|
|
165
202
|
const element = files.shift();
|
|
166
203
|
if (element) {
|
|
@@ -176,14 +213,20 @@ function locateManySync(pattern, options) {
|
|
|
176
213
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
177
214
|
pattern
|
|
178
215
|
];
|
|
216
|
+
let ignore;
|
|
217
|
+
if (options.ignore) {
|
|
218
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
219
|
+
options.ignore
|
|
220
|
+
];
|
|
221
|
+
}
|
|
179
222
|
const items = [];
|
|
180
223
|
for(let i = 0; i < patterns.length; i++){
|
|
181
224
|
for(let j = 0; j < options.path.length; j++){
|
|
182
|
-
const files =
|
|
225
|
+
const files = fg.sync(patterns[i], {
|
|
183
226
|
absolute: true,
|
|
184
227
|
cwd: options.path[j],
|
|
185
|
-
|
|
186
|
-
|
|
228
|
+
ignore,
|
|
229
|
+
onlyFiles: true
|
|
187
230
|
});
|
|
188
231
|
for(let k = 0; k < files.length; k++){
|
|
189
232
|
items.push(pathToLocatorInfo(files[k], true));
|
|
@@ -197,13 +240,19 @@ function locateSync(pattern, options) {
|
|
|
197
240
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
198
241
|
pattern
|
|
199
242
|
];
|
|
243
|
+
let ignore;
|
|
244
|
+
if (options.ignore) {
|
|
245
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
246
|
+
options.ignore
|
|
247
|
+
];
|
|
248
|
+
}
|
|
200
249
|
for(let i = 0; i < patterns.length; i++){
|
|
201
250
|
for(let j = 0; j < options.path.length; j++){
|
|
202
|
-
const files =
|
|
251
|
+
const files = fg.sync(patterns[i], {
|
|
203
252
|
absolute: true,
|
|
204
253
|
cwd: options.path[j],
|
|
205
|
-
|
|
206
|
-
|
|
254
|
+
ignore,
|
|
255
|
+
onlyFiles: true
|
|
207
256
|
});
|
|
208
257
|
const element = files.shift();
|
|
209
258
|
if (element) {
|
|
@@ -270,41 +319,60 @@ class ConfLoader {
|
|
|
270
319
|
}
|
|
271
320
|
}
|
|
272
321
|
|
|
273
|
-
function
|
|
274
|
-
return
|
|
275
|
-
|
|
276
|
-
function isTsNodeRuntimeEnvironment() {
|
|
277
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
278
|
-
// @ts-ignore
|
|
279
|
-
return !!process[Symbol.for('ts-node.register.instance')];
|
|
322
|
+
function isESModule(input) {
|
|
323
|
+
return isObject(input) && // eslint-disable-next-line no-underscore-dangle
|
|
324
|
+
typeof input.__esModule !== 'undefined';
|
|
280
325
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
326
|
+
// https://2ality.com/2017/01/babel-esm-spec-mode.html
|
|
327
|
+
function toModuleRecord(data) {
|
|
328
|
+
if (isESModule(data)) {
|
|
329
|
+
return data;
|
|
330
|
+
}
|
|
331
|
+
const output = Object.create(null, {
|
|
332
|
+
__esModule: {
|
|
333
|
+
value: true
|
|
334
|
+
},
|
|
335
|
+
[Symbol.toStringTag]: {
|
|
336
|
+
value: 'Module'
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
if (isObject(data)) {
|
|
340
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
341
|
+
for(const key in data){
|
|
342
|
+
if (hasOwnProperty(output, key)) {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
let descriptor = Object.getOwnPropertyDescriptor(data, key);
|
|
346
|
+
if (!descriptor || 'get' in descriptor || descriptor.writable || descriptor.configurable) {
|
|
347
|
+
descriptor = {
|
|
348
|
+
enumerable: true,
|
|
349
|
+
get () {
|
|
350
|
+
return data[key];
|
|
351
|
+
}
|
|
289
352
|
};
|
|
290
353
|
}
|
|
354
|
+
Object.defineProperty(output, key, descriptor);
|
|
291
355
|
}
|
|
292
|
-
throw new BaseError('Cannot find specific module export.');
|
|
293
356
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
value = data;
|
|
357
|
+
if (!hasOwnProperty(output, 'default')) {
|
|
358
|
+
Object.defineProperty(output, 'default', {
|
|
359
|
+
value: data,
|
|
360
|
+
enumerable: true
|
|
361
|
+
});
|
|
300
362
|
}
|
|
301
|
-
|
|
302
|
-
|
|
363
|
+
return Object.freeze(output);
|
|
364
|
+
}
|
|
365
|
+
function getModuleExport(data, filterFn) {
|
|
366
|
+
const keys = Object.keys(data);
|
|
367
|
+
for(let i = 0; i < keys.length; i++){
|
|
368
|
+
if (filterFn(keys[i], data[keys[i]])) {
|
|
369
|
+
return {
|
|
370
|
+
key: keys[i],
|
|
371
|
+
value: data[keys[i]]
|
|
372
|
+
};
|
|
373
|
+
}
|
|
303
374
|
}
|
|
304
|
-
return
|
|
305
|
-
key: 'default',
|
|
306
|
-
value
|
|
307
|
-
};
|
|
375
|
+
return undefined;
|
|
308
376
|
}
|
|
309
377
|
|
|
310
378
|
class ModuleLoader {
|
|
@@ -313,7 +381,7 @@ class ModuleLoader {
|
|
|
313
381
|
try {
|
|
314
382
|
output = await this.load(input);
|
|
315
383
|
} catch (e) {
|
|
316
|
-
if (e instanceof SyntaxError || e instanceof ReferenceError) {
|
|
384
|
+
if (e instanceof SyntaxError || e instanceof ReferenceError || isTypeScriptError(e)) {
|
|
317
385
|
throw e;
|
|
318
386
|
}
|
|
319
387
|
// jiti + ts-node
|
|
@@ -324,48 +392,38 @@ class ModuleLoader {
|
|
|
324
392
|
output = this.jiti(input);
|
|
325
393
|
}
|
|
326
394
|
}
|
|
327
|
-
return output;
|
|
395
|
+
return toModuleRecord(output);
|
|
328
396
|
}
|
|
329
397
|
executeSync(input) {
|
|
330
398
|
let output;
|
|
331
399
|
try {
|
|
332
400
|
output = this.loadSync(input);
|
|
333
401
|
} catch (e) {
|
|
334
|
-
if (e instanceof SyntaxError || e instanceof ReferenceError) {
|
|
402
|
+
if (e instanceof SyntaxError || e instanceof ReferenceError || isTypeScriptError(e)) {
|
|
335
403
|
throw e;
|
|
336
404
|
}
|
|
337
405
|
output = this.jiti(input);
|
|
338
406
|
}
|
|
339
|
-
return output;
|
|
407
|
+
return toModuleRecord(output);
|
|
340
408
|
}
|
|
341
409
|
// ---------------------------------------------------------------------------
|
|
342
|
-
async load(data, options) {
|
|
343
|
-
|
|
344
|
-
const [name, locatorInfo] = this.build(data, options);
|
|
410
|
+
async load(data, options = {}) {
|
|
411
|
+
const id = this.build(data, options);
|
|
345
412
|
try {
|
|
346
413
|
// segmentation fault
|
|
347
414
|
// issue: https://github.com/nodejs/node/issues/35889
|
|
415
|
+
/*
|
|
348
416
|
if (isJestRuntimeEnvironment()) {
|
|
349
|
-
|
|
350
|
-
return require(name);
|
|
417
|
+
return require(id);
|
|
351
418
|
}
|
|
352
|
-
|
|
419
|
+
*/ return await import(id);
|
|
353
420
|
} catch (e) {
|
|
354
421
|
/* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
|
|
355
|
-
if (
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
if (!options.withFilePrefix && (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
|
|
363
|
-
return this.load(locatorInfo, {
|
|
364
|
-
...options,
|
|
365
|
-
withExtension: true,
|
|
366
|
-
withFilePrefix: true
|
|
367
|
-
});
|
|
368
|
-
}
|
|
422
|
+
if (!options.withFilePrefix && (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
|
|
423
|
+
return this.load(data, {
|
|
424
|
+
...options,
|
|
425
|
+
withFilePrefix: true
|
|
426
|
+
});
|
|
369
427
|
}
|
|
370
428
|
throw new BaseError({
|
|
371
429
|
code: e.code,
|
|
@@ -376,22 +434,13 @@ class ModuleLoader {
|
|
|
376
434
|
/* istanbul ignore next */ return handleException(e);
|
|
377
435
|
}
|
|
378
436
|
}
|
|
379
|
-
loadSync(data, options) {
|
|
380
|
-
|
|
381
|
-
const [name, locatorInfo] = this.build(data, options);
|
|
437
|
+
loadSync(data, options = {}) {
|
|
438
|
+
const id = this.build(data, options);
|
|
382
439
|
try {
|
|
383
440
|
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
384
|
-
return require(
|
|
441
|
+
return require(id);
|
|
385
442
|
} catch (e) {
|
|
386
443
|
/* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
|
|
387
|
-
if (locatorInfo) {
|
|
388
|
-
if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
|
|
389
|
-
return this.loadSync(locatorInfo, {
|
|
390
|
-
...options,
|
|
391
|
-
withExtension: true
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
444
|
throw new BaseError({
|
|
396
445
|
code: e.code,
|
|
397
446
|
message: hasStringProperty(e, 'message') ? e.message : undefined,
|
|
@@ -401,31 +450,16 @@ class ModuleLoader {
|
|
|
401
450
|
return handleException(e);
|
|
402
451
|
}
|
|
403
452
|
}
|
|
404
|
-
build(data, options) {
|
|
405
|
-
let name;
|
|
406
|
-
let locatorInfo;
|
|
407
|
-
options = options || {};
|
|
453
|
+
build(data, options = {}) {
|
|
408
454
|
if (isLocatorInfo(data) || isFilePath(data)) {
|
|
409
|
-
if (typeof data
|
|
410
|
-
|
|
411
|
-
} else {
|
|
412
|
-
locatorInfo = data;
|
|
413
|
-
}
|
|
414
|
-
if (options.withExtension) {
|
|
415
|
-
name = buildFilePath(locatorInfo);
|
|
416
|
-
} else {
|
|
417
|
-
name = buildFilePathWithoutExtension(locatorInfo);
|
|
455
|
+
if (typeof data !== 'string') {
|
|
456
|
+
data = buildFilePath(data);
|
|
418
457
|
}
|
|
419
458
|
if (options.withFilePrefix) {
|
|
420
|
-
|
|
459
|
+
data = pathToFileURL(data).href;
|
|
421
460
|
}
|
|
422
|
-
} else {
|
|
423
|
-
name = data;
|
|
424
461
|
}
|
|
425
|
-
return
|
|
426
|
-
name,
|
|
427
|
-
locatorInfo
|
|
428
|
-
];
|
|
462
|
+
return data;
|
|
429
463
|
}
|
|
430
464
|
constructor(){
|
|
431
465
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -468,6 +502,31 @@ class JSONLoader {
|
|
|
468
502
|
}
|
|
469
503
|
}
|
|
470
504
|
|
|
505
|
+
class YAMLLoader {
|
|
506
|
+
async execute(input) {
|
|
507
|
+
const filePath = buildFilePath(input);
|
|
508
|
+
try {
|
|
509
|
+
const file = await fs.promises.readFile(filePath, {
|
|
510
|
+
encoding: 'utf-8'
|
|
511
|
+
});
|
|
512
|
+
return parse(file);
|
|
513
|
+
} catch (e) {
|
|
514
|
+
return handleException(e);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
executeSync(input) {
|
|
518
|
+
const filePath = buildFilePath(input);
|
|
519
|
+
try {
|
|
520
|
+
const file = fs.readFileSync(filePath, {
|
|
521
|
+
encoding: 'utf-8'
|
|
522
|
+
});
|
|
523
|
+
return parse(file);
|
|
524
|
+
} catch (e) {
|
|
525
|
+
return handleException(e);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
471
530
|
/*
|
|
472
531
|
* Copyright (c) 2023.
|
|
473
532
|
* Author Peter Placzek (tada5hi)
|
|
@@ -478,6 +537,7 @@ class JSONLoader {
|
|
|
478
537
|
LoaderId["MODULE"] = "module";
|
|
479
538
|
LoaderId["JSON"] = "json";
|
|
480
539
|
LoaderId["CONF"] = "conf";
|
|
540
|
+
LoaderId["YAML"] = "yaml";
|
|
481
541
|
})(LoaderId || (LoaderId = {}));
|
|
482
542
|
|
|
483
543
|
class LoaderManager {
|
|
@@ -555,6 +615,11 @@ class LoaderManager {
|
|
|
555
615
|
loader = new JSONLoader();
|
|
556
616
|
break;
|
|
557
617
|
}
|
|
618
|
+
case LoaderId.YAML:
|
|
619
|
+
{
|
|
620
|
+
loader = new YAMLLoader();
|
|
621
|
+
break;
|
|
622
|
+
}
|
|
558
623
|
/* istanbul ignore next */ default:
|
|
559
624
|
{
|
|
560
625
|
const pluginPath = this.normalizePath(id);
|
|
@@ -606,6 +671,12 @@ class LoaderManager {
|
|
|
606
671
|
'.json'
|
|
607
672
|
],
|
|
608
673
|
loader: LoaderId.JSON
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
test: [
|
|
677
|
+
'.yml'
|
|
678
|
+
],
|
|
679
|
+
loader: LoaderId.YAML
|
|
609
680
|
}
|
|
610
681
|
];
|
|
611
682
|
}
|
|
@@ -643,5 +714,5 @@ function loadSync(input) {
|
|
|
643
714
|
return manager.executeSync(buildFilePath(input));
|
|
644
715
|
}
|
|
645
716
|
|
|
646
|
-
export { ConfLoader, JSONLoader, LoaderManager, ModuleLoader, buildFilePath, buildFilePathWithoutExtension, buildLocatorOptions, getFileNameExtension, getModuleExport, handleException, hasOwnProperty, hasStringProperty, isFilePath, isJestRuntimeEnvironment, isLocatorInfo, isObject, isSafeObjectKey, isTsNodeRuntimeEnvironment, load, loadSync, locate, locateMany, locateManySync, locateSync, pathToLocatorInfo, registerLoader, removeFileNameExtension, toArray };
|
|
717
|
+
export { ConfLoader, JSONLoader, LoaderManager, ModuleLoader, buildFilePath, buildFilePathWithoutExtension, buildLocatorOptions, getFileNameExtension, getModuleExport, handleException, hasOwnProperty, hasStringProperty, isESModule, isFilePath, isJestRuntimeEnvironment, isLocatorInfo, isObject, isSafeObjectKey, isTsNodeRuntimeEnvironment, isTypeScriptError, load, loadSync, locate, locateMany, locateManySync, locateSync, pathToLocatorInfo, registerLoader, removeFileNameExtension, toArray, toModuleRecord };
|
|
647
718
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/utils/has-property.ts","../src/utils/object.ts","../src/utils/error.ts","../src/utils/file-name.ts","../src/utils/to-array.ts","../src/utils/file-path.ts","../src/locator/utils.ts","../src/locator/async.ts","../src/locator/sync.ts","../src/loader/built-in/conf/module.ts","../src/loader/built-in/module/utils.ts","../src/loader/built-in/module/module.ts","../src/loader/built-in/json/module.ts","../src/loader/constants.ts","../src/loader/module.ts","../src/loader/singleton.ts","../src/loader/helpers.ts"],"sourcesContent":["export function hasOwnProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n\nexport function hasStringProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, string> {\n return hasOwnProperty(obj, prop) && typeof obj[prop] === 'string';\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isObject(item: unknown) : item is Record<string, any> {\n return (\n !!item &&\n typeof item === 'object' &&\n !Array.isArray(item)\n );\n}\n\nexport function isSafeObjectKey(key: string) : boolean {\n return key !== '__proto__' &&\n key !== 'prototype' &&\n key !== 'constructor';\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { hasOwnProperty } from './has-property';\nimport { isObject } from './object';\n\nexport function handleException(e: unknown) : void {\n if (e instanceof Error) {\n throw e;\n }\n\n const error = new Error('Can not process thrown exception.');\n\n if (isObject(e)) {\n if (\n hasOwnProperty(e, 'message') &&\n typeof e.message === 'string'\n ) {\n error.message = e.message;\n }\n\n if (\n hasOwnProperty(e, 'stack') &&\n typeof e.stack === 'string'\n ) {\n error.stack = e.stack;\n }\n }\n\n throw error;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport path from 'node:path';\n\nexport function getFileNameExtension(\n input: string,\n allowed?: string[],\n) : string | undefined {\n const extension = path.extname(input);\n if (extension === '' || extension === '.') {\n return undefined;\n }\n\n if (\n typeof allowed === 'undefined' ||\n allowed.indexOf(extension) !== -1\n ) {\n return extension;\n }\n\n return undefined;\n}\n\nexport function removeFileNameExtension(\n input: string,\n extensions?: string[],\n) {\n const extension = getFileNameExtension(input, extensions);\n if (extension) {\n return input.substring(0, input.length - extension.length);\n }\n\n return input;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function toArray(input: string | string[]) : string[] {\n return Array.isArray(input) ? input : [input];\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\nimport path from 'node:path';\n\nexport function isFilePath(input: string) {\n const extension = path.extname(input);\n return extension && extension !== '';\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport path from 'node:path';\nimport { isObject, toArray } from '../utils';\nimport type { LocatorInfo, LocatorOptions } from './type';\n\nexport function buildLocatorOptions(options?: Partial<LocatorOptions>) : LocatorOptions {\n options = options || {};\n\n options.path = options.path || [];\n options.path = toArray(options.path);\n if (options.path.length === 0) {\n options.path.push(process.cwd());\n }\n\n options.ignore ??= [];\n\n return options as LocatorOptions;\n}\n\nexport function pathToLocatorInfo(\n input: string,\n skipResolve?: boolean,\n) : LocatorInfo {\n if (\n !skipResolve &&\n !path.isAbsolute(input)\n ) {\n input = path.resolve(process.cwd(), input);\n }\n\n const info = path.parse(input);\n\n return {\n path: info.dir.split('/').join(path.sep),\n name: info.name,\n extension: info.ext,\n };\n}\n\nexport function isLocatorInfo(\n input: unknown,\n) : input is LocatorInfo {\n return isObject(input) &&\n typeof input.path === 'string' &&\n typeof input.name === 'string' &&\n typeof input.extension === 'string';\n}\n\nexport function buildFilePath(input: LocatorInfo | string) {\n if (typeof input === 'string') {\n return input;\n }\n\n if (input.extension) {\n return path.join(input.path, input.name) + input.extension;\n }\n\n return path.join(input.path, input.name);\n}\n\nexport function buildFilePathWithoutExtension(input: LocatorInfo | string) {\n let info: LocatorInfo;\n\n if (typeof input === 'string') {\n info = pathToLocatorInfo(input);\n } else {\n info = input;\n }\n\n return path.join(info.path, info.name);\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { glob } from 'glob';\nimport type { LocatorInfo, LocatorOptions } from './type';\nimport { buildLocatorOptions, pathToLocatorInfo } from './utils';\n\nexport async function locateMany(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : Promise<LocatorInfo[]> {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n const items : LocatorInfo[] = [];\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = await glob(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n nodir: true,\n ignore: options.ignore,\n });\n\n for (let k = 0; k < files.length; k++) {\n items.push(pathToLocatorInfo(files[k] as string, true));\n }\n }\n }\n\n return items;\n}\n\nexport async function locate(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : Promise<LocatorInfo | undefined> {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = await glob(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n nodir: true,\n ignore: options.ignore,\n });\n\n const element = files.shift();\n if (element) {\n return pathToLocatorInfo(element, true);\n }\n }\n }\n\n return undefined;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { globSync } from 'glob';\nimport type { LocatorInfo, LocatorOptions } from './type';\nimport { buildLocatorOptions, pathToLocatorInfo } from './utils';\n\nexport function locateManySync(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : LocatorInfo[] {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n const items : LocatorInfo[] = [];\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = globSync(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n nodir: true,\n ignore: options.ignore,\n });\n\n for (let k = 0; k < files.length; k++) {\n items.push(pathToLocatorInfo(files[k] as string, true));\n }\n }\n }\n\n return items;\n}\n\nexport function locateSync(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : LocatorInfo | undefined {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = globSync(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n nodir: true,\n ignore: options.ignore,\n });\n\n const element = files.shift();\n if (element) {\n return pathToLocatorInfo(element, true);\n }\n }\n }\n\n return undefined;\n}\n","/*\n * Copyright (c) 2022-2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// Based on https://github.com/unjs/rc9 (MIT)\n\nimport { destr } from 'destr';\nimport flat from 'flat';\nimport fs from 'node:fs';\nimport { buildFilePath } from '../../../locator';\nimport { handleException, isSafeObjectKey } from '../../../utils';\nimport type { Loader } from '../../type';\n\nexport class ConfLoader implements Loader {\n async execute(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = await fs.promises.readFile(filePath, { encoding: 'utf-8' });\n\n return this.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n executeSync(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = fs.readFileSync(filePath, { encoding: 'utf-8' });\n\n return this.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n parse(contents: string): Record<string, any> {\n const config: Record<string, any> = {};\n\n const lines = contents.split(/\\n|\\r|\\r\\n/);\n\n for (let i = 0; i < lines.length; i++) {\n const match = lines[i].match(/^\\s*([^\\s=]+)\\s*=\\s*(.*)?\\s*$/);\n if (!match) {\n continue;\n }\n\n // Key\n const key = match[1];\n\n if (!key || !isSafeObjectKey(key)) {\n continue;\n }\n\n const value = destr(match[2].trim() /* val */);\n\n if (key.endsWith('[]')) {\n const arrKey = key.slice(0, Math.max(0, key.length - 2));\n config[arrKey] = (config[arrKey] || []).concat(value);\n continue;\n }\n\n config[key] = value;\n }\n\n return flat.unflatten(config, { overwrite: true });\n }\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { BaseError } from 'ebec';\nimport { hasOwnProperty, isObject } from '../../../utils';\nimport type { LoaderFilterFn, ModuleExport } from './type';\n\nexport function isJestRuntimeEnvironment() : boolean {\n return process.env &&\n process.env.JEST_WORKER_ID !== undefined;\n}\n\nexport function isTsNodeRuntimeEnvironment() : boolean {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n return !!process[Symbol.for('ts-node.register.instance')];\n}\n\nexport function getModuleExport(\n data: Record<string, any>,\n filterFn?: LoaderFilterFn,\n): ModuleExport {\n if (filterFn) {\n const keys = Object.keys(data);\n for (let i = 0; i < keys.length; i++) {\n if (filterFn(keys[i] as string, data[keys[i] as string])) {\n return {\n key: keys[i] as string,\n value: data[keys[i] as string],\n };\n }\n }\n\n throw new BaseError('Cannot find specific module export.');\n }\n\n let value: any;\n\n if (\n hasOwnProperty(data, '__esModule') &&\n // eslint-disable-next-line no-underscore-dangle\n !!data.__esModule &&\n hasOwnProperty(data, 'default')\n ) {\n value = data.default;\n } else {\n value = data;\n }\n\n if (\n isObject(value) &&\n hasOwnProperty(value, 'default')\n ) {\n value = value.default;\n }\n\n return {\n key: 'default',\n value,\n };\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { BaseError } from 'ebec';\nimport type { JITI } from 'jiti';\nimport createJITI from 'jiti';\nimport { pathToFileURL } from 'node:url';\nimport type { LocatorInfo } from '../../../locator';\nimport {\n buildFilePath,\n buildFilePathWithoutExtension,\n isLocatorInfo,\n pathToLocatorInfo,\n} from '../../../locator';\nimport {\n handleException,\n hasStringProperty, isFilePath,\n isObject,\n} from '../../../utils';\nimport type { Loader } from '../../type';\nimport type { ModuleLoadOptions } from './type';\nimport { isJestRuntimeEnvironment, isTsNodeRuntimeEnvironment } from './utils';\n\nexport class ModuleLoader implements Loader {\n protected jiti : JITI;\n\n constructor() {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n this.jiti = createJITI(undefined, {\n extensions: ['.js', '.mjs', '.mts', '.cjs', '.cts', '.ts'],\n });\n }\n\n async execute(input: string) {\n let output : any;\n\n try {\n output = await this.load(input);\n } catch (e) {\n if (\n e instanceof SyntaxError ||\n e instanceof ReferenceError\n ) {\n throw e;\n }\n\n // jiti + ts-node\n // issue: https://github.com/nuxt/bridge/issues/228\n if (isTsNodeRuntimeEnvironment()) {\n output = this.loadSync(input);\n } else {\n output = this.jiti(input);\n }\n }\n\n return output;\n }\n\n executeSync(input: string) {\n let output : any;\n\n try {\n output = this.loadSync(input);\n } catch (e) {\n if (\n e instanceof SyntaxError ||\n e instanceof ReferenceError\n ) {\n throw e;\n }\n\n output = this.jiti(input);\n }\n\n return output;\n }\n\n // ---------------------------------------------------------------------------\n\n async load(\n data: LocatorInfo | string,\n options?: ModuleLoadOptions,\n ) : Promise<unknown> {\n options = options || {};\n const [name, locatorInfo] = this.build(data, options);\n\n try {\n // segmentation fault\n // issue: https://github.com/nodejs/node/issues/35889\n if (isJestRuntimeEnvironment()) {\n // eslint-disable-next-line global-require,import/no-dynamic-require\n return require(name);\n }\n\n return await import(name);\n } catch (e) {\n /* istanbul ignore next */\n if (\n isObject(e) &&\n hasStringProperty(e, 'code')\n ) {\n if (locatorInfo) {\n if (\n !options.withExtension &&\n (\n e.code === 'ERR_MODULE_NOT_FOUND' ||\n e.code === 'MODULE_NOT_FOUND'\n )\n ) {\n return this.load(locatorInfo, {\n ...options,\n withExtension: true,\n });\n }\n\n if (\n !options.withFilePrefix &&\n (\n e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' ||\n e.code === 'UNSUPPORTED_ESM_URL_SCHEME'\n )\n ) {\n return this.load(locatorInfo, {\n ...options,\n withExtension: true,\n withFilePrefix: true,\n });\n }\n }\n\n throw new BaseError({\n code: e.code,\n message: hasStringProperty(e, 'message') ? e.message : undefined,\n stack: hasStringProperty(e, 'stack') ? e.stack : undefined,\n });\n }\n\n /* istanbul ignore next */\n return handleException(e);\n }\n }\n\n loadSync(\n data: LocatorInfo | string,\n options?: ModuleLoadOptions,\n ) : unknown {\n options = options || {};\n const [name, locatorInfo] = this.build(data, options);\n\n try {\n // eslint-disable-next-line global-require,import/no-dynamic-require\n return require(name);\n } catch (e) {\n /* istanbul ignore next */\n if (\n isObject(e) &&\n hasStringProperty(e, 'code')\n ) {\n if (locatorInfo) {\n if (\n !options.withExtension &&\n (\n e.code === 'ERR_MODULE_NOT_FOUND' ||\n e.code === 'MODULE_NOT_FOUND'\n )\n ) {\n return this.loadSync(locatorInfo, {\n ...options,\n withExtension: true,\n });\n }\n }\n\n throw new BaseError({\n code: e.code,\n message: hasStringProperty(e, 'message') ? e.message : undefined,\n stack: hasStringProperty(e, 'stack') ? e.stack : undefined,\n });\n }\n\n return handleException(e);\n }\n }\n\n private build(\n data: LocatorInfo | string,\n options: ModuleLoadOptions,\n ) : [string, LocatorInfo | undefined] {\n let name : string;\n let locatorInfo : LocatorInfo | undefined;\n\n options = options || {};\n\n if (isLocatorInfo(data) || isFilePath(data)) {\n if (typeof data === 'string') {\n locatorInfo = pathToLocatorInfo(data);\n } else {\n locatorInfo = data;\n }\n\n if (options.withExtension) {\n name = buildFilePath(locatorInfo);\n } else {\n name = buildFilePathWithoutExtension(locatorInfo);\n }\n\n if (options.withFilePrefix) {\n name = pathToFileURL(name).href;\n }\n } else {\n name = data;\n }\n\n return [name, locatorInfo];\n }\n}\n","/*\n * Copyright (c) 2022-2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport fs from 'node:fs';\nimport { buildFilePath } from '../../../locator';\nimport { handleException } from '../../../utils';\nimport type { Loader } from '../../type';\n\nexport class JSONLoader implements Loader {\n async execute(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = await fs.promises.readFile(filePath, { encoding: 'utf-8' });\n return JSON.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n executeSync(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = fs.readFileSync(filePath, { encoding: 'utf-8' });\n return JSON.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum LoaderId {\n MODULE = 'module',\n JSON = 'json',\n CONF = 'conf',\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport path from 'node:path';\nimport { buildFilePath, pathToLocatorInfo } from '../locator';\nimport { isFilePath } from '../utils';\nimport { ConfLoader, JSONLoader, ModuleLoader } from './built-in';\nimport { LoaderId } from './constants';\nimport type { Loader, Rule } from './type';\n\nexport class LoaderManager implements Loader {\n protected loaders : Record<string, Loader>;\n\n protected rules : Rule[];\n\n constructor() {\n this.loaders = {};\n this.rules = [\n {\n test: ['.js', '.mjs', '.mts', '.cjs', '.cts', '.ts'],\n loader: LoaderId.MODULE,\n },\n {\n test: ['.conf'], loader: LoaderId.CONF,\n },\n {\n test: ['.json'], loader: LoaderId.JSON,\n },\n ];\n }\n\n register(rule: Rule) : void;\n\n register(test: string[] | RegExp, loader: Loader) : void;\n\n register(test: any, loader?: Loader) : void {\n if (typeof loader !== 'undefined') {\n this.rules.push({ test, loader });\n return;\n }\n\n this.rules.push(test);\n }\n\n async execute(input: string) : Promise<any> {\n const id = this.findLoader(input);\n if (!id) {\n const info = pathToLocatorInfo(input);\n throw new Error(`No loader registered for extension: \"${info.extension}\"`);\n }\n\n const loader = this.resolve(id);\n return loader.execute(input);\n }\n\n executeSync(input: string) : any {\n const id = this.findLoader(input);\n if (!id) {\n const info = pathToLocatorInfo(input);\n throw new Error(`No loader registered for extension: ${info.extension || 'unknown'}`);\n }\n\n const loader = this.resolve(id);\n return loader.executeSync(input);\n }\n\n findLoader(input: string) : Loader | string | undefined {\n if (!isFilePath(input)) {\n return LoaderId.MODULE;\n }\n\n const info = pathToLocatorInfo(input);\n for (let i = 0; i < this.rules.length; i++) {\n const { test } = this.rules[i] as Rule;\n if (Array.isArray(test)) {\n if (test.indexOf(info.extension) !== -1) {\n return this.rules[i].loader;\n }\n } else if (test.test(buildFilePath(info))) {\n return this.rules[i].loader;\n }\n }\n\n return undefined;\n }\n\n /**\n * Resolve loader by id.\n *\n * @param id\n */\n resolve(id: string | Loader) : Loader {\n if (typeof id !== 'string') {\n return id;\n }\n\n if (Object.prototype.hasOwnProperty.call(this.loaders, id)) {\n return this.loaders[id] as Loader;\n }\n\n let loader : Loader | undefined;\n\n // built-in\n switch (id) {\n case LoaderId.CONF: {\n loader = new ConfLoader();\n break;\n }\n case LoaderId.MODULE: {\n loader = new ModuleLoader();\n break;\n }\n case LoaderId.JSON: {\n loader = new JSONLoader();\n break;\n }\n /* istanbul ignore next */\n default: {\n const pluginPath = this.normalizePath(id);\n const moduleLoader = this.resolve(LoaderId.MODULE);\n loader = moduleLoader.executeSync(pluginPath);\n\n break;\n }\n }\n\n if (typeof loader !== 'undefined') {\n this.loaders[id] = loader;\n\n return loader;\n }\n\n throw new Error(`The loader ${id} could not be resolved.`);\n }\n\n /* istanbul ignore next */\n normalizePath(input: string) {\n if (path.isAbsolute(input) || input.startsWith('./')) {\n return input;\n }\n\n if (input.startsWith('module:')) {\n return input.substring(0, 'module:'.length);\n }\n\n if (!input.startsWith('@')) {\n return `@locter/${input}`;\n }\n\n return input;\n }\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { LoaderManager } from './module';\n\nlet instance : LoaderManager;\nexport function useLoader() {\n if (typeof instance !== 'undefined') {\n return instance;\n }\n\n instance = new LoaderManager();\n\n return instance;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { buildFilePath } from '../locator';\nimport type { LocatorInfo } from '../locator';\nimport { useLoader } from './singleton';\nimport type { Loader, Rule } from './type';\n\nexport function registerLoader(rule: Rule) : void;\nexport function registerLoader(test: string[] | RegExp, loader: Loader) : void;\nexport function registerLoader(test: any, loader?: Loader) : void {\n const manager = useLoader();\n if (typeof loader !== 'undefined') {\n manager.register(test, loader);\n\n return;\n }\n\n manager.register(test);\n}\n\nexport async function load(input: LocatorInfo | string) : Promise<any> {\n const manager = useLoader();\n if (typeof input === 'string') {\n return manager.execute(input);\n }\n\n return manager.execute(buildFilePath(input));\n}\n\nexport function loadSync(input: LocatorInfo | string) : any {\n const manager = useLoader();\n if (typeof input === 'string') {\n return manager.executeSync(input);\n }\n\n return manager.executeSync(buildFilePath(input));\n}\n"],"names":["input","options","name","extension","info","pattern","cwd","ignore","items","executeSync","parse","config","value","output","code","message","stack","locatorInfo","LoaderId","test","loader","findLoader","instance","manager"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/utils/has-property.ts","../src/utils/object.ts","../src/utils/error.ts","../src/utils/file-name.ts","../src/utils/to-array.ts","../src/utils/file-path.ts","../src/utils/runtime.ts","../src/utils/typescript.ts","../src/locator/utils.ts","../src/locator/async.ts","../src/locator/sync.ts","../src/loader/built-in/conf/module.ts","../src/loader/built-in/module/utils.ts","../src/loader/built-in/module/module.ts","../src/loader/built-in/json/module.ts","../src/loader/built-in/yaml/module.ts","../src/loader/constants.ts","../src/loader/module.ts","../src/loader/singleton.ts","../src/loader/helpers.ts"],"sourcesContent":["export function hasOwnProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n\nexport function hasStringProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, string> {\n return hasOwnProperty(obj, prop) && typeof obj[prop] === 'string';\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isObject(item: unknown) : item is Record<string, any> {\n return (\n !!item &&\n typeof item === 'object' &&\n !Array.isArray(item)\n );\n}\n\nexport function isSafeObjectKey(key: string) : boolean {\n return key !== '__proto__' &&\n key !== 'prototype' &&\n key !== 'constructor';\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { hasOwnProperty } from './has-property';\nimport { isObject } from './object';\n\nexport function handleException(e: unknown) : void {\n if (e instanceof Error) {\n throw e;\n }\n\n const error = new Error('Can not process thrown exception.');\n\n if (isObject(e)) {\n if (\n hasOwnProperty(e, 'message') &&\n typeof e.message === 'string'\n ) {\n error.message = e.message;\n }\n\n if (\n hasOwnProperty(e, 'stack') &&\n typeof e.stack === 'string'\n ) {\n error.stack = e.stack;\n }\n }\n\n throw error;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport path from 'node:path';\n\nexport function getFileNameExtension(\n input: string,\n allowed?: string[],\n) : string | undefined {\n const extension = path.extname(input);\n if (extension === '' || extension === '.') {\n return undefined;\n }\n\n if (\n typeof allowed === 'undefined' ||\n allowed.indexOf(extension) !== -1\n ) {\n return extension;\n }\n\n return undefined;\n}\n\nexport function removeFileNameExtension(\n input: string,\n extensions?: string[],\n) {\n const extension = getFileNameExtension(input, extensions);\n if (extension) {\n return input.substring(0, input.length - extension.length);\n }\n\n return input;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function toArray(input: string | string[]) : string[] {\n return Array.isArray(input) ? input : [input];\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\nimport path from 'node:path';\n\nexport function isFilePath(input: string) {\n const extension = path.extname(input);\n return extension && extension !== '';\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isJestRuntimeEnvironment() : boolean {\n return process.env &&\n process.env.JEST_WORKER_ID !== undefined;\n}\n\nexport function isTsNodeRuntimeEnvironment(): boolean {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n return !!process[Symbol.for('ts-node.register.instance')];\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from './object';\n\nexport function isTypeScriptError(input: unknown) : input is Error {\n if (!isObject(input)) {\n return false;\n }\n\n if (typeof input.diagnosticCodes !== 'undefined') {\n return true;\n }\n\n return typeof input.message === 'string' &&\n /TS\\d+/.test(input.message);\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport path from 'node:path';\nimport { isObject, toArray } from '../utils';\nimport type { LocatorInfo, LocatorOptions } from './type';\n\nexport function buildLocatorOptions(options?: Partial<LocatorOptions>) : LocatorOptions {\n options = options || {};\n\n options.path = options.path || [];\n options.path = toArray(options.path);\n if (options.path.length === 0) {\n options.path.push(process.cwd());\n }\n\n options.ignore ??= [];\n\n return options as LocatorOptions;\n}\n\nexport function pathToLocatorInfo(\n input: string,\n skipResolve?: boolean,\n) : LocatorInfo {\n if (\n !skipResolve &&\n !path.isAbsolute(input)\n ) {\n input = path.resolve(process.cwd(), input);\n }\n\n const info = path.parse(input);\n\n return {\n path: info.dir.split('/').join(path.sep),\n name: info.name,\n extension: info.ext,\n };\n}\n\nexport function isLocatorInfo(\n input: unknown,\n) : input is LocatorInfo {\n return isObject(input) &&\n typeof input.path === 'string' &&\n typeof input.name === 'string' &&\n typeof input.extension === 'string';\n}\n\nexport function buildFilePath(input: LocatorInfo | string) {\n if (typeof input === 'string') {\n return input;\n }\n\n if (input.extension) {\n return path.join(input.path, input.name) + input.extension;\n }\n\n return path.join(input.path, input.name);\n}\n\nexport function buildFilePathWithoutExtension(input: LocatorInfo | string) {\n let info: LocatorInfo;\n\n if (typeof input === 'string') {\n info = pathToLocatorInfo(input);\n } else {\n info = input;\n }\n\n return path.join(info.path, info.name);\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport fg from 'fast-glob';\nimport type { LocatorInfo, LocatorOptions } from './type';\nimport { buildLocatorOptions, pathToLocatorInfo } from './utils';\n\nexport async function locateMany(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : Promise<LocatorInfo[]> {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n let ignore : string[] | undefined;\n if (options.ignore) {\n ignore = Array.isArray(options.ignore) ? options.ignore : [options.ignore];\n }\n\n const items : LocatorInfo[] = [];\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = await fg(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n ignore,\n onlyFiles: true,\n });\n\n for (let k = 0; k < files.length; k++) {\n items.push(pathToLocatorInfo(files[k] as string, true));\n }\n }\n }\n\n return items;\n}\n\nexport async function locate(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : Promise<LocatorInfo | undefined> {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n let ignore : string[] | undefined;\n if (options.ignore) {\n ignore = Array.isArray(options.ignore) ? options.ignore : [options.ignore];\n }\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = await fg(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n ignore,\n onlyFiles: true,\n });\n\n const element = files.shift();\n if (element) {\n return pathToLocatorInfo(element, true);\n }\n }\n }\n\n return undefined;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport fg from 'fast-glob';\nimport type { LocatorInfo, LocatorOptions } from './type';\nimport { buildLocatorOptions, pathToLocatorInfo } from './utils';\n\nexport function locateManySync(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : LocatorInfo[] {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n let ignore : string[] | undefined;\n if (options.ignore) {\n ignore = Array.isArray(options.ignore) ? options.ignore : [options.ignore];\n }\n\n const items : LocatorInfo[] = [];\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = fg.sync(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n ignore,\n onlyFiles: true,\n });\n\n for (let k = 0; k < files.length; k++) {\n items.push(pathToLocatorInfo(files[k] as string, true));\n }\n }\n }\n\n return items;\n}\n\nexport function locateSync(\n pattern: string | string[],\n options?: Partial<LocatorOptions>,\n) : LocatorInfo | undefined {\n options = buildLocatorOptions(options);\n\n const patterns = Array.isArray(pattern) ?\n pattern :\n [pattern];\n\n let ignore : string[] | undefined;\n if (options.ignore) {\n ignore = Array.isArray(options.ignore) ? options.ignore : [options.ignore];\n }\n\n for (let i = 0; i < patterns.length; i++) {\n for (let j = 0; j < (options as LocatorOptions).path.length; j++) {\n const files = fg.sync(patterns[i] as string, {\n absolute: true,\n cwd: (options as LocatorOptions).path[j],\n ignore,\n onlyFiles: true,\n });\n\n const element = files.shift();\n if (element) {\n return pathToLocatorInfo(element, true);\n }\n }\n }\n\n return undefined;\n}\n","/*\n * Copyright (c) 2022-2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// Based on https://github.com/unjs/rc9 (MIT)\n\nimport { destr } from 'destr';\nimport flat from 'flat';\nimport fs from 'node:fs';\nimport { buildFilePath } from '../../../locator';\nimport { handleException, isSafeObjectKey } from '../../../utils';\nimport type { Loader } from '../../type';\n\nexport class ConfLoader implements Loader {\n async execute(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = await fs.promises.readFile(filePath, { encoding: 'utf-8' });\n\n return this.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n executeSync(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = fs.readFileSync(filePath, { encoding: 'utf-8' });\n\n return this.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n parse(contents: string): Record<string, any> {\n const config: Record<string, any> = {};\n\n const lines = contents.split(/\\n|\\r|\\r\\n/);\n\n for (let i = 0; i < lines.length; i++) {\n const match = lines[i].match(/^\\s*([^\\s=]+)\\s*=\\s*(.*)?\\s*$/);\n if (!match) {\n continue;\n }\n\n // Key\n const key = match[1];\n\n if (!key || !isSafeObjectKey(key)) {\n continue;\n }\n\n const value = destr(match[2].trim() /* val */);\n\n if (key.endsWith('[]')) {\n const arrKey = key.slice(0, Math.max(0, key.length - 2));\n config[arrKey] = (config[arrKey] || []).concat(value);\n continue;\n }\n\n config[key] = value;\n }\n\n return flat.unflatten(config, { overwrite: true });\n }\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { hasOwnProperty, isObject } from '../../../utils';\nimport type { LoaderFilterFn, ModuleExport } from './type';\n\ntype ESModule = { [key: string]: any, __esModule: boolean };\nexport function isESModule(input: unknown) : input is ESModule {\n return isObject(input) &&\n // eslint-disable-next-line no-underscore-dangle\n typeof input.__esModule !== 'undefined';\n}\n\n// https://2ality.com/2017/01/babel-esm-spec-mode.html\nexport function toModuleRecord(\n data: unknown,\n) {\n if (isESModule(data)) {\n return data;\n }\n\n const output = Object.create(null, {\n __esModule: {\n value: true,\n },\n [Symbol.toStringTag]: {\n value: 'Module',\n },\n });\n\n if (isObject(data)) {\n // eslint-disable-next-line no-restricted-syntax\n for (const key in data) {\n if (hasOwnProperty(output, key)) {\n continue;\n }\n\n let descriptor = Object.getOwnPropertyDescriptor(data, key);\n if (\n !descriptor ||\n ('get' in descriptor || descriptor.writable || descriptor.configurable)\n ) {\n descriptor = {\n enumerable: true,\n get() {\n return data[key];\n },\n };\n }\n\n Object.defineProperty(output, key, descriptor);\n }\n }\n\n if (!hasOwnProperty(output, 'default')) {\n Object.defineProperty(output, 'default', {\n value: data,\n enumerable: true,\n });\n }\n\n return Object.freeze(output);\n}\n\nexport function getModuleExport(\n data: Record<string, any>,\n filterFn: LoaderFilterFn,\n): ModuleExport | undefined {\n const keys = Object.keys(data);\n for (let i = 0; i < keys.length; i++) {\n if (filterFn(keys[i] as string, data[keys[i] as string])) {\n return {\n key: keys[i] as string,\n value: data[keys[i] as string],\n };\n }\n }\n\n return undefined;\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { BaseError } from 'ebec';\nimport type { JITI } from 'jiti';\nimport createJITI from 'jiti';\nimport { pathToFileURL } from 'node:url';\nimport type { LocatorInfo } from '../../../locator';\nimport {\n buildFilePath,\n isLocatorInfo,\n} from '../../../locator';\nimport {\n handleException,\n hasStringProperty,\n isFilePath,\n isObject,\n isTsNodeRuntimeEnvironment,\n isTypeScriptError,\n} from '../../../utils';\nimport type { Loader } from '../../type';\nimport type { ModuleLoadOptions } from './type';\nimport { toModuleRecord } from './utils';\n\nexport class ModuleLoader implements Loader {\n protected jiti : JITI;\n\n constructor() {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n this.jiti = createJITI(undefined, {\n extensions: ['.js', '.mjs', '.mts', '.cjs', '.cts', '.ts'],\n });\n }\n\n async execute(input: string) {\n let output : any;\n\n try {\n output = await this.load(input);\n } catch (e) {\n if (\n e instanceof SyntaxError ||\n e instanceof ReferenceError ||\n isTypeScriptError(e)\n ) {\n throw e;\n }\n\n // jiti + ts-node\n // issue: https://github.com/nuxt/bridge/issues/228\n if (isTsNodeRuntimeEnvironment()) {\n output = this.loadSync(input);\n } else {\n output = this.jiti(input);\n }\n }\n\n return toModuleRecord(output);\n }\n\n executeSync(input: string) {\n let output : any;\n\n try {\n output = this.loadSync(input);\n } catch (e) {\n if (\n e instanceof SyntaxError ||\n e instanceof ReferenceError ||\n isTypeScriptError(e)\n ) {\n throw e;\n }\n\n output = this.jiti(input);\n }\n\n return toModuleRecord(output);\n }\n\n // ---------------------------------------------------------------------------\n\n async load(\n data: LocatorInfo | string,\n options: ModuleLoadOptions = {},\n ) : Promise<unknown> {\n const id = this.build(data, options);\n\n try {\n // segmentation fault\n // issue: https://github.com/nodejs/node/issues/35889\n /*\n if (isJestRuntimeEnvironment()) {\n return require(id);\n }\n */\n\n return await import(id);\n } catch (e) {\n /* istanbul ignore next */\n if (\n isObject(e) &&\n hasStringProperty(e, 'code')\n ) {\n if (\n !options.withFilePrefix &&\n (\n e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' ||\n e.code === 'UNSUPPORTED_ESM_URL_SCHEME'\n )\n ) {\n return this.load(data, {\n ...options,\n withFilePrefix: true,\n });\n }\n\n throw new BaseError({\n code: e.code,\n message: hasStringProperty(e, 'message') ? e.message : undefined,\n stack: hasStringProperty(e, 'stack') ? e.stack : undefined,\n });\n }\n\n /* istanbul ignore next */\n return handleException(e);\n }\n }\n\n loadSync(\n data: LocatorInfo | string,\n options: ModuleLoadOptions = {},\n ) : unknown {\n const id = this.build(data, options);\n\n try {\n // eslint-disable-next-line global-require,import/no-dynamic-require\n return require(id);\n } catch (e) {\n /* istanbul ignore next */\n if (\n isObject(e) &&\n hasStringProperty(e, 'code')\n ) {\n throw new BaseError({\n code: e.code,\n message: hasStringProperty(e, 'message') ? e.message : undefined,\n stack: hasStringProperty(e, 'stack') ? e.stack : undefined,\n });\n }\n\n return handleException(e);\n }\n }\n\n private build(\n data: LocatorInfo | string,\n options: ModuleLoadOptions = {},\n ) : string {\n if (isLocatorInfo(data) || isFilePath(data)) {\n if (typeof data !== 'string') {\n data = buildFilePath(data);\n }\n\n if (options.withFilePrefix) {\n data = pathToFileURL(data).href;\n }\n }\n\n return data;\n }\n}\n","/*\n * Copyright (c) 2022-2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport fs from 'node:fs';\nimport { buildFilePath } from '../../../locator';\nimport { handleException } from '../../../utils';\nimport type { Loader } from '../../type';\n\nexport class JSONLoader implements Loader {\n async execute(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = await fs.promises.readFile(filePath, { encoding: 'utf-8' });\n return JSON.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n executeSync(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = fs.readFileSync(filePath, { encoding: 'utf-8' });\n return JSON.parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n}\n","/*\n * Copyright (c) 2022-2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { parse } from 'yaml';\nimport fs from 'node:fs';\nimport { buildFilePath } from '../../../locator';\nimport { handleException } from '../../../utils';\nimport type { Loader } from '../../type';\n\nexport class YAMLLoader implements Loader {\n async execute(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = await fs.promises.readFile(filePath, { encoding: 'utf-8' });\n return parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n\n executeSync(input: string) {\n const filePath = buildFilePath(input);\n\n try {\n const file = fs.readFileSync(filePath, { encoding: 'utf-8' });\n return parse(file);\n } catch (e) {\n return handleException(e);\n }\n }\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum LoaderId {\n MODULE = 'module',\n JSON = 'json',\n CONF = 'conf',\n YAML = 'yaml',\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport path from 'node:path';\nimport { buildFilePath, pathToLocatorInfo } from '../locator';\nimport { isFilePath } from '../utils';\nimport { ConfLoader, JSONLoader, ModuleLoader } from './built-in';\nimport { YAMLLoader } from './built-in/yaml';\nimport { LoaderId } from './constants';\nimport type { Loader, Rule } from './type';\n\nexport class LoaderManager implements Loader {\n protected loaders : Record<string, Loader>;\n\n protected rules : Rule[];\n\n constructor() {\n this.loaders = {};\n this.rules = [\n {\n test: ['.js', '.mjs', '.mts', '.cjs', '.cts', '.ts'],\n loader: LoaderId.MODULE,\n },\n {\n test: ['.conf'], loader: LoaderId.CONF,\n },\n {\n test: ['.json'], loader: LoaderId.JSON,\n },\n {\n test: ['.yml'], loader: LoaderId.YAML,\n },\n ];\n }\n\n register(rule: Rule) : void;\n\n register(test: string[] | RegExp, loader: Loader) : void;\n\n register(test: any, loader?: Loader) : void {\n if (typeof loader !== 'undefined') {\n this.rules.push({ test, loader });\n return;\n }\n\n this.rules.push(test);\n }\n\n async execute(input: string) : Promise<any> {\n const id = this.findLoader(input);\n if (!id) {\n const info = pathToLocatorInfo(input);\n throw new Error(`No loader registered for extension: \"${info.extension}\"`);\n }\n\n const loader = this.resolve(id);\n return loader.execute(input);\n }\n\n executeSync(input: string) : any {\n const id = this.findLoader(input);\n if (!id) {\n const info = pathToLocatorInfo(input);\n throw new Error(`No loader registered for extension: ${info.extension || 'unknown'}`);\n }\n\n const loader = this.resolve(id);\n return loader.executeSync(input);\n }\n\n findLoader(input: string) : Loader | string | undefined {\n if (!isFilePath(input)) {\n return LoaderId.MODULE;\n }\n\n const info = pathToLocatorInfo(input);\n for (let i = 0; i < this.rules.length; i++) {\n const { test } = this.rules[i] as Rule;\n if (Array.isArray(test)) {\n if (test.indexOf(info.extension) !== -1) {\n return this.rules[i].loader;\n }\n } else if (test.test(buildFilePath(info))) {\n return this.rules[i].loader;\n }\n }\n\n return undefined;\n }\n\n /**\n * Resolve loader by id.\n *\n * @param id\n */\n resolve(id: string | Loader) : Loader {\n if (typeof id !== 'string') {\n return id;\n }\n\n if (Object.prototype.hasOwnProperty.call(this.loaders, id)) {\n return this.loaders[id] as Loader;\n }\n\n let loader : Loader | undefined;\n\n // built-in\n switch (id) {\n case LoaderId.CONF: {\n loader = new ConfLoader();\n break;\n }\n case LoaderId.MODULE: {\n loader = new ModuleLoader();\n break;\n }\n case LoaderId.JSON: {\n loader = new JSONLoader();\n break;\n }\n case LoaderId.YAML: {\n loader = new YAMLLoader();\n break;\n }\n /* istanbul ignore next */\n default: {\n const pluginPath = this.normalizePath(id);\n const moduleLoader = this.resolve(LoaderId.MODULE);\n loader = moduleLoader.executeSync(pluginPath);\n\n break;\n }\n }\n\n if (typeof loader !== 'undefined') {\n this.loaders[id] = loader;\n\n return loader;\n }\n\n throw new Error(`The loader ${id} could not be resolved.`);\n }\n\n /* istanbul ignore next */\n normalizePath(input: string) {\n if (path.isAbsolute(input) || input.startsWith('./')) {\n return input;\n }\n\n if (input.startsWith('module:')) {\n return input.substring(0, 'module:'.length);\n }\n\n if (!input.startsWith('@')) {\n return `@locter/${input}`;\n }\n\n return input;\n }\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { LoaderManager } from './module';\n\nlet instance : LoaderManager;\nexport function useLoader() {\n if (typeof instance !== 'undefined') {\n return instance;\n }\n\n instance = new LoaderManager();\n\n return instance;\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { buildFilePath } from '../locator';\nimport type { LocatorInfo } from '../locator';\nimport { useLoader } from './singleton';\nimport type { Loader, Rule } from './type';\n\nexport function registerLoader(rule: Rule) : void;\nexport function registerLoader(test: string[] | RegExp, loader: Loader) : void;\nexport function registerLoader(test: any, loader?: Loader) : void {\n const manager = useLoader();\n if (typeof loader !== 'undefined') {\n manager.register(test, loader);\n\n return;\n }\n\n manager.register(test);\n}\n\nexport async function load(input: LocatorInfo | string) : Promise<any> {\n const manager = useLoader();\n if (typeof input === 'string') {\n return manager.execute(input);\n }\n\n return manager.execute(buildFilePath(input));\n}\n\nexport function loadSync(input: LocatorInfo | string) : any {\n const manager = useLoader();\n if (typeof input === 'string') {\n return manager.executeSync(input);\n }\n\n return manager.executeSync(buildFilePath(input));\n}\n"],"names":["input","options","name","extension","info","pattern","cwd","ignore","items","executeSync","parse","config","get","value","output","code","message","stack","loadSync","build","data","LoaderId","test","loader","findLoader","instance","manager"],"mappings":";;;;;;;;;;;;;;;;;;AAAO;AACH;AACJ;AAEO;AACH;AACJ;;ACNA;;;;;;;AAaA;AAEO;AACH;AAGJ;;ACTO;AACH;;AAEA;;AAIA;AACI;;AAKA;AAEA;;AAKA;AACJ;;AAGJ;;ACzBO;;;;AAOH;;;AAOA;;AAGJ;AAEO;;AAKH;;AAEA;;AAGJ;;ACtCA;;;;;;AAQI;AAAuCA;AAAM;AACjD;;ACDO;;AAEH;AACJ;;ACXA;;;;;AAKC;AAGG;AAEJ;AAEO;;;AAGH;AACJ;;ACPO;;;AAGH;AAEA;;AAEA;;AAIJ;;ACTO;AASHC;AARAA;AAEAA;AACAA;AACA;AACIA;AACJ;AAEAA;;AAGJ;AAEO;AAIH;AAIID;AACJ;;;;AAMIE;AACAC;AACJ;AACJ;AAEO;AAGH;AAIJ;AAEO;;;AAGH;;;AAIA;AAEA;AACJ;AAEO;;;AAICC;;;AAGJ;AAEA;AACJ;;ACjEO;AAIHH;AAEA;AAEKI;AAAQ;;;;AAIkDJ;AAAe;AAC9E;AAEA;AAEA;;AAEQ;;AAEIK;AACAC;;AAEJ;AAEA;AACIC;AACJ;AACJ;AACJ;;AAGJ;AAEO;AAIHP;AAEA;AAEKI;AAAQ;;;;AAIkDJ;AAAe;AAC9E;AAEA;;AAEQ;;AAEIK;AACAC;;AAEJ;;AAGA;AACI;AACJ;AACJ;AACJ;;AAGJ;;ACnEO;AAIHN;AAEA;AAEKI;AAAQ;;;;AAIkDJ;AAAe;AAC9E;AAEA;AAEA;;AAEQ;;AAEIK;AACAC;;AAEJ;AAEA;AACIC;AACJ;AACJ;AACJ;;AAGJ;AAEO;AAIHP;AAEA;AAEKI;AAAQ;;;;AAIkDJ;AAAe;AAC9E;AAEA;;AAEQ;;AAEIK;AACAC;;AAEJ;;AAGA;AACI;AACJ;AACJ;AACJ;;AAGJ;;AC9EA;;;;;AAKC;AAWM;;AAEC;;AAGI;;AAAsE;;AAG1E;AACI;AACJ;AACJ;AAEAE;AACI;;AAGI;;AAA2D;;AAG/D;AACI;AACJ;AACJ;AAEAC;AACI;;AAIA;AACI;AACA;AACI;AACJ;;;AAKA;AACI;AACJ;AAEA;;;AAIIC;AACA;AACJ;;AAGJ;;;AAEgD;AACpD;AACJ;;AC7DO;;;AAIP;AAEA;AACO;AAGH;;AAEA;AAEA;;;AAGI;;;AAGA;AACJ;AAEA;;;;AAIY;AACJ;AAEA;;;;AAOQC;;AAEA;AACJ;AACJ;;AAGJ;AACJ;;;;;AAMI;AACJ;;AAGJ;AAEO;;AAKH;;;;AAIYC;AACJ;AACJ;AACJ;;AAGJ;;ACvDO;;;;AAeKC;AACJ;AACI;;AAMA;;;AAIA;;;;AAIA;AACJ;AAEA;AACJ;AAEAL;;;;AAKI;AACI;;AAMA;;AAGJ;AAEA;AACJ;;AAIA;AAII;;;;AAKI;;;;;AAOJ;AACI;AAKI;AAOI;AACI;;AAEJ;AACJ;AAEA;AACIM;AACAC;AACAC;AACJ;AACJ;;AAIJ;AACJ;AAEAC;AAII;;;AAII;AACJ;AACI;AAKI;AACIH;AACAC;AACAC;AACJ;AACJ;AAEA;AACJ;AACJ;AAEQE;;;AAMIC;AACJ;;;AAIA;AACJ;;AAGJ;;;;AA7II;;AACiB;AAAO;AAAQ;AAAQ;AAAQ;AAAQ;AAAM;AAC9D;AACJ;AA2IJ;;ACpKO;;AAEC;;AAGI;;AAAsE;;AAE1E;AACI;AACJ;AACJ;AAEAX;AACI;;AAGI;;AAA2D;;AAE/D;AACI;AACJ;AACJ;AACJ;;ACrBO;;AAEC;;AAGI;;AAAsE;AACtE;AACJ;AACI;AACJ;AACJ;AAEAA;AACI;;AAGI;;AAA2D;AAC3D;AACJ;AACI;AACJ;AACJ;AACJ;;ACnCA;;;;;AAKC;AAEWY;;;;;;;ACQL;;;AA8BK;AAAkBC;AAAMC;AAAO;AAC/B;AACJ;AAEA;AACJ;;AAGI;AACA;AACI;;AAEJ;AAEA;;AAEJ;AAEAd;AACI;AACA;AACI;;AAEJ;AAEA;;AAEJ;AAEAe;;AAEQ;AACJ;AAEA;;;;AAIQ;AACI;AACJ;AACJ;AACI;AACJ;AACJ;;AAGJ;AAEA;;;;;;;AAQI;;AAGI;AACJ;;;;AAMI;AAAoB;AAChBD;AACA;AACJ;AACA;AAAsB;AAClBA;AACA;AACJ;AACA;AAAoB;AAChBA;AACA;AACJ;AACA;AAAoB;AAChBA;AACA;AACJ;AACA;AACS;AACL;AACA;;AAGA;AACJ;AACJ;;AAGI;;AAGJ;AAEA;AACJ;;AAII;;AAEA;;AAGI;AACJ;AAEA;AACI;AACJ;;AAGJ;;;;AA3IQ;;AACW;AAAO;AAAQ;AAAQ;AAAQ;AAAQ;AAAM;AACpDA;AACJ;AACA;;AACW;AAAQ;AAAEA;AACrB;AACA;;AACW;AAAQ;AAAEA;AACrB;AACA;;AACW;AAAO;AAAEA;AACpB;AACH;AACL;AA8HJ;;AC1JA;AACO;;;AAGH;AAEAE;;AAGJ;;ACJO;AACH;;;AAII;AACJ;AAEAC;AACJ;AAEO;AACH;;;AAGA;;AAGJ;AAEO;AACH;;;AAGA;;AAGJ;;"}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { LoaderFilterFn, ModuleExport } from './type';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
type ESModule = {
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
__esModule: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare function isESModule(input: unknown): input is ESModule;
|
|
7
|
+
export declare function toModuleRecord(data: unknown): any;
|
|
8
|
+
export declare function getModuleExport(data: Record<string, any>, filterFn: LoaderFilterFn): ModuleExport | undefined;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './module';
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isTypeScriptError(input: unknown): input is Error;
|