locter 0.7.1 → 0.8.1

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/dist/index.cjs CHANGED
@@ -1,2 +1,487 @@
1
- "use strict";var t=require("ebec"),r=require("node:url"),e=require("glob"),n=require("node:util"),o=require("node:path"),i=require("node:fs");function a(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable}))),e.push.apply(e,n)}return e}function c(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?a(Object(e),!0).forEach((function(r){l(t,r,e[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):a(Object(e)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))}))}return t}function u(){u=function(){return t};var t={},r=Object.prototype,e=r.hasOwnProperty,n=Object.defineProperty||function(t,r,e){t[r]=e.value},o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function s(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{s({},"")}catch(t){s=function(t,r,e){return t[r]=e}}function f(t,r,e,o){var i=r&&r.prototype instanceof h?r:h,a=Object.create(i.prototype),c=new j(o||[]);return n(a,"_invoke",{value:E(t,e,c)}),a}function p(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=f;var l={};function h(){}function y(){}function d(){}var v={};s(v,i,(function(){return this}));var g=Object.getPrototypeOf,b=g&&g(g(k([])));b&&b!==r&&e.call(b,i)&&(v=b);var w=d.prototype=h.prototype=Object.create(v);function x(t){["next","throw","return"].forEach((function(r){s(t,r,(function(t){return this._invoke(r,t)}))}))}function m(t,r){function o(n,i,a,c){var u=p(t[n],t,i);if("throw"!==u.type){var s=u.arg,f=s.value;return f&&"object"==typeof f&&e.call(f,"__await")?r.resolve(f.__await).then((function(t){o("next",t,a,c)}),(function(t){o("throw",t,a,c)})):r.resolve(f).then((function(t){s.value=t,a(s)}),(function(t){return o("throw",t,a,c)}))}c(u.arg)}var i;n(this,"_invoke",{value:function(t,e){function n(){return new r((function(r,n){o(t,e,r,n)}))}return i=i?i.then(n,n):n()}})}function E(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return _()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=O(a,e);if(c){if(c===l)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=p(t,r,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}function O(t,r){var e=r.method,n=t.iterator[e];if(void 0===n)return r.delegate=null,"throw"===e&&t.iterator.return&&(r.method="return",r.arg=void 0,O(t,r),"throw"===r.method)||"return"!==e&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+e+"' method")),l;var o=p(n,t.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,l;var i=o.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function L(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function S(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function j(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function k(t){if(t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return o.next=o}}return{next:_}}function _(){return{value:void 0,done:!0}}return y.prototype=d,n(w,"constructor",{value:d,configurable:!0}),n(d,"constructor",{value:y,configurable:!0}),y.displayName=s(d,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===y||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,d):(t.__proto__=d,s(t,c,"GeneratorFunction")),t.prototype=Object.create(w),t},t.awrap=function(t){return{__await:t}},x(m.prototype),s(m.prototype,a,(function(){return this})),t.AsyncIterator=m,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var a=new m(f(r,e,n,o),i);return t.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},x(w),s(w,c,"Generator"),s(w,i,(function(){return this})),s(w,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=k,j.prototype={constructor:j,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(S),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return a.type="throw",a.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=e.call(i,"catchLoc"),u=e.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),l},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),S(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;S(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:k(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}function s(t){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s(t)}function f(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function p(t){return function(){var r=this,e=arguments;return new Promise((function(n,o){var i=t.apply(r,e);function a(t){f(i,n,o,a,c,"next",t)}function c(t){f(i,n,o,a,c,"throw",t)}a(void 0)}))}}function l(t,r,e){return(r=function(t){var r=function(t,r){if("object"!=typeof t||null===t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,r||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"==typeof r?r:String(r)}(r))in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function h(t){if(t instanceof Error)throw t;if("string"==typeof t)throw new Error(t);throw new Error("Cannot determine file loading error.")}function y(t,r){return Object.prototype.hasOwnProperty.call(t,r)}function d(t,r){return y(t,r)&&"string"==typeof t[r]}function v(t){return!!t&&"object"===s(t)&&!Array.isArray(t)}function g(t){return Array.isArray(t)?t:[t]}function b(t){var r,e;return(t=t||{}).path=t.path||[],t.path=g(t.path),0===t.path.length&&t.path.push(process.cwd()),null!==(e=(r=t).ignore)&&void 0!==e||(r.ignore=[]),t}function w(t,r){r||o.isAbsolute(t)||(t=o.resolve(process.cwd(),t));var e=o.parse(t);return{path:e.dir.split("/").join(o.sep),name:e.name,extension:e.ext}}var x=n.promisify(e);function m(){return(m=p(u().mark((function t(r,e){var n,o,i,a,c,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:e=b(e),n=Array.isArray(r)?r:[r],o=[],i=0;case 4:if(!(i<n.length)){t.next=17;break}a=0;case 6:if(!(a<e.path.length)){t.next=14;break}return t.next=9,x(n[i],{absolute:!0,cwd:e.path[a],nodir:!0,ignore:e.ignore});case 9:for(c=t.sent,s=0;s<c.length;s++)o.push(w(c[s],!0));case 11:a++,t.next=6;break;case 14:i++,t.next=4;break;case 17:return t.abrupt("return",o);case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function E(){return(E=p(u().mark((function t(r,e){var n,o,i,a,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:e=b(e),n=Array.isArray(r)?r:[r],o=0;case 3:if(!(o<n.length)){t.next=18;break}i=0;case 5:if(!(i<e.path.length)){t.next=15;break}return t.next=8,x(n[o],{absolute:!0,cwd:e.path[i],nodir:!0,ignore:e.ignore});case 8:if(a=t.sent,!(c=a.shift())){t.next=12;break}return t.abrupt("return",w(c,!0));case 12:i++,t.next=5;break;case 15:o++,t.next=3;break;case 18:return t.abrupt("return",void 0);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function O(t,r){return r?o.join(t.path,t.name)+t.extension:o.join(t.path,t.name)}function L(r,e){if(e){for(var n=Object.keys(r),o=0;o<n.length;o++)if(e(n[o],r[n[o]]))return{key:n[o],value:r[n[o]]};throw new t.BaseError("Cannot find specific module export.")}return{key:"default",value:y(r,"default")?r.default:r}}function S(t,r){return j.apply(this,arguments)}function j(){return(j=p(u().mark((function e(n,o){var i,a;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return i="string"==typeof n?w(n):n,a=O(i,(o=o||{}).withExtension),o.withFilePrefix&&(a=r.pathToFileURL(a).href),e.prev=4,e.next=7,import(a);case 7:return e.abrupt("return",e.sent);case 10:if(e.prev=10,e.t0=e.catch(4),!v(e.t0)||!d(e.t0,"code")){e.next=18;break}if(o.withExtension||"ERR_MODULE_NOT_FOUND"!==e.t0.code&&"MODULE_NOT_FOUND"!==e.t0.code){e.next=15;break}return e.abrupt("return",S(i,c(c({},o),{},{withExtension:!0})));case 15:if(o.withFilePrefix||"ERR_UNSUPPORTED_ESM_URL_SCHEME"!==e.t0.code&&"UNSUPPORTED_ESM_URL_SCHEME"!==e.t0.code){e.next=17;break}return e.abrupt("return",S(i,c(c({},o),{},{withFilePrefix:!0})));case 17:throw new t.BaseError({code:e.t0.code,message:d(e.t0,"message")?e.t0.message:void 0,stack:d(e.t0,"stack")?e.t0.stack:void 0});case 18:return e.abrupt("return",h(e.t0));case 19:case"end":return e.stop()}}),e,null,[[4,10]])})))).apply(this,arguments)}function k(){return(k=p(u().mark((function r(e,n){var o;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,S(e);case 2:if("object"!==s(o=r.sent)||!o){r.next=5;break}return r.abrupt("return",L(o,n));case 5:throw new t.BaseError("Cannot extract specific module export");case 6:case"end":return r.stop()}}),r)})))).apply(this,arguments)}function _(r,e){var n,o=O(n="string"==typeof r?w(r):r,(e=e||{}).withExtension);try{return require(o)}catch(r){if(v(r)&&d(r,"code")){if(!e.withExtension&&("ERR_MODULE_NOT_FOUND"===r.code||"MODULE_NOT_FOUND"===r.code))return _(n,c(c({},e),{},{withExtension:!0}));throw new t.BaseError({code:r.code,message:d(r,"message")?r.message:void 0,stack:d(r,"stack")?r.stack:void 0})}return h(r)}}function P(t){return F.apply(this,arguments)}function F(){return(F=p(u().mark((function t(r){var e,n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e="string"==typeof r?o.isAbsolute(r)?r:o.resolve(process.cwd(),r):O(r,!0),t.prev=1,t.next=4,i.promises.readFile(e);case 4:return n=t.sent,t.abrupt("return",JSON.parse(n.toString("utf-8")));case 8:return t.prev=8,t.t0=t.catch(1),t.abrupt("return",h(t.t0));case 11:case"end":return t.stop()}}),t,null,[[1,8]])})))).apply(this,arguments)}function N(t){var r;r="string"==typeof t?o.isAbsolute(t)?t:o.resolve(process.cwd(),t):O(t,!0);try{var e=i.readFileSync(r);return JSON.parse(e.toString("utf-8"))}catch(t){return h(t)}}function A(){return(A=p(u().mark((function t(r){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(".json"!==(e="string"==typeof r?w(r):r).extension){t.next=3;break}return t.abrupt("return",P(e));case 3:return t.abrupt("return",S(e));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.buildLoaderFilePath=O,exports.buildLocatorOptions=b,exports.getExportItem=L,exports.handleFileLoadError=h,exports.hasOwnProperty=y,exports.hasStringProperty=d,exports.isObject=v,exports.loadFile=function(t){return A.apply(this,arguments)},exports.loadFileSync=function(t){var r;return".json"===(r="string"==typeof t?w(t):t).extension?N(r):_(r)},exports.loadJsonFile=P,exports.loadJsonFileSync=N,exports.loadScriptFile=S,exports.loadScriptFileExport=function(t,r){return k.apply(this,arguments)},exports.loadScriptFileExportSync=function(r,e){var n=_(r);if("object"===s(n)&&n)return L(n,e);throw new t.BaseError("Cannot extract specific module export")},exports.loadScriptFileSync=_,exports.locateFile=function(t,r){return E.apply(this,arguments)},exports.locateFileSync=function(t,r){r=b(r);for(var n=Array.isArray(t)?t:[t],o=0;o<n.length;o++)for(var i=0;i<r.path.length;i++){var a=e.sync(n[o],{absolute:!0,cwd:r.path[i],nodir:!0,ignore:r.ignore}).shift();if(a)return w(a,!0)}},exports.locateFiles=function(t,r){return m.apply(this,arguments)},exports.locateFilesSync=function(t,r){r=b(r);for(var n=Array.isArray(t)?t:[t],o=[],i=0;i<n.length;i++)for(var a=0;a<r.path.length;a++)for(var c=e.sync(n[i],{absolute:!0,cwd:r.path[a],nodir:!0,ignore:r.ignore}),u=0;u<c.length;u++)o.push(w(c[u],!0));return o},exports.pathToLocatorInfo=w,exports.removeFileNameExtension=function(t,r){if(t.includes(".")){var e=t.lastIndexOf("."),n=t.substring(e,t.length);void 0!==r&&-1===r.indexOf(n)||(t=t.substring(0,e))}return t},exports.toArray=g;
1
+ 'use strict';
2
+
3
+ var ebec = require('ebec');
4
+ var createJITI = require('jiti');
5
+ var node_url = require('node:url');
6
+ var glob = require('glob');
7
+ var node_util = require('node:util');
8
+ var path = require('node:path');
9
+ var fs = require('node:fs');
10
+
11
+ /*
12
+ * Copyright (c) 2022.
13
+ * Author Peter Placzek (tada5hi)
14
+ * For the full copyright and license information,
15
+ * view the LICENSE file that was distributed with this source code.
16
+ */ /* istanbul ignore next */ function handleFileLoadError(e) {
17
+ if (e instanceof Error) {
18
+ throw e;
19
+ }
20
+ if (typeof e === 'string') {
21
+ throw new Error(e);
22
+ }
23
+ throw new Error('Cannot determine file loading error.');
24
+ }
25
+
26
+ /*
27
+ * Copyright (c) 2022.
28
+ * Author Peter Placzek (tada5hi)
29
+ * For the full copyright and license information,
30
+ * view the LICENSE file that was distributed with this source code.
31
+ */ function removeFileNameExtension(input, extensions) {
32
+ if (input.includes('.')) {
33
+ const position = input.lastIndexOf('.');
34
+ const extension = input.substring(position, input.length);
35
+ if (typeof extensions === 'undefined' || extensions.indexOf(extension) !== -1) {
36
+ input = input.substring(0, position);
37
+ }
38
+ }
39
+ return input;
40
+ }
41
+
42
+ function hasOwnProperty(obj, prop) {
43
+ return Object.prototype.hasOwnProperty.call(obj, prop);
44
+ }
45
+ function hasStringProperty(obj, prop) {
46
+ return hasOwnProperty(obj, prop) && typeof obj[prop] === 'string';
47
+ }
48
+
49
+ /*
50
+ * Copyright (c) 2022.
51
+ * Author Peter Placzek (tada5hi)
52
+ * For the full copyright and license information,
53
+ * view the LICENSE file that was distributed with this source code.
54
+ */ function isObject(item) {
55
+ return !!item && typeof item === 'object' && !Array.isArray(item);
56
+ }
57
+
58
+ /*
59
+ * Copyright (c) 2022.
60
+ * Author Peter Placzek (tada5hi)
61
+ * For the full copyright and license information,
62
+ * view the LICENSE file that was distributed with this source code.
63
+ */ function toArray(input) {
64
+ return Array.isArray(input) ? input : [
65
+ input
66
+ ];
67
+ }
68
+
69
+ /*
70
+ * Copyright (c) 2022.
71
+ * Author Peter Placzek (tada5hi)
72
+ * For the full copyright and license information,
73
+ * view the LICENSE file that was distributed with this source code.
74
+ */ var _options, _options1;
75
+ function buildLocatorOptions(options) {
76
+ options = options || {};
77
+ options.path = options.path || [];
78
+ options.path = toArray(options.path);
79
+ if (options.path.length === 0) {
80
+ options.path.push(process.cwd());
81
+ }
82
+ (_options = options).soft ?? (_options.soft = true);
83
+ (_options1 = options).ignore ?? (_options1.ignore = []);
84
+ return options;
85
+ }
86
+ function pathToLocatorInfo(input, skipResolve) {
87
+ if (!skipResolve && !path.isAbsolute(input)) {
88
+ input = path.resolve(process.cwd(), input);
89
+ }
90
+ const info = path.parse(input);
91
+ return {
92
+ path: info.dir.split('/').join(path.sep),
93
+ name: info.name,
94
+ extension: info.ext
95
+ };
96
+ }
97
+
98
+ const globAsync = node_util.promisify(glob);
99
+ async function locateFiles(pattern, options) {
100
+ options = buildLocatorOptions(options);
101
+ const patterns = Array.isArray(pattern) ? pattern : [
102
+ pattern
103
+ ];
104
+ const items = [];
105
+ for(let i = 0; i < patterns.length; i++){
106
+ for(let j = 0; j < options.path.length; j++){
107
+ const files = await globAsync(patterns[i], {
108
+ absolute: true,
109
+ cwd: options.path[j],
110
+ nodir: true,
111
+ ignore: options.ignore
112
+ });
113
+ for(let k = 0; k < files.length; k++){
114
+ items.push(pathToLocatorInfo(files[k], true));
115
+ }
116
+ }
117
+ }
118
+ return items;
119
+ }
120
+ async function locateFile(pattern, options) {
121
+ options = buildLocatorOptions(options);
122
+ const patterns = Array.isArray(pattern) ? pattern : [
123
+ pattern
124
+ ];
125
+ for(let i = 0; i < patterns.length; i++){
126
+ for(let j = 0; j < options.path.length; j++){
127
+ const files = await globAsync(patterns[i], {
128
+ absolute: true,
129
+ cwd: options.path[j],
130
+ nodir: true,
131
+ ignore: options.ignore
132
+ });
133
+ const element = files.shift();
134
+ if (element) {
135
+ return pathToLocatorInfo(element, true);
136
+ }
137
+ }
138
+ }
139
+ if (!options.soft) {
140
+ throw new Error('The file could not be located.');
141
+ }
142
+ return undefined;
143
+ }
144
+
145
+ function locateFilesSync(pattern, options) {
146
+ options = buildLocatorOptions(options);
147
+ const patterns = Array.isArray(pattern) ? pattern : [
148
+ pattern
149
+ ];
150
+ const items = [];
151
+ for(let i = 0; i < patterns.length; i++){
152
+ for(let j = 0; j < options.path.length; j++){
153
+ const files = glob.sync(patterns[i], {
154
+ absolute: true,
155
+ cwd: options.path[j],
156
+ nodir: true,
157
+ ignore: options.ignore
158
+ });
159
+ for(let k = 0; k < files.length; k++){
160
+ items.push(pathToLocatorInfo(files[k], true));
161
+ }
162
+ }
163
+ }
164
+ return items;
165
+ }
166
+ function locateFileSync(pattern, options) {
167
+ options = buildLocatorOptions(options);
168
+ const patterns = Array.isArray(pattern) ? pattern : [
169
+ pattern
170
+ ];
171
+ for(let i = 0; i < patterns.length; i++){
172
+ for(let j = 0; j < options.path.length; j++){
173
+ const files = glob.sync(patterns[i], {
174
+ absolute: true,
175
+ cwd: options.path[j],
176
+ nodir: true,
177
+ ignore: options.ignore
178
+ });
179
+ const element = files.shift();
180
+ if (element) {
181
+ return pathToLocatorInfo(element, true);
182
+ }
183
+ }
184
+ }
185
+ return undefined;
186
+ }
187
+
188
+ function buildLoaderFilePath(info, withExtension) {
189
+ if (withExtension) {
190
+ return path.join(info.path, info.name) + info.extension;
191
+ }
192
+ return path.join(info.path, info.name);
193
+ }
194
+ function getExportItem(data, filterFn) {
195
+ if (filterFn) {
196
+ const keys = Object.keys(data);
197
+ for(let i = 0; i < keys.length; i++){
198
+ if (filterFn(keys[i], data[keys[i]])) {
199
+ return {
200
+ key: keys[i],
201
+ value: data[keys[i]]
202
+ };
203
+ }
204
+ }
205
+ throw new ebec.BaseError('Cannot find specific module export.');
206
+ }
207
+ return {
208
+ key: 'default',
209
+ value: hasOwnProperty(data, 'default') ? data.default : data
210
+ };
211
+ }
212
+
213
+ /*
214
+ * Copyright (c) 2022.
215
+ * Author Peter Placzek (tada5hi)
216
+ * For the full copyright and license information,
217
+ * view the LICENSE file that was distributed with this source code.
218
+ */ function isJestEnvironment() {
219
+ return process.env && process.env.JEST_WORKER_ID !== undefined;
220
+ }
221
+
222
+ class ScriptLoader {
223
+ async execute(info) {
224
+ const filePath = buildLoaderFilePath(info);
225
+ let output;
226
+ try {
227
+ output = await this.load(info);
228
+ } catch (e) {
229
+ output = this.jiti(filePath);
230
+ }
231
+ return output;
232
+ }
233
+ executeSync(info) {
234
+ const filePath = buildLoaderFilePath(info);
235
+ let output;
236
+ try {
237
+ output = this.loadSync(info);
238
+ } catch (e) {
239
+ output = this.jiti(filePath);
240
+ }
241
+ return output;
242
+ }
243
+ // ---------------------------------------------------------------------------
244
+ async load(data, options) {
245
+ let locatorInfo;
246
+ if (typeof data === 'string') {
247
+ locatorInfo = pathToLocatorInfo(data);
248
+ } else {
249
+ locatorInfo = data;
250
+ }
251
+ options = options || {};
252
+ let filePath = buildLoaderFilePath(locatorInfo, options.withExtension);
253
+ if (options.withFilePrefix) {
254
+ filePath = node_url.pathToFileURL(filePath).href;
255
+ }
256
+ try {
257
+ // segmentation fault
258
+ // issue: https://github.com/nodejs/node/issues/35889
259
+ if (isJestEnvironment()) {
260
+ // eslint-disable-next-line global-require,import/no-dynamic-require
261
+ return require(filePath);
262
+ }
263
+ return await import(filePath);
264
+ } catch (e) {
265
+ /* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
266
+ if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
267
+ return this.load(locatorInfo, {
268
+ ...options,
269
+ withExtension: true
270
+ });
271
+ }
272
+ if (!options.withFilePrefix && (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
273
+ return this.load(locatorInfo, {
274
+ ...options,
275
+ withFilePrefix: true
276
+ });
277
+ }
278
+ throw new ebec.BaseError({
279
+ code: e.code,
280
+ message: hasStringProperty(e, 'message') ? e.message : undefined,
281
+ stack: hasStringProperty(e, 'stack') ? e.stack : undefined
282
+ });
283
+ }
284
+ /* istanbul ignore next */ return handleFileLoadError(e);
285
+ }
286
+ }
287
+ loadSync(data, options) {
288
+ let locatorInfo;
289
+ if (typeof data === 'string') {
290
+ locatorInfo = pathToLocatorInfo(data);
291
+ } else {
292
+ locatorInfo = data;
293
+ }
294
+ options = options || {};
295
+ const filePath = buildLoaderFilePath(locatorInfo, options.withExtension);
296
+ try {
297
+ // eslint-disable-next-line global-require,import/no-dynamic-require
298
+ return require(filePath);
299
+ } catch (e) {
300
+ /* istanbul ignore next */ if (isObject(e) && hasStringProperty(e, 'code')) {
301
+ if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
302
+ return this.loadSync(locatorInfo, {
303
+ ...options,
304
+ withExtension: true
305
+ });
306
+ }
307
+ throw new ebec.BaseError({
308
+ code: e.code,
309
+ message: hasStringProperty(e, 'message') ? e.message : undefined,
310
+ stack: hasStringProperty(e, 'stack') ? e.stack : undefined
311
+ });
312
+ }
313
+ return handleFileLoadError(e);
314
+ }
315
+ }
316
+ constructor(){
317
+ /*
318
+ const loaderMap : Record<string, ESLoader> = {
319
+ '.mjs': 'js',
320
+ '.js': 'js',
321
+ '.jsx': 'jsx',
322
+ '.ts': 'ts',
323
+ '.tsx': 'tsx',
324
+ };
325
+ */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
326
+ // @ts-ignore
327
+ this.jiti = createJITI(undefined, {
328
+ esmResolve: true
329
+ });
330
+ }
331
+ }
332
+
333
+ class JSONLoader {
334
+ async execute(input) {
335
+ const filePath = buildLoaderFilePath(input, true);
336
+ try {
337
+ const file = await fs.promises.readFile(filePath);
338
+ return JSON.parse(file.toString('utf-8'));
339
+ } catch (e) {
340
+ return handleFileLoadError(e);
341
+ }
342
+ }
343
+ executeSync(input) {
344
+ const filePath = buildLoaderFilePath(input, true);
345
+ try {
346
+ const file = fs.readFileSync(filePath);
347
+ return JSON.parse(file.toString('utf-8'));
348
+ } catch (e) {
349
+ return handleFileLoadError(e);
350
+ }
351
+ }
352
+ }
353
+
354
+ class LoaderManager {
355
+ register(rule) {
356
+ this.rules.push(rule);
357
+ }
358
+ async execute(info) {
359
+ const rule = this.findRule(info);
360
+ if (!rule) {
361
+ throw new Error(`No loader registered for extension: "${info.extension}"`);
362
+ }
363
+ const loader = this.resolve(rule.loader);
364
+ return loader.execute(info);
365
+ }
366
+ executeSync(info) {
367
+ const rule = this.findRule(info);
368
+ if (!rule) {
369
+ throw new Error(`No loader registered for extension: ${info.extension || 'unknown'}`);
370
+ }
371
+ const loader = this.resolve(rule.loader);
372
+ return loader.executeSync(info);
373
+ }
374
+ findRule(info) {
375
+ for(let i = 0; i < this.rules.length; i++){
376
+ const { test } = this.rules[i];
377
+ if (Array.isArray(test)) {
378
+ if (test.indexOf(info.extension) !== -1) {
379
+ return this.rules[i];
380
+ }
381
+ } else if (test.test(buildLoaderFilePath(info))) {
382
+ return this.rules[i];
383
+ }
384
+ }
385
+ return undefined;
386
+ }
387
+ /**
388
+ * Resolve loader by id.
389
+ *
390
+ * @param id
391
+ */ resolve(id) {
392
+ if (typeof id !== 'string') {
393
+ return id;
394
+ }
395
+ if (this.loaders[id]) {
396
+ return this.loaders[id];
397
+ }
398
+ let loader;
399
+ switch(id){
400
+ case 'script':
401
+ {
402
+ loader = new ScriptLoader();
403
+ break;
404
+ }
405
+ case 'json':
406
+ {
407
+ loader = new JSONLoader();
408
+ break;
409
+ }
410
+ }
411
+ if (typeof loader !== 'undefined') {
412
+ return loader;
413
+ }
414
+ throw new Error(`The loader ${id} could not be resolved.`);
415
+ }
416
+ constructor(){
417
+ this.loaders = {};
418
+ this.rules = [
419
+ {
420
+ test: [
421
+ '.js',
422
+ '.mjs',
423
+ '.cjs',
424
+ '.ts'
425
+ ],
426
+ loader: 'script'
427
+ },
428
+ {
429
+ test: [
430
+ '.json'
431
+ ],
432
+ loader: 'json'
433
+ }
434
+ ];
435
+ }
436
+ }
437
+
438
+ let instance;
439
+ function useLoaderManager() {
440
+ if (typeof instance !== 'undefined') {
441
+ return instance;
442
+ }
443
+ instance = new LoaderManager();
444
+ return instance;
445
+ }
446
+
447
+ async function loadFile(input) {
448
+ let info;
449
+ if (typeof input === 'string') {
450
+ info = pathToLocatorInfo(input);
451
+ } else {
452
+ info = input;
453
+ }
454
+ const manager = useLoaderManager();
455
+ return manager.execute(info);
456
+ }
457
+ function loadFileSync(input) {
458
+ let info;
459
+ if (typeof input === 'string') {
460
+ info = pathToLocatorInfo(input);
461
+ } else {
462
+ info = input;
463
+ }
464
+ const manager = useLoaderManager();
465
+ return manager.executeSync(info);
466
+ }
467
+
468
+ exports.JSONLoader = JSONLoader;
469
+ exports.ScriptLoader = ScriptLoader;
470
+ exports.buildLoaderFilePath = buildLoaderFilePath;
471
+ exports.buildLocatorOptions = buildLocatorOptions;
472
+ exports.getExportItem = getExportItem;
473
+ exports.handleFileLoadError = handleFileLoadError;
474
+ exports.hasOwnProperty = hasOwnProperty;
475
+ exports.hasStringProperty = hasStringProperty;
476
+ exports.isJestEnvironment = isJestEnvironment;
477
+ exports.isObject = isObject;
478
+ exports.loadFile = loadFile;
479
+ exports.loadFileSync = loadFileSync;
480
+ exports.locateFile = locateFile;
481
+ exports.locateFileSync = locateFileSync;
482
+ exports.locateFiles = locateFiles;
483
+ exports.locateFilesSync = locateFilesSync;
484
+ exports.pathToLocatorInfo = pathToLocatorInfo;
485
+ exports.removeFileNameExtension = removeFileNameExtension;
486
+ exports.toArray = toArray;
2
487
  //# sourceMappingURL=index.cjs.map