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/README.MD
CHANGED
|
@@ -153,7 +153,8 @@ import { loadSync, locateSync } from 'locter';
|
|
|
153
153
|
|
|
154
154
|
Two loaders are predefined from scratch and already registered:
|
|
155
155
|
- **ConfLoader**: This loader allows to load `.conf` files.
|
|
156
|
-
- **
|
|
156
|
+
- **JSONLoader**: This loader allows to load `.json` files.
|
|
157
|
+
- **YAMLLoader**: This loader allows to load `.yml` files.
|
|
157
158
|
- **ModuleLoader**: This loader allows to load modules with
|
|
158
159
|
`.js`, `.mjs`, `.mts`, `.cjs`, `.cts`, `.ts` file extensions independent of the environment (cjs or esm).
|
|
159
160
|
|
package/dist/index.cjs
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
var destr = require('destr');
|
|
4
4
|
var flat = require('flat');
|
|
5
5
|
var fs = require('node:fs');
|
|
6
|
-
var
|
|
6
|
+
var fg = require('fast-glob');
|
|
7
7
|
var path = require('node:path');
|
|
8
8
|
var ebec = require('ebec');
|
|
9
9
|
var createJITI = require('jiti');
|
|
10
10
|
var node_url = require('node:url');
|
|
11
|
+
var yaml = require('yaml');
|
|
11
12
|
|
|
12
13
|
function hasOwnProperty(obj, prop) {
|
|
13
14
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
@@ -78,6 +79,30 @@ function isFilePath(input) {
|
|
|
78
79
|
return extension && extension !== '';
|
|
79
80
|
}
|
|
80
81
|
|
|
82
|
+
/*
|
|
83
|
+
* Copyright (c) 2024.
|
|
84
|
+
* Author Peter Placzek (tada5hi)
|
|
85
|
+
* For the full copyright and license information,
|
|
86
|
+
* view the LICENSE file that was distributed with this source code.
|
|
87
|
+
*/ function isJestRuntimeEnvironment() {
|
|
88
|
+
return process.env && process.env.JEST_WORKER_ID !== undefined;
|
|
89
|
+
}
|
|
90
|
+
function isTsNodeRuntimeEnvironment() {
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
return !!process[Symbol.for('ts-node.register.instance')];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isTypeScriptError(input) {
|
|
97
|
+
if (!isObject(input)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (typeof input.diagnosticCodes !== 'undefined') {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return typeof input.message === 'string' && /TS\d+/.test(input.message);
|
|
104
|
+
}
|
|
105
|
+
|
|
81
106
|
function buildLocatorOptions(options) {
|
|
82
107
|
var _options;
|
|
83
108
|
options = options || {};
|
|
@@ -127,14 +152,20 @@ async function locateMany(pattern, options) {
|
|
|
127
152
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
128
153
|
pattern
|
|
129
154
|
];
|
|
155
|
+
let ignore;
|
|
156
|
+
if (options.ignore) {
|
|
157
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
158
|
+
options.ignore
|
|
159
|
+
];
|
|
160
|
+
}
|
|
130
161
|
const items = [];
|
|
131
162
|
for(let i = 0; i < patterns.length; i++){
|
|
132
163
|
for(let j = 0; j < options.path.length; j++){
|
|
133
|
-
const files = await
|
|
164
|
+
const files = await fg(patterns[i], {
|
|
134
165
|
absolute: true,
|
|
135
166
|
cwd: options.path[j],
|
|
136
|
-
|
|
137
|
-
|
|
167
|
+
ignore,
|
|
168
|
+
onlyFiles: true
|
|
138
169
|
});
|
|
139
170
|
for(let k = 0; k < files.length; k++){
|
|
140
171
|
items.push(pathToLocatorInfo(files[k], true));
|
|
@@ -148,13 +179,19 @@ async function locate(pattern, options) {
|
|
|
148
179
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
149
180
|
pattern
|
|
150
181
|
];
|
|
182
|
+
let ignore;
|
|
183
|
+
if (options.ignore) {
|
|
184
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
185
|
+
options.ignore
|
|
186
|
+
];
|
|
187
|
+
}
|
|
151
188
|
for(let i = 0; i < patterns.length; i++){
|
|
152
189
|
for(let j = 0; j < options.path.length; j++){
|
|
153
|
-
const files = await
|
|
190
|
+
const files = await fg(patterns[i], {
|
|
154
191
|
absolute: true,
|
|
155
192
|
cwd: options.path[j],
|
|
156
|
-
|
|
157
|
-
|
|
193
|
+
ignore,
|
|
194
|
+
onlyFiles: true
|
|
158
195
|
});
|
|
159
196
|
const element = files.shift();
|
|
160
197
|
if (element) {
|
|
@@ -170,14 +207,20 @@ function locateManySync(pattern, options) {
|
|
|
170
207
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
171
208
|
pattern
|
|
172
209
|
];
|
|
210
|
+
let ignore;
|
|
211
|
+
if (options.ignore) {
|
|
212
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
213
|
+
options.ignore
|
|
214
|
+
];
|
|
215
|
+
}
|
|
173
216
|
const items = [];
|
|
174
217
|
for(let i = 0; i < patterns.length; i++){
|
|
175
218
|
for(let j = 0; j < options.path.length; j++){
|
|
176
|
-
const files =
|
|
219
|
+
const files = fg.sync(patterns[i], {
|
|
177
220
|
absolute: true,
|
|
178
221
|
cwd: options.path[j],
|
|
179
|
-
|
|
180
|
-
|
|
222
|
+
ignore,
|
|
223
|
+
onlyFiles: true
|
|
181
224
|
});
|
|
182
225
|
for(let k = 0; k < files.length; k++){
|
|
183
226
|
items.push(pathToLocatorInfo(files[k], true));
|
|
@@ -191,13 +234,19 @@ function locateSync(pattern, options) {
|
|
|
191
234
|
const patterns = Array.isArray(pattern) ? pattern : [
|
|
192
235
|
pattern
|
|
193
236
|
];
|
|
237
|
+
let ignore;
|
|
238
|
+
if (options.ignore) {
|
|
239
|
+
ignore = Array.isArray(options.ignore) ? options.ignore : [
|
|
240
|
+
options.ignore
|
|
241
|
+
];
|
|
242
|
+
}
|
|
194
243
|
for(let i = 0; i < patterns.length; i++){
|
|
195
244
|
for(let j = 0; j < options.path.length; j++){
|
|
196
|
-
const files =
|
|
245
|
+
const files = fg.sync(patterns[i], {
|
|
197
246
|
absolute: true,
|
|
198
247
|
cwd: options.path[j],
|
|
199
|
-
|
|
200
|
-
|
|
248
|
+
ignore,
|
|
249
|
+
onlyFiles: true
|
|
201
250
|
});
|
|
202
251
|
const element = files.shift();
|
|
203
252
|
if (element) {
|
|
@@ -264,41 +313,60 @@ class ConfLoader {
|
|
|
264
313
|
}
|
|
265
314
|
}
|
|
266
315
|
|
|
267
|
-
function
|
|
268
|
-
return
|
|
269
|
-
|
|
270
|
-
function isTsNodeRuntimeEnvironment() {
|
|
271
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
272
|
-
// @ts-ignore
|
|
273
|
-
return !!process[Symbol.for('ts-node.register.instance')];
|
|
316
|
+
function isESModule(input) {
|
|
317
|
+
return isObject(input) && // eslint-disable-next-line no-underscore-dangle
|
|
318
|
+
typeof input.__esModule !== 'undefined';
|
|
274
319
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
320
|
+
// https://2ality.com/2017/01/babel-esm-spec-mode.html
|
|
321
|
+
function toModuleRecord(data) {
|
|
322
|
+
if (isESModule(data)) {
|
|
323
|
+
return data;
|
|
324
|
+
}
|
|
325
|
+
const output = Object.create(null, {
|
|
326
|
+
__esModule: {
|
|
327
|
+
value: true
|
|
328
|
+
},
|
|
329
|
+
[Symbol.toStringTag]: {
|
|
330
|
+
value: 'Module'
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
if (isObject(data)) {
|
|
334
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
335
|
+
for(const key in data){
|
|
336
|
+
if (hasOwnProperty(output, key)) {
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
let descriptor = Object.getOwnPropertyDescriptor(data, key);
|
|
340
|
+
if (!descriptor || 'get' in descriptor || descriptor.writable || descriptor.configurable) {
|
|
341
|
+
descriptor = {
|
|
342
|
+
enumerable: true,
|
|
343
|
+
get () {
|
|
344
|
+
return data[key];
|
|
345
|
+
}
|
|
283
346
|
};
|
|
284
347
|
}
|
|
348
|
+
Object.defineProperty(output, key, descriptor);
|
|
285
349
|
}
|
|
286
|
-
throw new ebec.BaseError('Cannot find specific module export.');
|
|
287
350
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
value = data;
|
|
351
|
+
if (!hasOwnProperty(output, 'default')) {
|
|
352
|
+
Object.defineProperty(output, 'default', {
|
|
353
|
+
value: data,
|
|
354
|
+
enumerable: true
|
|
355
|
+
});
|
|
294
356
|
}
|
|
295
|
-
|
|
296
|
-
|
|
357
|
+
return Object.freeze(output);
|
|
358
|
+
}
|
|
359
|
+
function getModuleExport(data, filterFn) {
|
|
360
|
+
const keys = Object.keys(data);
|
|
361
|
+
for(let i = 0; i < keys.length; i++){
|
|
362
|
+
if (filterFn(keys[i], data[keys[i]])) {
|
|
363
|
+
return {
|
|
364
|
+
key: keys[i],
|
|
365
|
+
value: data[keys[i]]
|
|
366
|
+
};
|
|
367
|
+
}
|
|
297
368
|
}
|
|
298
|
-
return
|
|
299
|
-
key: 'default',
|
|
300
|
-
value
|
|
301
|
-
};
|
|
369
|
+
return undefined;
|
|
302
370
|
}
|
|
303
371
|
|
|
304
372
|
class ModuleLoader {
|
|
@@ -307,7 +375,7 @@ class ModuleLoader {
|
|
|
307
375
|
try {
|
|
308
376
|
output = await this.load(input);
|
|
309
377
|
} catch (e) {
|
|
310
|
-
if (e instanceof SyntaxError || e instanceof ReferenceError) {
|
|
378
|
+
if (e instanceof SyntaxError || e instanceof ReferenceError || isTypeScriptError(e)) {
|
|
311
379
|
throw e;
|
|
312
380
|
}
|
|
313
381
|
// jiti + ts-node
|
|
@@ -318,48 +386,38 @@ class ModuleLoader {
|
|
|
318
386
|
output = this.jiti(input);
|
|
319
387
|
}
|
|
320
388
|
}
|
|
321
|
-
return output;
|
|
389
|
+
return toModuleRecord(output);
|
|
322
390
|
}
|
|
323
391
|
executeSync(input) {
|
|
324
392
|
let output;
|
|
325
393
|
try {
|
|
326
394
|
output = this.loadSync(input);
|
|
327
395
|
} catch (e) {
|
|
328
|
-
if (e instanceof SyntaxError || e instanceof ReferenceError) {
|
|
396
|
+
if (e instanceof SyntaxError || e instanceof ReferenceError || isTypeScriptError(e)) {
|
|
329
397
|
throw e;
|
|
330
398
|
}
|
|
331
399
|
output = this.jiti(input);
|
|
332
400
|
}
|
|
333
|
-
return output;
|
|
401
|
+
return toModuleRecord(output);
|
|
334
402
|
}
|
|
335
403
|
// ---------------------------------------------------------------------------
|
|
336
|
-
async load(data, options) {
|
|
337
|
-
|
|
338
|
-
const [name, locatorInfo] = this.build(data, options);
|
|
404
|
+
async load(data, options = {}) {
|
|
405
|
+
const id = this.build(data, options);
|
|
339
406
|
try {
|
|
340
407
|
// segmentation fault
|
|
341
408
|
// issue: https://github.com/nodejs/node/issues/35889
|
|
409
|
+
/*
|
|
342
410
|
if (isJestRuntimeEnvironment()) {
|
|
343
|
-
|
|
344
|
-
return require(name);
|
|
411
|
+
return require(id);
|
|
345
412
|
}
|
|
346
|
-
|
|
413
|
+
*/ return await import(id);
|
|
347
414
|
} catch (e) {
|
|
348
415
|
/* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
|
|
349
|
-
if (
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
if (!options.withFilePrefix && (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
|
|
357
|
-
return this.load(locatorInfo, {
|
|
358
|
-
...options,
|
|
359
|
-
withExtension: true,
|
|
360
|
-
withFilePrefix: true
|
|
361
|
-
});
|
|
362
|
-
}
|
|
416
|
+
if (!options.withFilePrefix && (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
|
|
417
|
+
return this.load(data, {
|
|
418
|
+
...options,
|
|
419
|
+
withFilePrefix: true
|
|
420
|
+
});
|
|
363
421
|
}
|
|
364
422
|
throw new ebec.BaseError({
|
|
365
423
|
code: e.code,
|
|
@@ -370,22 +428,13 @@ class ModuleLoader {
|
|
|
370
428
|
/* istanbul ignore next */ return handleException(e);
|
|
371
429
|
}
|
|
372
430
|
}
|
|
373
|
-
loadSync(data, options) {
|
|
374
|
-
|
|
375
|
-
const [name, locatorInfo] = this.build(data, options);
|
|
431
|
+
loadSync(data, options = {}) {
|
|
432
|
+
const id = this.build(data, options);
|
|
376
433
|
try {
|
|
377
434
|
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
378
|
-
return require(
|
|
435
|
+
return require(id);
|
|
379
436
|
} catch (e) {
|
|
380
437
|
/* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
|
|
381
|
-
if (locatorInfo) {
|
|
382
|
-
if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
|
|
383
|
-
return this.loadSync(locatorInfo, {
|
|
384
|
-
...options,
|
|
385
|
-
withExtension: true
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
438
|
throw new ebec.BaseError({
|
|
390
439
|
code: e.code,
|
|
391
440
|
message: hasStringProperty(e, 'message') ? e.message : undefined,
|
|
@@ -395,31 +444,16 @@ class ModuleLoader {
|
|
|
395
444
|
return handleException(e);
|
|
396
445
|
}
|
|
397
446
|
}
|
|
398
|
-
build(data, options) {
|
|
399
|
-
let name;
|
|
400
|
-
let locatorInfo;
|
|
401
|
-
options = options || {};
|
|
447
|
+
build(data, options = {}) {
|
|
402
448
|
if (isLocatorInfo(data) || isFilePath(data)) {
|
|
403
|
-
if (typeof data
|
|
404
|
-
|
|
405
|
-
} else {
|
|
406
|
-
locatorInfo = data;
|
|
407
|
-
}
|
|
408
|
-
if (options.withExtension) {
|
|
409
|
-
name = buildFilePath(locatorInfo);
|
|
410
|
-
} else {
|
|
411
|
-
name = buildFilePathWithoutExtension(locatorInfo);
|
|
449
|
+
if (typeof data !== 'string') {
|
|
450
|
+
data = buildFilePath(data);
|
|
412
451
|
}
|
|
413
452
|
if (options.withFilePrefix) {
|
|
414
|
-
|
|
453
|
+
data = node_url.pathToFileURL(data).href;
|
|
415
454
|
}
|
|
416
|
-
} else {
|
|
417
|
-
name = data;
|
|
418
455
|
}
|
|
419
|
-
return
|
|
420
|
-
name,
|
|
421
|
-
locatorInfo
|
|
422
|
-
];
|
|
456
|
+
return data;
|
|
423
457
|
}
|
|
424
458
|
constructor(){
|
|
425
459
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -462,6 +496,31 @@ class JSONLoader {
|
|
|
462
496
|
}
|
|
463
497
|
}
|
|
464
498
|
|
|
499
|
+
class YAMLLoader {
|
|
500
|
+
async execute(input) {
|
|
501
|
+
const filePath = buildFilePath(input);
|
|
502
|
+
try {
|
|
503
|
+
const file = await fs.promises.readFile(filePath, {
|
|
504
|
+
encoding: 'utf-8'
|
|
505
|
+
});
|
|
506
|
+
return yaml.parse(file);
|
|
507
|
+
} catch (e) {
|
|
508
|
+
return handleException(e);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
executeSync(input) {
|
|
512
|
+
const filePath = buildFilePath(input);
|
|
513
|
+
try {
|
|
514
|
+
const file = fs.readFileSync(filePath, {
|
|
515
|
+
encoding: 'utf-8'
|
|
516
|
+
});
|
|
517
|
+
return yaml.parse(file);
|
|
518
|
+
} catch (e) {
|
|
519
|
+
return handleException(e);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
465
524
|
/*
|
|
466
525
|
* Copyright (c) 2023.
|
|
467
526
|
* Author Peter Placzek (tada5hi)
|
|
@@ -472,6 +531,7 @@ class JSONLoader {
|
|
|
472
531
|
LoaderId["MODULE"] = "module";
|
|
473
532
|
LoaderId["JSON"] = "json";
|
|
474
533
|
LoaderId["CONF"] = "conf";
|
|
534
|
+
LoaderId["YAML"] = "yaml";
|
|
475
535
|
})(LoaderId || (LoaderId = {}));
|
|
476
536
|
|
|
477
537
|
class LoaderManager {
|
|
@@ -549,6 +609,11 @@ class LoaderManager {
|
|
|
549
609
|
loader = new JSONLoader();
|
|
550
610
|
break;
|
|
551
611
|
}
|
|
612
|
+
case LoaderId.YAML:
|
|
613
|
+
{
|
|
614
|
+
loader = new YAMLLoader();
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
552
617
|
/* istanbul ignore next */ default:
|
|
553
618
|
{
|
|
554
619
|
const pluginPath = this.normalizePath(id);
|
|
@@ -600,6 +665,12 @@ class LoaderManager {
|
|
|
600
665
|
'.json'
|
|
601
666
|
],
|
|
602
667
|
loader: LoaderId.JSON
|
|
668
|
+
},
|
|
669
|
+
{
|
|
670
|
+
test: [
|
|
671
|
+
'.yml'
|
|
672
|
+
],
|
|
673
|
+
loader: LoaderId.YAML
|
|
603
674
|
}
|
|
604
675
|
];
|
|
605
676
|
}
|
|
@@ -649,12 +720,14 @@ exports.getModuleExport = getModuleExport;
|
|
|
649
720
|
exports.handleException = handleException;
|
|
650
721
|
exports.hasOwnProperty = hasOwnProperty;
|
|
651
722
|
exports.hasStringProperty = hasStringProperty;
|
|
723
|
+
exports.isESModule = isESModule;
|
|
652
724
|
exports.isFilePath = isFilePath;
|
|
653
725
|
exports.isJestRuntimeEnvironment = isJestRuntimeEnvironment;
|
|
654
726
|
exports.isLocatorInfo = isLocatorInfo;
|
|
655
727
|
exports.isObject = isObject;
|
|
656
728
|
exports.isSafeObjectKey = isSafeObjectKey;
|
|
657
729
|
exports.isTsNodeRuntimeEnvironment = isTsNodeRuntimeEnvironment;
|
|
730
|
+
exports.isTypeScriptError = isTypeScriptError;
|
|
658
731
|
exports.load = load;
|
|
659
732
|
exports.loadSync = loadSync;
|
|
660
733
|
exports.locate = locate;
|
|
@@ -665,4 +738,5 @@ exports.pathToLocatorInfo = pathToLocatorInfo;
|
|
|
665
738
|
exports.registerLoader = registerLoader;
|
|
666
739
|
exports.removeFileNameExtension = removeFileNameExtension;
|
|
667
740
|
exports.toArray = toArray;
|
|
741
|
+
exports.toModuleRecord = toModuleRecord;
|
|
668
742
|
//# sourceMappingURL=index.cjs.map
|