locter 2.2.1 → 3.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 +188 -6
- package/dist/index.d.mts +244 -0
- package/dist/index.mjs +688 -672
- package/dist/index.mjs.map +1 -1
- package/package.json +27 -34
- package/dist/index.cjs +0 -743
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/loader/built-in/conf/index.d.ts +0 -1
- package/dist/loader/built-in/conf/module.d.ts +0 -6
- package/dist/loader/built-in/index.d.ts +0 -3
- package/dist/loader/built-in/json/index.d.ts +0 -1
- package/dist/loader/built-in/json/module.d.ts +0 -5
- package/dist/loader/built-in/module/index.d.ts +0 -3
- package/dist/loader/built-in/module/module.d.ts +0 -15
- package/dist/loader/built-in/module/type.d.ts +0 -8
- package/dist/loader/built-in/module/utils.d.ts +0 -9
- package/dist/loader/built-in/yaml/index.d.ts +0 -1
- package/dist/loader/built-in/yaml/module.d.ts +0 -5
- package/dist/loader/constants.d.ts +0 -6
- package/dist/loader/helpers.d.ts +0 -6
- package/dist/loader/index.d.ts +0 -3
- package/dist/loader/module.d.ts +0 -18
- package/dist/loader/singleton.d.ts +0 -2
- package/dist/loader/type.d.ts +0 -8
- package/dist/locator/async.d.ts +0 -3
- package/dist/locator/index.d.ts +0 -4
- package/dist/locator/sync.d.ts +0 -3
- package/dist/locator/types.d.ts +0 -15
- package/dist/locator/utils.d.ts +0 -7
- package/dist/utils/error.d.ts +0 -1
- package/dist/utils/file-name.d.ts +0 -2
- package/dist/utils/file-path.d.ts +0 -1
- package/dist/utils/has-property.d.ts +0 -2
- package/dist/utils/index.d.ts +0 -8
- package/dist/utils/object.d.ts +0 -2
- package/dist/utils/runtime.d.ts +0 -2
- package/dist/utils/to-array.d.ts +0 -1
- package/dist/utils/typescript.d.ts +0 -1
package/dist/index.cjs
DELETED
|
@@ -1,743 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var destr = require('destr');
|
|
4
|
-
var flat = require('flat');
|
|
5
|
-
var fs = require('node:fs');
|
|
6
|
-
var fg = require('fast-glob');
|
|
7
|
-
var path = require('node:path');
|
|
8
|
-
var ebec = require('ebec');
|
|
9
|
-
var jiti = require('jiti');
|
|
10
|
-
var node_url = require('node:url');
|
|
11
|
-
var yaml = require('yaml');
|
|
12
|
-
|
|
13
|
-
function hasOwnProperty(obj, prop) {
|
|
14
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
15
|
-
}
|
|
16
|
-
function hasStringProperty(obj, prop) {
|
|
17
|
-
return hasOwnProperty(obj, prop) && typeof obj[prop] === 'string';
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/*
|
|
21
|
-
* Copyright (c) 2022.
|
|
22
|
-
* Author Peter Placzek (tada5hi)
|
|
23
|
-
* For the full copyright and license information,
|
|
24
|
-
* view the LICENSE file that was distributed with this source code.
|
|
25
|
-
*/ function isObject(item) {
|
|
26
|
-
return !!item && typeof item === 'object' && !Array.isArray(item);
|
|
27
|
-
}
|
|
28
|
-
function isSafeObjectKey(key) {
|
|
29
|
-
return key !== '__proto__' && key !== 'prototype' && key !== 'constructor';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function handleException(e) {
|
|
33
|
-
if (e instanceof Error) {
|
|
34
|
-
throw e;
|
|
35
|
-
}
|
|
36
|
-
const error = new Error('Can not process thrown exception.');
|
|
37
|
-
if (isObject(e)) {
|
|
38
|
-
if (hasOwnProperty(e, 'message') && typeof e.message === 'string') {
|
|
39
|
-
error.message = e.message;
|
|
40
|
-
}
|
|
41
|
-
if (hasOwnProperty(e, 'stack') && typeof e.stack === 'string') {
|
|
42
|
-
error.stack = e.stack;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
throw error;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function getFileNameExtension(input, allowed) {
|
|
49
|
-
const extension = path.extname(input);
|
|
50
|
-
if (extension === '' || extension === '.') {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
if (typeof allowed === 'undefined' || allowed.indexOf(extension) !== -1) {
|
|
54
|
-
return extension;
|
|
55
|
-
}
|
|
56
|
-
return undefined;
|
|
57
|
-
}
|
|
58
|
-
function removeFileNameExtension(input, extensions) {
|
|
59
|
-
const extension = getFileNameExtension(input, extensions);
|
|
60
|
-
if (extension) {
|
|
61
|
-
return input.substring(0, input.length - extension.length);
|
|
62
|
-
}
|
|
63
|
-
return input;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/*
|
|
67
|
-
* Copyright (c) 2022.
|
|
68
|
-
* Author Peter Placzek (tada5hi)
|
|
69
|
-
* For the full copyright and license information,
|
|
70
|
-
* view the LICENSE file that was distributed with this source code.
|
|
71
|
-
*/ function toArray(input) {
|
|
72
|
-
return Array.isArray(input) ? input : [
|
|
73
|
-
input
|
|
74
|
-
];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function isFilePath(input) {
|
|
78
|
-
const extension = path.extname(input);
|
|
79
|
-
return extension && extension !== '';
|
|
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
|
-
|
|
106
|
-
function buildLocatorPatterns(pattern) {
|
|
107
|
-
return Array.isArray(pattern) ? pattern : [
|
|
108
|
-
pattern
|
|
109
|
-
];
|
|
110
|
-
}
|
|
111
|
-
function buildLocatorOptions(options = {}) {
|
|
112
|
-
const paths = options.path ? toArray(options.path) : [];
|
|
113
|
-
const ignore = options.ignore ? toArray(options.ignore) : [];
|
|
114
|
-
if (paths.length === 0) {
|
|
115
|
-
paths.push(process.cwd());
|
|
116
|
-
}
|
|
117
|
-
let onlyFiles;
|
|
118
|
-
let onlyDirectories;
|
|
119
|
-
if (options.onlyDirectories === options.onlyFiles) {
|
|
120
|
-
onlyDirectories = false;
|
|
121
|
-
onlyFiles = !options.onlyDirectories;
|
|
122
|
-
} else if (typeof options.onlyFiles === 'undefined') {
|
|
123
|
-
onlyDirectories = options.onlyDirectories ?? false;
|
|
124
|
-
onlyFiles = !options.onlyDirectories;
|
|
125
|
-
} else {
|
|
126
|
-
onlyFiles = options.onlyFiles ?? true;
|
|
127
|
-
onlyDirectories = !options.onlyFiles;
|
|
128
|
-
}
|
|
129
|
-
return {
|
|
130
|
-
path: paths,
|
|
131
|
-
ignore,
|
|
132
|
-
onlyDirectories,
|
|
133
|
-
onlyFiles
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
function pathToLocatorInfo(input, skipResolve) {
|
|
137
|
-
if (!skipResolve && !path.isAbsolute(input)) {
|
|
138
|
-
input = path.resolve(process.cwd(), input);
|
|
139
|
-
}
|
|
140
|
-
const info = path.parse(input);
|
|
141
|
-
return {
|
|
142
|
-
path: info.dir.split('/').join(path.sep),
|
|
143
|
-
name: info.name,
|
|
144
|
-
extension: info.ext ? info.ext : undefined
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
function isLocatorInfo(input) {
|
|
148
|
-
return isObject(input) && typeof input.path === 'string' && typeof input.name === 'string' && typeof input.extension === 'string';
|
|
149
|
-
}
|
|
150
|
-
function buildFilePath(input) {
|
|
151
|
-
if (typeof input === 'string') {
|
|
152
|
-
return input;
|
|
153
|
-
}
|
|
154
|
-
if (input.extension) {
|
|
155
|
-
return path.join(input.path, input.name) + input.extension;
|
|
156
|
-
}
|
|
157
|
-
return path.join(input.path, input.name);
|
|
158
|
-
}
|
|
159
|
-
function buildFilePathWithoutExtension(input) {
|
|
160
|
-
let info;
|
|
161
|
-
if (typeof input === 'string') {
|
|
162
|
-
info = pathToLocatorInfo(input);
|
|
163
|
-
} else {
|
|
164
|
-
info = input;
|
|
165
|
-
}
|
|
166
|
-
return path.join(info.path, info.name);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async function locateMany(pattern, options) {
|
|
170
|
-
const patterns = buildLocatorPatterns(pattern);
|
|
171
|
-
const opts = buildLocatorOptions(options);
|
|
172
|
-
const items = [];
|
|
173
|
-
for(let i = 0; i < patterns.length; i++){
|
|
174
|
-
for(let j = 0; j < opts.path.length; j++){
|
|
175
|
-
const files = await fg(patterns[i], {
|
|
176
|
-
absolute: true,
|
|
177
|
-
cwd: opts.path[j],
|
|
178
|
-
ignore: opts.ignore,
|
|
179
|
-
onlyFiles: opts.onlyFiles,
|
|
180
|
-
onlyDirectories: opts.onlyDirectories
|
|
181
|
-
});
|
|
182
|
-
for(let k = 0; k < files.length; k++){
|
|
183
|
-
items.push(pathToLocatorInfo(files[k], true));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return items;
|
|
188
|
-
}
|
|
189
|
-
async function locate(pattern, options) {
|
|
190
|
-
const patterns = buildLocatorPatterns(pattern);
|
|
191
|
-
const opts = buildLocatorOptions(options);
|
|
192
|
-
for(let i = 0; i < patterns.length; i++){
|
|
193
|
-
for(let j = 0; j < opts.path.length; j++){
|
|
194
|
-
const files = await fg(patterns[i], {
|
|
195
|
-
absolute: true,
|
|
196
|
-
cwd: opts.path[j],
|
|
197
|
-
ignore: opts.ignore,
|
|
198
|
-
onlyFiles: opts.onlyFiles,
|
|
199
|
-
onlyDirectories: opts.onlyDirectories
|
|
200
|
-
});
|
|
201
|
-
const element = files.shift();
|
|
202
|
-
if (element) {
|
|
203
|
-
return pathToLocatorInfo(element, true);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return undefined;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function locateManySync(pattern, options) {
|
|
211
|
-
const patterns = buildLocatorPatterns(pattern);
|
|
212
|
-
const opts = buildLocatorOptions(options);
|
|
213
|
-
const items = [];
|
|
214
|
-
for(let i = 0; i < patterns.length; i++){
|
|
215
|
-
for(let j = 0; j < opts.path.length; j++){
|
|
216
|
-
const files = fg.sync(patterns[i], {
|
|
217
|
-
absolute: true,
|
|
218
|
-
cwd: opts.path[j],
|
|
219
|
-
ignore: opts.ignore,
|
|
220
|
-
onlyFiles: opts.onlyFiles,
|
|
221
|
-
onlyDirectories: opts.onlyDirectories
|
|
222
|
-
});
|
|
223
|
-
for(let k = 0; k < files.length; k++){
|
|
224
|
-
items.push(pathToLocatorInfo(files[k], true));
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return items;
|
|
229
|
-
}
|
|
230
|
-
function locateSync(pattern, options) {
|
|
231
|
-
const patterns = buildLocatorPatterns(pattern);
|
|
232
|
-
const opts = buildLocatorOptions(options);
|
|
233
|
-
for(let i = 0; i < patterns.length; i++){
|
|
234
|
-
for(let j = 0; j < opts.path.length; j++){
|
|
235
|
-
const files = fg.sync(patterns[i], {
|
|
236
|
-
absolute: true,
|
|
237
|
-
cwd: opts.path[j],
|
|
238
|
-
ignore: opts.ignore,
|
|
239
|
-
onlyFiles: opts.onlyFiles,
|
|
240
|
-
onlyDirectories: opts.onlyDirectories
|
|
241
|
-
});
|
|
242
|
-
const element = files.shift();
|
|
243
|
-
if (element) {
|
|
244
|
-
return pathToLocatorInfo(element, true);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return undefined;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/*
|
|
252
|
-
* Copyright (c) 2022-2023.
|
|
253
|
-
* Author Peter Placzek (tada5hi)
|
|
254
|
-
* For the full copyright and license information,
|
|
255
|
-
* view the LICENSE file that was distributed with this source code.
|
|
256
|
-
*/ // Based on https://github.com/unjs/rc9 (MIT)
|
|
257
|
-
class ConfLoader {
|
|
258
|
-
async execute(input) {
|
|
259
|
-
const filePath = buildFilePath(input);
|
|
260
|
-
try {
|
|
261
|
-
const file = await fs.promises.readFile(filePath, {
|
|
262
|
-
encoding: 'utf-8'
|
|
263
|
-
});
|
|
264
|
-
return this.parse(file);
|
|
265
|
-
} catch (e) {
|
|
266
|
-
return handleException(e);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
executeSync(input) {
|
|
270
|
-
const filePath = buildFilePath(input);
|
|
271
|
-
try {
|
|
272
|
-
const file = fs.readFileSync(filePath, {
|
|
273
|
-
encoding: 'utf-8'
|
|
274
|
-
});
|
|
275
|
-
return this.parse(file);
|
|
276
|
-
} catch (e) {
|
|
277
|
-
return handleException(e);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
parse(contents) {
|
|
281
|
-
const config = {};
|
|
282
|
-
const lines = contents.split(/\n|\r|\r\n/);
|
|
283
|
-
for(let i = 0; i < lines.length; i++){
|
|
284
|
-
const match = lines[i].match(/^\s*([^\s=]+)\s*=\s*(.*)?\s*$/);
|
|
285
|
-
if (!match) {
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
// Key
|
|
289
|
-
const key = match[1];
|
|
290
|
-
if (!key || !isSafeObjectKey(key)) {
|
|
291
|
-
continue;
|
|
292
|
-
}
|
|
293
|
-
const value = destr.destr(match[2].trim());
|
|
294
|
-
if (key.endsWith('[]')) {
|
|
295
|
-
const arrKey = key.slice(0, Math.max(0, key.length - 2));
|
|
296
|
-
config[arrKey] = (config[arrKey] || []).concat(value);
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
config[key] = value;
|
|
300
|
-
}
|
|
301
|
-
return flat.unflatten(config, {
|
|
302
|
-
overwrite: true
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function isESModule(input) {
|
|
308
|
-
return isObject(input) && // eslint-disable-next-line no-underscore-dangle
|
|
309
|
-
typeof input.__esModule !== 'undefined';
|
|
310
|
-
}
|
|
311
|
-
// https://2ality.com/2017/01/babel-esm-spec-mode.html
|
|
312
|
-
function toModuleRecord(data) {
|
|
313
|
-
if (isESModule(data)) {
|
|
314
|
-
// @see https://github.com/testing-library/user-event/issues/813
|
|
315
|
-
// @see https://stackoverflow.com/questions/62717394/export-default-class-exports-double-nested-default
|
|
316
|
-
if (isESModule(data.default) && data.default.default) {
|
|
317
|
-
return {
|
|
318
|
-
...data,
|
|
319
|
-
default: data.default.default
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
return data;
|
|
323
|
-
}
|
|
324
|
-
const output = Object.create(null, {
|
|
325
|
-
__esModule: {
|
|
326
|
-
value: true
|
|
327
|
-
},
|
|
328
|
-
[Symbol.toStringTag]: {
|
|
329
|
-
value: 'Module'
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
if (isObject(data)) {
|
|
333
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
334
|
-
for(const key in data){
|
|
335
|
-
if (hasOwnProperty(output, key)) {
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
let descriptor = Object.getOwnPropertyDescriptor(data, key);
|
|
339
|
-
if (!descriptor || 'get' in descriptor || descriptor.writable || descriptor.configurable) {
|
|
340
|
-
descriptor = {
|
|
341
|
-
enumerable: true,
|
|
342
|
-
get () {
|
|
343
|
-
return data[key];
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
Object.defineProperty(output, key, descriptor);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
if (!hasOwnProperty(output, 'default')) {
|
|
351
|
-
Object.defineProperty(output, 'default', {
|
|
352
|
-
value: data,
|
|
353
|
-
enumerable: true
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
return Object.freeze(output);
|
|
357
|
-
}
|
|
358
|
-
function getModuleExport(data, filterFn) {
|
|
359
|
-
const keys = Object.keys(data);
|
|
360
|
-
for(let i = 0; i < keys.length; i++){
|
|
361
|
-
if (filterFn(keys[i], data[keys[i]])) {
|
|
362
|
-
return {
|
|
363
|
-
key: keys[i],
|
|
364
|
-
value: data[keys[i]]
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
return undefined;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
class ModuleLoader {
|
|
372
|
-
async execute(input) {
|
|
373
|
-
let output;
|
|
374
|
-
try {
|
|
375
|
-
output = await this.load(input);
|
|
376
|
-
} catch (e) {
|
|
377
|
-
if (e instanceof SyntaxError || e instanceof ReferenceError || isTypeScriptError(e)) {
|
|
378
|
-
throw e;
|
|
379
|
-
}
|
|
380
|
-
// jiti + ts-node
|
|
381
|
-
// issue: https://github.com/nuxt/bridge/issues/228
|
|
382
|
-
if (isTsNodeRuntimeEnvironment()) {
|
|
383
|
-
output = this.loadSync(input);
|
|
384
|
-
} else {
|
|
385
|
-
output = this.instance(input);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return toModuleRecord(output);
|
|
389
|
-
}
|
|
390
|
-
executeSync(input) {
|
|
391
|
-
let output;
|
|
392
|
-
try {
|
|
393
|
-
output = this.loadSync(input);
|
|
394
|
-
} catch (e) {
|
|
395
|
-
if (e instanceof SyntaxError || e instanceof ReferenceError || isTypeScriptError(e)) {
|
|
396
|
-
throw e;
|
|
397
|
-
}
|
|
398
|
-
output = this.instance(input);
|
|
399
|
-
}
|
|
400
|
-
return toModuleRecord(output);
|
|
401
|
-
}
|
|
402
|
-
// ---------------------------------------------------------------------------
|
|
403
|
-
async load(data, options = {}) {
|
|
404
|
-
const id = this.build(data, options);
|
|
405
|
-
try {
|
|
406
|
-
// segmentation fault
|
|
407
|
-
// issue: https://github.com/nodejs/node/issues/35889
|
|
408
|
-
if (isJestRuntimeEnvironment()) {
|
|
409
|
-
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
410
|
-
return require(id);
|
|
411
|
-
}
|
|
412
|
-
return await import(id);
|
|
413
|
-
} catch (e) {
|
|
414
|
-
/* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
|
|
415
|
-
if (!options.withFilePrefix && (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
|
|
416
|
-
return this.load(data, {
|
|
417
|
-
...options,
|
|
418
|
-
withFilePrefix: true
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
throw new ebec.BaseError({
|
|
422
|
-
code: e.code,
|
|
423
|
-
message: hasStringProperty(e, 'message') ? e.message : undefined,
|
|
424
|
-
stack: hasStringProperty(e, 'stack') ? e.stack : undefined
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
/* istanbul ignore next */ return handleException(e);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
loadSync(data, options = {}) {
|
|
431
|
-
const id = this.build(data, options);
|
|
432
|
-
try {
|
|
433
|
-
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
434
|
-
return require(id);
|
|
435
|
-
} catch (e) {
|
|
436
|
-
/* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
|
|
437
|
-
throw new ebec.BaseError({
|
|
438
|
-
code: e.code,
|
|
439
|
-
message: hasStringProperty(e, 'message') ? e.message : undefined,
|
|
440
|
-
stack: hasStringProperty(e, 'stack') ? e.stack : undefined
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
return handleException(e);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
build(data, options = {}) {
|
|
447
|
-
if (isLocatorInfo(data) || isFilePath(data)) {
|
|
448
|
-
if (typeof data !== 'string') {
|
|
449
|
-
data = buildFilePath(data);
|
|
450
|
-
}
|
|
451
|
-
if (options.withFilePrefix) {
|
|
452
|
-
data = node_url.pathToFileURL(data).href;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
return data;
|
|
456
|
-
}
|
|
457
|
-
constructor(){
|
|
458
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
459
|
-
// @ts-ignore
|
|
460
|
-
this.instance = jiti.createJiti(undefined, {
|
|
461
|
-
extensions: [
|
|
462
|
-
'.js',
|
|
463
|
-
'.mjs',
|
|
464
|
-
'.mts',
|
|
465
|
-
'.cjs',
|
|
466
|
-
'.cts',
|
|
467
|
-
'.ts'
|
|
468
|
-
]
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
class JSONLoader {
|
|
474
|
-
async execute(input) {
|
|
475
|
-
const filePath = buildFilePath(input);
|
|
476
|
-
try {
|
|
477
|
-
const file = await fs.promises.readFile(filePath, {
|
|
478
|
-
encoding: 'utf-8'
|
|
479
|
-
});
|
|
480
|
-
return JSON.parse(file);
|
|
481
|
-
} catch (e) {
|
|
482
|
-
return handleException(e);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
executeSync(input) {
|
|
486
|
-
const filePath = buildFilePath(input);
|
|
487
|
-
try {
|
|
488
|
-
const file = fs.readFileSync(filePath, {
|
|
489
|
-
encoding: 'utf-8'
|
|
490
|
-
});
|
|
491
|
-
return JSON.parse(file);
|
|
492
|
-
} catch (e) {
|
|
493
|
-
return handleException(e);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
class YAMLLoader {
|
|
499
|
-
async execute(input) {
|
|
500
|
-
const filePath = buildFilePath(input);
|
|
501
|
-
try {
|
|
502
|
-
const file = await fs.promises.readFile(filePath, {
|
|
503
|
-
encoding: 'utf-8'
|
|
504
|
-
});
|
|
505
|
-
return yaml.parse(file);
|
|
506
|
-
} catch (e) {
|
|
507
|
-
return handleException(e);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
executeSync(input) {
|
|
511
|
-
const filePath = buildFilePath(input);
|
|
512
|
-
try {
|
|
513
|
-
const file = fs.readFileSync(filePath, {
|
|
514
|
-
encoding: 'utf-8'
|
|
515
|
-
});
|
|
516
|
-
return yaml.parse(file);
|
|
517
|
-
} catch (e) {
|
|
518
|
-
return handleException(e);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/*
|
|
524
|
-
* Copyright (c) 2023.
|
|
525
|
-
* Author Peter Placzek (tada5hi)
|
|
526
|
-
* For the full copyright and license information,
|
|
527
|
-
* view the LICENSE file that was distributed with this source code.
|
|
528
|
-
*/ var LoaderId = /*#__PURE__*/ function(LoaderId) {
|
|
529
|
-
LoaderId["MODULE"] = "module";
|
|
530
|
-
LoaderId["JSON"] = "json";
|
|
531
|
-
LoaderId["CONF"] = "conf";
|
|
532
|
-
LoaderId["YAML"] = "yaml";
|
|
533
|
-
return LoaderId;
|
|
534
|
-
}({});
|
|
535
|
-
|
|
536
|
-
class LoaderManager {
|
|
537
|
-
register(test, loader) {
|
|
538
|
-
if (typeof loader !== 'undefined') {
|
|
539
|
-
this.rules.push({
|
|
540
|
-
test,
|
|
541
|
-
loader
|
|
542
|
-
});
|
|
543
|
-
return;
|
|
544
|
-
}
|
|
545
|
-
this.rules.push(test);
|
|
546
|
-
}
|
|
547
|
-
async execute(input) {
|
|
548
|
-
const id = this.findLoader(input);
|
|
549
|
-
if (!id) {
|
|
550
|
-
const info = pathToLocatorInfo(input);
|
|
551
|
-
throw new Error(`No loader registered for extension: "${info.extension}"`);
|
|
552
|
-
}
|
|
553
|
-
const loader = this.resolve(id);
|
|
554
|
-
return loader.execute(input);
|
|
555
|
-
}
|
|
556
|
-
executeSync(input) {
|
|
557
|
-
const id = this.findLoader(input);
|
|
558
|
-
if (!id) {
|
|
559
|
-
const info = pathToLocatorInfo(input);
|
|
560
|
-
throw new Error(`No loader registered for extension: ${info.extension || 'unknown'}`);
|
|
561
|
-
}
|
|
562
|
-
const loader = this.resolve(id);
|
|
563
|
-
return loader.executeSync(input);
|
|
564
|
-
}
|
|
565
|
-
findLoader(input) {
|
|
566
|
-
if (!isFilePath(input)) {
|
|
567
|
-
return LoaderId.MODULE;
|
|
568
|
-
}
|
|
569
|
-
const info = pathToLocatorInfo(input);
|
|
570
|
-
for(let i = 0; i < this.rules.length; i++){
|
|
571
|
-
const { test } = this.rules[i];
|
|
572
|
-
if (Array.isArray(test)) {
|
|
573
|
-
if (info.extension && test.indexOf(info.extension) !== -1) {
|
|
574
|
-
return this.rules[i].loader;
|
|
575
|
-
}
|
|
576
|
-
} else if (test.test(buildFilePath(info))) {
|
|
577
|
-
return this.rules[i].loader;
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
return undefined;
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Resolve loader by id.
|
|
584
|
-
*
|
|
585
|
-
* @param id
|
|
586
|
-
*/ resolve(id) {
|
|
587
|
-
if (typeof id !== 'string') {
|
|
588
|
-
return id;
|
|
589
|
-
}
|
|
590
|
-
if (Object.prototype.hasOwnProperty.call(this.loaders, id)) {
|
|
591
|
-
return this.loaders[id];
|
|
592
|
-
}
|
|
593
|
-
let loader;
|
|
594
|
-
// built-in
|
|
595
|
-
switch(id){
|
|
596
|
-
case LoaderId.CONF:
|
|
597
|
-
{
|
|
598
|
-
loader = new ConfLoader();
|
|
599
|
-
break;
|
|
600
|
-
}
|
|
601
|
-
case LoaderId.MODULE:
|
|
602
|
-
{
|
|
603
|
-
loader = new ModuleLoader();
|
|
604
|
-
break;
|
|
605
|
-
}
|
|
606
|
-
case LoaderId.JSON:
|
|
607
|
-
{
|
|
608
|
-
loader = new JSONLoader();
|
|
609
|
-
break;
|
|
610
|
-
}
|
|
611
|
-
case LoaderId.YAML:
|
|
612
|
-
{
|
|
613
|
-
loader = new YAMLLoader();
|
|
614
|
-
break;
|
|
615
|
-
}
|
|
616
|
-
/* istanbul ignore next */ default:
|
|
617
|
-
{
|
|
618
|
-
const pluginPath = this.normalizePath(id);
|
|
619
|
-
const moduleLoader = this.resolve(LoaderId.MODULE);
|
|
620
|
-
loader = moduleLoader.executeSync(pluginPath);
|
|
621
|
-
break;
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
if (typeof loader !== 'undefined') {
|
|
625
|
-
this.loaders[id] = loader;
|
|
626
|
-
return loader;
|
|
627
|
-
}
|
|
628
|
-
throw new Error(`The loader ${id} could not be resolved.`);
|
|
629
|
-
}
|
|
630
|
-
/* istanbul ignore next */ normalizePath(input) {
|
|
631
|
-
if (path.isAbsolute(input) || input.startsWith('./')) {
|
|
632
|
-
return input;
|
|
633
|
-
}
|
|
634
|
-
if (input.startsWith('module:')) {
|
|
635
|
-
return input.substring(0, 'module:'.length);
|
|
636
|
-
}
|
|
637
|
-
if (!input.startsWith('@')) {
|
|
638
|
-
return `@locter/${input}`;
|
|
639
|
-
}
|
|
640
|
-
return input;
|
|
641
|
-
}
|
|
642
|
-
constructor(){
|
|
643
|
-
this.loaders = {};
|
|
644
|
-
this.rules = [
|
|
645
|
-
{
|
|
646
|
-
test: [
|
|
647
|
-
'.js',
|
|
648
|
-
'.mjs',
|
|
649
|
-
'.mts',
|
|
650
|
-
'.cjs',
|
|
651
|
-
'.cts',
|
|
652
|
-
'.ts'
|
|
653
|
-
],
|
|
654
|
-
loader: LoaderId.MODULE
|
|
655
|
-
},
|
|
656
|
-
{
|
|
657
|
-
test: [
|
|
658
|
-
'.conf'
|
|
659
|
-
],
|
|
660
|
-
loader: LoaderId.CONF
|
|
661
|
-
},
|
|
662
|
-
{
|
|
663
|
-
test: [
|
|
664
|
-
'.json'
|
|
665
|
-
],
|
|
666
|
-
loader: LoaderId.JSON
|
|
667
|
-
},
|
|
668
|
-
{
|
|
669
|
-
test: [
|
|
670
|
-
'.yml',
|
|
671
|
-
'.yaml'
|
|
672
|
-
],
|
|
673
|
-
loader: LoaderId.YAML
|
|
674
|
-
}
|
|
675
|
-
];
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
let instance;
|
|
680
|
-
function useLoader() {
|
|
681
|
-
if (typeof instance !== 'undefined') {
|
|
682
|
-
return instance;
|
|
683
|
-
}
|
|
684
|
-
instance = new LoaderManager();
|
|
685
|
-
return instance;
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
function registerLoader(test, loader) {
|
|
689
|
-
const manager = useLoader();
|
|
690
|
-
if (typeof loader !== 'undefined') {
|
|
691
|
-
manager.register(test, loader);
|
|
692
|
-
return;
|
|
693
|
-
}
|
|
694
|
-
manager.register(test);
|
|
695
|
-
}
|
|
696
|
-
async function load(input) {
|
|
697
|
-
const manager = useLoader();
|
|
698
|
-
if (typeof input === 'string') {
|
|
699
|
-
return manager.execute(input);
|
|
700
|
-
}
|
|
701
|
-
return manager.execute(buildFilePath(input));
|
|
702
|
-
}
|
|
703
|
-
function loadSync(input) {
|
|
704
|
-
const manager = useLoader();
|
|
705
|
-
if (typeof input === 'string') {
|
|
706
|
-
return manager.executeSync(input);
|
|
707
|
-
}
|
|
708
|
-
return manager.executeSync(buildFilePath(input));
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
exports.ConfLoader = ConfLoader;
|
|
712
|
-
exports.JSONLoader = JSONLoader;
|
|
713
|
-
exports.LoaderManager = LoaderManager;
|
|
714
|
-
exports.ModuleLoader = ModuleLoader;
|
|
715
|
-
exports.buildFilePath = buildFilePath;
|
|
716
|
-
exports.buildFilePathWithoutExtension = buildFilePathWithoutExtension;
|
|
717
|
-
exports.buildLocatorOptions = buildLocatorOptions;
|
|
718
|
-
exports.buildLocatorPatterns = buildLocatorPatterns;
|
|
719
|
-
exports.getFileNameExtension = getFileNameExtension;
|
|
720
|
-
exports.getModuleExport = getModuleExport;
|
|
721
|
-
exports.handleException = handleException;
|
|
722
|
-
exports.hasOwnProperty = hasOwnProperty;
|
|
723
|
-
exports.hasStringProperty = hasStringProperty;
|
|
724
|
-
exports.isESModule = isESModule;
|
|
725
|
-
exports.isFilePath = isFilePath;
|
|
726
|
-
exports.isJestRuntimeEnvironment = isJestRuntimeEnvironment;
|
|
727
|
-
exports.isLocatorInfo = isLocatorInfo;
|
|
728
|
-
exports.isObject = isObject;
|
|
729
|
-
exports.isSafeObjectKey = isSafeObjectKey;
|
|
730
|
-
exports.isTsNodeRuntimeEnvironment = isTsNodeRuntimeEnvironment;
|
|
731
|
-
exports.isTypeScriptError = isTypeScriptError;
|
|
732
|
-
exports.load = load;
|
|
733
|
-
exports.loadSync = loadSync;
|
|
734
|
-
exports.locate = locate;
|
|
735
|
-
exports.locateMany = locateMany;
|
|
736
|
-
exports.locateManySync = locateManySync;
|
|
737
|
-
exports.locateSync = locateSync;
|
|
738
|
-
exports.pathToLocatorInfo = pathToLocatorInfo;
|
|
739
|
-
exports.registerLoader = registerLoader;
|
|
740
|
-
exports.removeFileNameExtension = removeFileNameExtension;
|
|
741
|
-
exports.toArray = toArray;
|
|
742
|
-
exports.toModuleRecord = toModuleRecord;
|
|
743
|
-
//# sourceMappingURL=index.cjs.map
|