locter 0.4.0 → 0.5.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.js CHANGED
@@ -1,10 +1,32 @@
1
1
  'use strict';
2
2
 
3
+ var url = require('url');
3
4
  var glob = require('glob');
4
- var path = require('path');
5
5
  var util = require('util');
6
+ var path = require('path');
6
7
  var fs = require('fs');
7
8
 
9
+ function ownKeys(object, enumerableOnly) {
10
+ var keys = Object.keys(object);
11
+ if (Object.getOwnPropertySymbols) {
12
+ var symbols = Object.getOwnPropertySymbols(object);
13
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
14
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
15
+ })), keys.push.apply(keys, symbols);
16
+ }
17
+ return keys;
18
+ }
19
+ function _objectSpread2(target) {
20
+ for (var i = 1; i < arguments.length; i++) {
21
+ var source = null != arguments[i] ? arguments[i] : {};
22
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
23
+ _defineProperty(target, key, source[key]);
24
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
25
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
26
+ });
27
+ }
28
+ return target;
29
+ }
8
30
  function _regeneratorRuntime() {
9
31
  _regeneratorRuntime = function () {
10
32
  return exports;
@@ -345,6 +367,34 @@ function _asyncToGenerator(fn) {
345
367
  });
346
368
  };
347
369
  }
370
+ function _defineProperty(obj, key, value) {
371
+ key = _toPropertyKey(key);
372
+ if (key in obj) {
373
+ Object.defineProperty(obj, key, {
374
+ value: value,
375
+ enumerable: true,
376
+ configurable: true,
377
+ writable: true
378
+ });
379
+ } else {
380
+ obj[key] = value;
381
+ }
382
+ return obj;
383
+ }
384
+ function _toPrimitive(input, hint) {
385
+ if (typeof input !== "object" || input === null) return input;
386
+ var prim = input[Symbol.toPrimitive];
387
+ if (prim !== undefined) {
388
+ var res = prim.call(input, hint || "default");
389
+ if (typeof res !== "object") return res;
390
+ throw new TypeError("@@toPrimitive must return a primitive value.");
391
+ }
392
+ return (hint === "string" ? String : Number)(input);
393
+ }
394
+ function _toPropertyKey(arg) {
395
+ var key = _toPrimitive(arg, "string");
396
+ return typeof key === "symbol" ? key : String(key);
397
+ }
348
398
 
349
399
  /*
350
400
  * Copyright (c) 2022.
@@ -397,34 +447,6 @@ function toArray(input) {
397
447
  return Array.isArray(input) ? input : [input];
398
448
  }
399
449
 
400
- /*
401
- * Copyright (c) 2022.
402
- * Author Peter Placzek (tada5hi)
403
- * For the full copyright and license information,
404
- * view the LICENSE file that was distributed with this source code.
405
- */
406
- function getExportItem(data, filterFn) {
407
- if (filterFn) {
408
- var keys = Object.keys(data);
409
- for (var i = 0; i < keys.length; i++) {
410
- if (filterFn(keys[i], data[keys[i]])) {
411
- return {
412
- key: keys[i],
413
- value: data[keys[i]]
414
- };
415
- }
416
- }
417
- } else {
418
- return {
419
- key: 'default',
420
- value: hasOwnProperty(data, 'default') ? data["default"] : data
421
- };
422
- }
423
-
424
- /* istanbul ignore next */
425
- return undefined;
426
- }
427
-
428
450
  function buildLocatorOptions(options) {
429
451
  var _options, _options$ignore;
430
452
  options = options || {};
@@ -450,6 +472,17 @@ function isLocatorInfo(data) {
450
472
  }
451
473
  return !(!hasOwnProperty(data, 'extension') || typeof data.extension !== 'string');
452
474
  }
475
+ function pathToLocatorInfo(input, skipResolve) {
476
+ if (!skipResolve && !path.isAbsolute(input)) {
477
+ input = path.resolve(process.cwd(), input);
478
+ }
479
+ var info = path.parse(input);
480
+ return {
481
+ path: info.dir.split('/').join(path.sep),
482
+ name: info.name,
483
+ extension: info.ext
484
+ };
485
+ }
453
486
 
454
487
  var globAsync = util.promisify(glob.glob);
455
488
  function locateFiles(_x, _x2) {
@@ -457,7 +490,7 @@ function locateFiles(_x, _x2) {
457
490
  }
458
491
  function _locateFiles() {
459
492
  _locateFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern, options) {
460
- var patterns, items, i, j, files, k, fileInfo;
493
+ var patterns, items, i, j, files, k;
461
494
  return _regeneratorRuntime().wrap(function _callee$(_context) {
462
495
  while (1) {
463
496
  switch (_context.prev = _context.next) {
@@ -487,12 +520,7 @@ function _locateFiles() {
487
520
  case 9:
488
521
  files = _context.sent;
489
522
  for (k = 0; k < files.length; k++) {
490
- fileInfo = path.parse(files[k]);
491
- items.push({
492
- path: fileInfo.dir.split('/').join(path.sep),
493
- name: fileInfo.name,
494
- extension: fileInfo.ext
495
- });
523
+ items.push(pathToLocatorInfo(files[k], true));
496
524
  }
497
525
  case 11:
498
526
  j++;
@@ -518,7 +546,7 @@ function locateFile(_x3, _x4) {
518
546
  }
519
547
  function _locateFile() {
520
548
  _locateFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(pattern, options) {
521
- var patterns, i, j, files, element, fileInfo;
549
+ var patterns, i, j, files, element;
522
550
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
523
551
  while (1) {
524
552
  switch (_context2.prev = _context2.next) {
@@ -528,13 +556,13 @@ function _locateFile() {
528
556
  i = 0;
529
557
  case 3:
530
558
  if (!(i < patterns.length)) {
531
- _context2.next = 19;
559
+ _context2.next = 18;
532
560
  break;
533
561
  }
534
562
  j = 0;
535
563
  case 5:
536
564
  if (!(j < options.path.length)) {
537
- _context2.next = 16;
565
+ _context2.next = 15;
538
566
  break;
539
567
  }
540
568
  _context2.next = 8;
@@ -548,26 +576,21 @@ function _locateFile() {
548
576
  files = _context2.sent;
549
577
  element = files.shift();
550
578
  if (!element) {
551
- _context2.next = 13;
579
+ _context2.next = 12;
552
580
  break;
553
581
  }
554
- fileInfo = path.parse(element);
555
- return _context2.abrupt("return", {
556
- path: fileInfo.dir.split('/').join(path.sep),
557
- name: fileInfo.name,
558
- extension: fileInfo.ext
559
- });
560
- case 13:
582
+ return _context2.abrupt("return", pathToLocatorInfo(element, true));
583
+ case 12:
561
584
  j++;
562
585
  _context2.next = 5;
563
586
  break;
564
- case 16:
587
+ case 15:
565
588
  i++;
566
589
  _context2.next = 3;
567
590
  break;
568
- case 19:
591
+ case 18:
569
592
  return _context2.abrupt("return", undefined);
570
- case 20:
593
+ case 19:
571
594
  case "end":
572
595
  return _context2.stop();
573
596
  }
@@ -596,12 +619,7 @@ function locateFilesSync(pattern, options) {
596
619
  ignore: options.ignore
597
620
  });
598
621
  for (var k = 0; k < files.length; k++) {
599
- var fileInfo = path.parse(files[k]);
600
- items.push({
601
- path: fileInfo.dir.split('/').join(path.sep),
602
- name: fileInfo.name,
603
- extension: fileInfo.ext
604
- });
622
+ items.push(pathToLocatorInfo(files[k], true));
605
623
  }
606
624
  }
607
625
  }
@@ -620,12 +638,7 @@ function locateFileSync(pattern, options) {
620
638
  });
621
639
  var element = files.shift();
622
640
  if (element) {
623
- var fileInfo = path.parse(element);
624
- return {
625
- path: fileInfo.dir.split('/').join(path.sep),
626
- name: fileInfo.name,
627
- extension: fileInfo.ext
628
- };
641
+ return pathToLocatorInfo(element, true);
629
642
  }
630
643
  }
631
644
  }
@@ -638,72 +651,131 @@ function locateFileSync(pattern, options) {
638
651
  * For the full copyright and license information,
639
652
  * view the LICENSE file that was distributed with this source code.
640
653
  */
641
- function buildLoaderFilePath(info) {
642
- return path.join(info.path, info.name) + (info.extension === '.json' ? '.json' : '');
654
+ function buildLoaderFilePath(info, withExtension) {
655
+ if (withExtension) {
656
+ return path.join(info.path, info.name) + info.extension;
657
+ }
658
+ return path.join(info.path, info.name);
643
659
  }
644
660
 
645
- function loadScriptFile(_x) {
661
+ /*
662
+ * Copyright (c) 2022.
663
+ * Author Peter Placzek (tada5hi)
664
+ * For the full copyright and license information,
665
+ * view the LICENSE file that was distributed with this source code.
666
+ */
667
+ function getExportItem(data, filterFn) {
668
+ if (filterFn) {
669
+ var keys = Object.keys(data);
670
+ for (var i = 0; i < keys.length; i++) {
671
+ if (filterFn(keys[i], data[keys[i]])) {
672
+ return {
673
+ key: keys[i],
674
+ value: data[keys[i]]
675
+ };
676
+ }
677
+ }
678
+ } else {
679
+ return {
680
+ key: 'default',
681
+ value: hasOwnProperty(data, 'default') ? data["default"] : data
682
+ };
683
+ }
684
+
685
+ /* istanbul ignore next */
686
+ return undefined;
687
+ }
688
+
689
+ function loadScriptFile(_x, _x2) {
646
690
  return _loadScriptFile.apply(this, arguments);
647
691
  }
648
692
  function _loadScriptFile() {
649
- _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data) {
650
- var filePath;
693
+ _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data, options) {
694
+ var locatorInfo, filePath;
651
695
  return _regeneratorRuntime().wrap(function _callee$(_context) {
652
696
  while (1) {
653
697
  switch (_context.prev = _context.next) {
654
698
  case 0:
655
- filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
656
- _context.prev = 1;
657
- _context.next = 4;
699
+ if (typeof data === 'string') {
700
+ locatorInfo = pathToLocatorInfo(data);
701
+ } else {
702
+ locatorInfo = data;
703
+ }
704
+ options = options || {};
705
+ filePath = buildLoaderFilePath(locatorInfo, options.withExtension);
706
+ if (options.withFilePrefix) {
707
+ filePath = url.pathToFileURL(filePath).href;
708
+ }
709
+ _context.prev = 4;
710
+ _context.next = 7;
658
711
  return import(filePath);
659
- case 4:
660
- return _context.abrupt("return", _context.sent);
661
712
  case 7:
662
- _context.prev = 7;
663
- _context.t0 = _context["catch"](1);
664
- return _context.abrupt("return", handleFileLoadError(_context.t0));
713
+ return _context.abrupt("return", _context.sent);
665
714
  case 10:
715
+ _context.prev = 10;
716
+ _context.t0 = _context["catch"](4);
717
+ if (!(_context.t0 instanceof Error && hasOwnProperty(_context.t0, 'code'))) {
718
+ _context.next = 17;
719
+ break;
720
+ }
721
+ if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND')) {
722
+ _context.next = 15;
723
+ break;
724
+ }
725
+ return _context.abrupt("return", loadScriptFile(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
726
+ withExtension: true
727
+ })));
728
+ case 15:
729
+ if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME')) {
730
+ _context.next = 17;
731
+ break;
732
+ }
733
+ return _context.abrupt("return", loadScriptFile(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
734
+ withFilePrefix: true
735
+ })));
736
+ case 17:
737
+ return _context.abrupt("return", handleFileLoadError(_context.t0));
738
+ case 18:
666
739
  case "end":
667
740
  return _context.stop();
668
741
  }
669
742
  }
670
- }, _callee, null, [[1, 7]]);
743
+ }, _callee, null, [[4, 10]]);
671
744
  }));
672
745
  return _loadScriptFile.apply(this, arguments);
673
746
  }
674
- function loadScriptFileExport(_x2, _x3) {
747
+ function loadScriptFileExport(_x3, _x4) {
675
748
  return _loadScriptFileExport.apply(this, arguments);
676
749
  }
677
750
  function _loadScriptFileExport() {
678
751
  _loadScriptFileExport = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(data, filterFn) {
679
- var filePath, _data;
752
+ var output;
680
753
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
681
754
  while (1) {
682
755
  switch (_context2.prev = _context2.next) {
683
756
  case 0:
684
- filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
685
- _context2.prev = 1;
686
- _context2.next = 4;
687
- return loadScriptFile(filePath);
688
- case 4:
689
- _data = _context2.sent;
690
- if (!(_typeof(_data) === 'object')) {
691
- _context2.next = 7;
757
+ _context2.prev = 0;
758
+ _context2.next = 3;
759
+ return loadScriptFile(data);
760
+ case 3:
761
+ output = _context2.sent;
762
+ if (!(_typeof(output) === 'object')) {
763
+ _context2.next = 6;
692
764
  break;
693
765
  }
694
- return _context2.abrupt("return", getExportItem(_data, filterFn));
695
- case 7:
766
+ return _context2.abrupt("return", getExportItem(output, filterFn));
767
+ case 6:
696
768
  return _context2.abrupt("return", undefined);
697
- case 10:
698
- _context2.prev = 10;
699
- _context2.t0 = _context2["catch"](1);
769
+ case 9:
770
+ _context2.prev = 9;
771
+ _context2.t0 = _context2["catch"](0);
700
772
  return _context2.abrupt("return", handleFileLoadError(_context2.t0));
701
- case 13:
773
+ case 12:
702
774
  case "end":
703
775
  return _context2.stop();
704
776
  }
705
777
  }
706
- }, _callee2, null, [[1, 10]]);
778
+ }, _callee2, null, [[0, 9]]);
707
779
  }));
708
780
  return _loadScriptFileExport.apply(this, arguments);
709
781
  }
@@ -734,32 +806,34 @@ function loadJsonFile(_x) {
734
806
  return _loadJsonFile.apply(this, arguments);
735
807
  }
736
808
  function _loadJsonFile() {
737
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(filePath) {
738
- var file;
809
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
810
+ var filePath, file;
739
811
  return _regeneratorRuntime().wrap(function _callee$(_context) {
740
812
  while (1) {
741
813
  switch (_context.prev = _context.next) {
742
814
  case 0:
743
- _context.prev = 0;
744
- _context.next = 3;
815
+ filePath = buildLoaderFilePath(info, true);
816
+ _context.prev = 1;
817
+ _context.next = 4;
745
818
  return fs.promises.readFile(filePath);
746
- case 3:
819
+ case 4:
747
820
  file = _context.sent;
748
821
  return _context.abrupt("return", JSON.parse(file.toString('utf-8')));
749
- case 7:
750
- _context.prev = 7;
751
- _context.t0 = _context["catch"](0);
822
+ case 8:
823
+ _context.prev = 8;
824
+ _context.t0 = _context["catch"](1);
752
825
  return _context.abrupt("return", handleFileLoadError(_context.t0));
753
- case 10:
826
+ case 11:
754
827
  case "end":
755
828
  return _context.stop();
756
829
  }
757
830
  }
758
- }, _callee, null, [[0, 7]]);
831
+ }, _callee, null, [[1, 8]]);
759
832
  }));
760
833
  return _loadJsonFile.apply(this, arguments);
761
834
  }
762
- function loadJsonFileSync(filePath) {
835
+ function loadJsonFileSync(info) {
836
+ var filePath = buildLoaderFilePath(info, true);
763
837
  try {
764
838
  var file = fs.readFileSync(filePath);
765
839
  return JSON.parse(file.toString('utf-8'));
@@ -772,26 +846,30 @@ function loadFile(_x) {
772
846
  return _loadFile.apply(this, arguments);
773
847
  }
774
848
  function _loadFile() {
775
- _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
776
- var filePath;
849
+ _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
850
+ var info;
777
851
  return _regeneratorRuntime().wrap(function _callee$(_context) {
778
852
  while (1) {
779
853
  switch (_context.prev = _context.next) {
780
854
  case 0:
781
- if (info) {
855
+ if (input) {
782
856
  _context.next = 2;
783
857
  break;
784
858
  }
785
859
  return _context.abrupt("return", undefined);
786
860
  case 2:
787
- filePath = buildLoaderFilePath(info);
861
+ if (typeof input === 'string') {
862
+ info = pathToLocatorInfo(input);
863
+ } else {
864
+ info = input;
865
+ }
788
866
  if (!(info.extension === '.json')) {
789
867
  _context.next = 5;
790
868
  break;
791
869
  }
792
- return _context.abrupt("return", loadJsonFile(filePath));
870
+ return _context.abrupt("return", loadJsonFile(info));
793
871
  case 5:
794
- return _context.abrupt("return", loadScriptFile(filePath));
872
+ return _context.abrupt("return", loadScriptFile(info));
795
873
  case 6:
796
874
  case "end":
797
875
  return _context.stop();
@@ -801,15 +879,20 @@ function _loadFile() {
801
879
  }));
802
880
  return _loadFile.apply(this, arguments);
803
881
  }
804
- function loadFileSync(info) {
805
- if (!info) {
882
+ function loadFileSync(input) {
883
+ if (!input) {
806
884
  return undefined;
807
885
  }
808
- var filePath = buildLoaderFilePath(info);
886
+ var info;
887
+ if (typeof input === 'string') {
888
+ info = pathToLocatorInfo(input);
889
+ } else {
890
+ info = input;
891
+ }
809
892
  if (info.extension === '.json') {
810
- return loadJsonFileSync(filePath);
893
+ return loadJsonFileSync(info);
811
894
  }
812
- return loadScriptFileSync(filePath);
895
+ return loadScriptFileSync(info);
813
896
  }
814
897
 
815
898
  exports.buildLoaderFilePath = buildLoaderFilePath;
@@ -830,5 +913,6 @@ exports.locateFile = locateFile;
830
913
  exports.locateFileSync = locateFileSync;
831
914
  exports.locateFiles = locateFiles;
832
915
  exports.locateFilesSync = locateFilesSync;
916
+ exports.pathToLocatorInfo = pathToLocatorInfo;
833
917
  exports.removeFileNameExtension = removeFileNameExtension;
834
918
  exports.toArray = toArray;
package/dist/index.esm.js CHANGED
@@ -1,8 +1,30 @@
1
+ import { pathToFileURL } from 'url';
1
2
  import { glob, sync } from 'glob';
2
- import path from 'path';
3
3
  import { promisify } from 'util';
4
+ import path from 'path';
4
5
  import fs from 'fs';
5
6
 
7
+ function ownKeys(object, enumerableOnly) {
8
+ var keys = Object.keys(object);
9
+ if (Object.getOwnPropertySymbols) {
10
+ var symbols = Object.getOwnPropertySymbols(object);
11
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
12
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
13
+ })), keys.push.apply(keys, symbols);
14
+ }
15
+ return keys;
16
+ }
17
+ function _objectSpread2(target) {
18
+ for (var i = 1; i < arguments.length; i++) {
19
+ var source = null != arguments[i] ? arguments[i] : {};
20
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
21
+ _defineProperty(target, key, source[key]);
22
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
23
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
24
+ });
25
+ }
26
+ return target;
27
+ }
6
28
  function _regeneratorRuntime() {
7
29
  _regeneratorRuntime = function () {
8
30
  return exports;
@@ -343,6 +365,34 @@ function _asyncToGenerator(fn) {
343
365
  });
344
366
  };
345
367
  }
368
+ function _defineProperty(obj, key, value) {
369
+ key = _toPropertyKey(key);
370
+ if (key in obj) {
371
+ Object.defineProperty(obj, key, {
372
+ value: value,
373
+ enumerable: true,
374
+ configurable: true,
375
+ writable: true
376
+ });
377
+ } else {
378
+ obj[key] = value;
379
+ }
380
+ return obj;
381
+ }
382
+ function _toPrimitive(input, hint) {
383
+ if (typeof input !== "object" || input === null) return input;
384
+ var prim = input[Symbol.toPrimitive];
385
+ if (prim !== undefined) {
386
+ var res = prim.call(input, hint || "default");
387
+ if (typeof res !== "object") return res;
388
+ throw new TypeError("@@toPrimitive must return a primitive value.");
389
+ }
390
+ return (hint === "string" ? String : Number)(input);
391
+ }
392
+ function _toPropertyKey(arg) {
393
+ var key = _toPrimitive(arg, "string");
394
+ return typeof key === "symbol" ? key : String(key);
395
+ }
346
396
 
347
397
  /*
348
398
  * Copyright (c) 2022.
@@ -395,34 +445,6 @@ function toArray(input) {
395
445
  return Array.isArray(input) ? input : [input];
396
446
  }
397
447
 
398
- /*
399
- * Copyright (c) 2022.
400
- * Author Peter Placzek (tada5hi)
401
- * For the full copyright and license information,
402
- * view the LICENSE file that was distributed with this source code.
403
- */
404
- function getExportItem(data, filterFn) {
405
- if (filterFn) {
406
- var keys = Object.keys(data);
407
- for (var i = 0; i < keys.length; i++) {
408
- if (filterFn(keys[i], data[keys[i]])) {
409
- return {
410
- key: keys[i],
411
- value: data[keys[i]]
412
- };
413
- }
414
- }
415
- } else {
416
- return {
417
- key: 'default',
418
- value: hasOwnProperty(data, 'default') ? data["default"] : data
419
- };
420
- }
421
-
422
- /* istanbul ignore next */
423
- return undefined;
424
- }
425
-
426
448
  function buildLocatorOptions(options) {
427
449
  var _options, _options$ignore;
428
450
  options = options || {};
@@ -448,6 +470,17 @@ function isLocatorInfo(data) {
448
470
  }
449
471
  return !(!hasOwnProperty(data, 'extension') || typeof data.extension !== 'string');
450
472
  }
473
+ function pathToLocatorInfo(input, skipResolve) {
474
+ if (!skipResolve && !path.isAbsolute(input)) {
475
+ input = path.resolve(process.cwd(), input);
476
+ }
477
+ var info = path.parse(input);
478
+ return {
479
+ path: info.dir.split('/').join(path.sep),
480
+ name: info.name,
481
+ extension: info.ext
482
+ };
483
+ }
451
484
 
452
485
  var globAsync = promisify(glob);
453
486
  function locateFiles(_x, _x2) {
@@ -455,7 +488,7 @@ function locateFiles(_x, _x2) {
455
488
  }
456
489
  function _locateFiles() {
457
490
  _locateFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern, options) {
458
- var patterns, items, i, j, files, k, fileInfo;
491
+ var patterns, items, i, j, files, k;
459
492
  return _regeneratorRuntime().wrap(function _callee$(_context) {
460
493
  while (1) {
461
494
  switch (_context.prev = _context.next) {
@@ -485,12 +518,7 @@ function _locateFiles() {
485
518
  case 9:
486
519
  files = _context.sent;
487
520
  for (k = 0; k < files.length; k++) {
488
- fileInfo = path.parse(files[k]);
489
- items.push({
490
- path: fileInfo.dir.split('/').join(path.sep),
491
- name: fileInfo.name,
492
- extension: fileInfo.ext
493
- });
521
+ items.push(pathToLocatorInfo(files[k], true));
494
522
  }
495
523
  case 11:
496
524
  j++;
@@ -516,7 +544,7 @@ function locateFile(_x3, _x4) {
516
544
  }
517
545
  function _locateFile() {
518
546
  _locateFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(pattern, options) {
519
- var patterns, i, j, files, element, fileInfo;
547
+ var patterns, i, j, files, element;
520
548
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
521
549
  while (1) {
522
550
  switch (_context2.prev = _context2.next) {
@@ -526,13 +554,13 @@ function _locateFile() {
526
554
  i = 0;
527
555
  case 3:
528
556
  if (!(i < patterns.length)) {
529
- _context2.next = 19;
557
+ _context2.next = 18;
530
558
  break;
531
559
  }
532
560
  j = 0;
533
561
  case 5:
534
562
  if (!(j < options.path.length)) {
535
- _context2.next = 16;
563
+ _context2.next = 15;
536
564
  break;
537
565
  }
538
566
  _context2.next = 8;
@@ -546,26 +574,21 @@ function _locateFile() {
546
574
  files = _context2.sent;
547
575
  element = files.shift();
548
576
  if (!element) {
549
- _context2.next = 13;
577
+ _context2.next = 12;
550
578
  break;
551
579
  }
552
- fileInfo = path.parse(element);
553
- return _context2.abrupt("return", {
554
- path: fileInfo.dir.split('/').join(path.sep),
555
- name: fileInfo.name,
556
- extension: fileInfo.ext
557
- });
558
- case 13:
580
+ return _context2.abrupt("return", pathToLocatorInfo(element, true));
581
+ case 12:
559
582
  j++;
560
583
  _context2.next = 5;
561
584
  break;
562
- case 16:
585
+ case 15:
563
586
  i++;
564
587
  _context2.next = 3;
565
588
  break;
566
- case 19:
589
+ case 18:
567
590
  return _context2.abrupt("return", undefined);
568
- case 20:
591
+ case 19:
569
592
  case "end":
570
593
  return _context2.stop();
571
594
  }
@@ -594,12 +617,7 @@ function locateFilesSync(pattern, options) {
594
617
  ignore: options.ignore
595
618
  });
596
619
  for (var k = 0; k < files.length; k++) {
597
- var fileInfo = path.parse(files[k]);
598
- items.push({
599
- path: fileInfo.dir.split('/').join(path.sep),
600
- name: fileInfo.name,
601
- extension: fileInfo.ext
602
- });
620
+ items.push(pathToLocatorInfo(files[k], true));
603
621
  }
604
622
  }
605
623
  }
@@ -618,12 +636,7 @@ function locateFileSync(pattern, options) {
618
636
  });
619
637
  var element = files.shift();
620
638
  if (element) {
621
- var fileInfo = path.parse(element);
622
- return {
623
- path: fileInfo.dir.split('/').join(path.sep),
624
- name: fileInfo.name,
625
- extension: fileInfo.ext
626
- };
639
+ return pathToLocatorInfo(element, true);
627
640
  }
628
641
  }
629
642
  }
@@ -636,72 +649,131 @@ function locateFileSync(pattern, options) {
636
649
  * For the full copyright and license information,
637
650
  * view the LICENSE file that was distributed with this source code.
638
651
  */
639
- function buildLoaderFilePath(info) {
640
- return path.join(info.path, info.name) + (info.extension === '.json' ? '.json' : '');
652
+ function buildLoaderFilePath(info, withExtension) {
653
+ if (withExtension) {
654
+ return path.join(info.path, info.name) + info.extension;
655
+ }
656
+ return path.join(info.path, info.name);
641
657
  }
642
658
 
643
- function loadScriptFile(_x) {
659
+ /*
660
+ * Copyright (c) 2022.
661
+ * Author Peter Placzek (tada5hi)
662
+ * For the full copyright and license information,
663
+ * view the LICENSE file that was distributed with this source code.
664
+ */
665
+ function getExportItem(data, filterFn) {
666
+ if (filterFn) {
667
+ var keys = Object.keys(data);
668
+ for (var i = 0; i < keys.length; i++) {
669
+ if (filterFn(keys[i], data[keys[i]])) {
670
+ return {
671
+ key: keys[i],
672
+ value: data[keys[i]]
673
+ };
674
+ }
675
+ }
676
+ } else {
677
+ return {
678
+ key: 'default',
679
+ value: hasOwnProperty(data, 'default') ? data["default"] : data
680
+ };
681
+ }
682
+
683
+ /* istanbul ignore next */
684
+ return undefined;
685
+ }
686
+
687
+ function loadScriptFile(_x, _x2) {
644
688
  return _loadScriptFile.apply(this, arguments);
645
689
  }
646
690
  function _loadScriptFile() {
647
- _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data) {
648
- var filePath;
691
+ _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data, options) {
692
+ var locatorInfo, filePath;
649
693
  return _regeneratorRuntime().wrap(function _callee$(_context) {
650
694
  while (1) {
651
695
  switch (_context.prev = _context.next) {
652
696
  case 0:
653
- filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
654
- _context.prev = 1;
655
- _context.next = 4;
697
+ if (typeof data === 'string') {
698
+ locatorInfo = pathToLocatorInfo(data);
699
+ } else {
700
+ locatorInfo = data;
701
+ }
702
+ options = options || {};
703
+ filePath = buildLoaderFilePath(locatorInfo, options.withExtension);
704
+ if (options.withFilePrefix) {
705
+ filePath = pathToFileURL(filePath).href;
706
+ }
707
+ _context.prev = 4;
708
+ _context.next = 7;
656
709
  return import(filePath);
657
- case 4:
658
- return _context.abrupt("return", _context.sent);
659
710
  case 7:
660
- _context.prev = 7;
661
- _context.t0 = _context["catch"](1);
662
- return _context.abrupt("return", handleFileLoadError(_context.t0));
711
+ return _context.abrupt("return", _context.sent);
663
712
  case 10:
713
+ _context.prev = 10;
714
+ _context.t0 = _context["catch"](4);
715
+ if (!(_context.t0 instanceof Error && hasOwnProperty(_context.t0, 'code'))) {
716
+ _context.next = 17;
717
+ break;
718
+ }
719
+ if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND')) {
720
+ _context.next = 15;
721
+ break;
722
+ }
723
+ return _context.abrupt("return", loadScriptFile(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
724
+ withExtension: true
725
+ })));
726
+ case 15:
727
+ if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME')) {
728
+ _context.next = 17;
729
+ break;
730
+ }
731
+ return _context.abrupt("return", loadScriptFile(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
732
+ withFilePrefix: true
733
+ })));
734
+ case 17:
735
+ return _context.abrupt("return", handleFileLoadError(_context.t0));
736
+ case 18:
664
737
  case "end":
665
738
  return _context.stop();
666
739
  }
667
740
  }
668
- }, _callee, null, [[1, 7]]);
741
+ }, _callee, null, [[4, 10]]);
669
742
  }));
670
743
  return _loadScriptFile.apply(this, arguments);
671
744
  }
672
- function loadScriptFileExport(_x2, _x3) {
745
+ function loadScriptFileExport(_x3, _x4) {
673
746
  return _loadScriptFileExport.apply(this, arguments);
674
747
  }
675
748
  function _loadScriptFileExport() {
676
749
  _loadScriptFileExport = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(data, filterFn) {
677
- var filePath, _data;
750
+ var output;
678
751
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
679
752
  while (1) {
680
753
  switch (_context2.prev = _context2.next) {
681
754
  case 0:
682
- filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
683
- _context2.prev = 1;
684
- _context2.next = 4;
685
- return loadScriptFile(filePath);
686
- case 4:
687
- _data = _context2.sent;
688
- if (!(_typeof(_data) === 'object')) {
689
- _context2.next = 7;
755
+ _context2.prev = 0;
756
+ _context2.next = 3;
757
+ return loadScriptFile(data);
758
+ case 3:
759
+ output = _context2.sent;
760
+ if (!(_typeof(output) === 'object')) {
761
+ _context2.next = 6;
690
762
  break;
691
763
  }
692
- return _context2.abrupt("return", getExportItem(_data, filterFn));
693
- case 7:
764
+ return _context2.abrupt("return", getExportItem(output, filterFn));
765
+ case 6:
694
766
  return _context2.abrupt("return", undefined);
695
- case 10:
696
- _context2.prev = 10;
697
- _context2.t0 = _context2["catch"](1);
767
+ case 9:
768
+ _context2.prev = 9;
769
+ _context2.t0 = _context2["catch"](0);
698
770
  return _context2.abrupt("return", handleFileLoadError(_context2.t0));
699
- case 13:
771
+ case 12:
700
772
  case "end":
701
773
  return _context2.stop();
702
774
  }
703
775
  }
704
- }, _callee2, null, [[1, 10]]);
776
+ }, _callee2, null, [[0, 9]]);
705
777
  }));
706
778
  return _loadScriptFileExport.apply(this, arguments);
707
779
  }
@@ -732,32 +804,34 @@ function loadJsonFile(_x) {
732
804
  return _loadJsonFile.apply(this, arguments);
733
805
  }
734
806
  function _loadJsonFile() {
735
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(filePath) {
736
- var file;
807
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
808
+ var filePath, file;
737
809
  return _regeneratorRuntime().wrap(function _callee$(_context) {
738
810
  while (1) {
739
811
  switch (_context.prev = _context.next) {
740
812
  case 0:
741
- _context.prev = 0;
742
- _context.next = 3;
813
+ filePath = buildLoaderFilePath(info, true);
814
+ _context.prev = 1;
815
+ _context.next = 4;
743
816
  return fs.promises.readFile(filePath);
744
- case 3:
817
+ case 4:
745
818
  file = _context.sent;
746
819
  return _context.abrupt("return", JSON.parse(file.toString('utf-8')));
747
- case 7:
748
- _context.prev = 7;
749
- _context.t0 = _context["catch"](0);
820
+ case 8:
821
+ _context.prev = 8;
822
+ _context.t0 = _context["catch"](1);
750
823
  return _context.abrupt("return", handleFileLoadError(_context.t0));
751
- case 10:
824
+ case 11:
752
825
  case "end":
753
826
  return _context.stop();
754
827
  }
755
828
  }
756
- }, _callee, null, [[0, 7]]);
829
+ }, _callee, null, [[1, 8]]);
757
830
  }));
758
831
  return _loadJsonFile.apply(this, arguments);
759
832
  }
760
- function loadJsonFileSync(filePath) {
833
+ function loadJsonFileSync(info) {
834
+ var filePath = buildLoaderFilePath(info, true);
761
835
  try {
762
836
  var file = fs.readFileSync(filePath);
763
837
  return JSON.parse(file.toString('utf-8'));
@@ -770,26 +844,30 @@ function loadFile(_x) {
770
844
  return _loadFile.apply(this, arguments);
771
845
  }
772
846
  function _loadFile() {
773
- _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
774
- var filePath;
847
+ _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
848
+ var info;
775
849
  return _regeneratorRuntime().wrap(function _callee$(_context) {
776
850
  while (1) {
777
851
  switch (_context.prev = _context.next) {
778
852
  case 0:
779
- if (info) {
853
+ if (input) {
780
854
  _context.next = 2;
781
855
  break;
782
856
  }
783
857
  return _context.abrupt("return", undefined);
784
858
  case 2:
785
- filePath = buildLoaderFilePath(info);
859
+ if (typeof input === 'string') {
860
+ info = pathToLocatorInfo(input);
861
+ } else {
862
+ info = input;
863
+ }
786
864
  if (!(info.extension === '.json')) {
787
865
  _context.next = 5;
788
866
  break;
789
867
  }
790
- return _context.abrupt("return", loadJsonFile(filePath));
868
+ return _context.abrupt("return", loadJsonFile(info));
791
869
  case 5:
792
- return _context.abrupt("return", loadScriptFile(filePath));
870
+ return _context.abrupt("return", loadScriptFile(info));
793
871
  case 6:
794
872
  case "end":
795
873
  return _context.stop();
@@ -799,15 +877,20 @@ function _loadFile() {
799
877
  }));
800
878
  return _loadFile.apply(this, arguments);
801
879
  }
802
- function loadFileSync(info) {
803
- if (!info) {
880
+ function loadFileSync(input) {
881
+ if (!input) {
804
882
  return undefined;
805
883
  }
806
- var filePath = buildLoaderFilePath(info);
884
+ var info;
885
+ if (typeof input === 'string') {
886
+ info = pathToLocatorInfo(input);
887
+ } else {
888
+ info = input;
889
+ }
807
890
  if (info.extension === '.json') {
808
- return loadJsonFileSync(filePath);
891
+ return loadJsonFileSync(info);
809
892
  }
810
- return loadScriptFileSync(filePath);
893
+ return loadScriptFileSync(info);
811
894
  }
812
895
 
813
- export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, isLocatorInfo, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, removeFileNameExtension, toArray };
896
+ export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, isLocatorInfo, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, pathToLocatorInfo, removeFileNameExtension, toArray };
@@ -1,2 +1,3 @@
1
- export declare function loadJsonFile(filePath: string): Promise<unknown | undefined>;
2
- export declare function loadJsonFileSync(filePath: string): unknown | undefined;
1
+ import { LocatorInfo } from '../../locator';
2
+ export declare function loadJsonFile(info: LocatorInfo): Promise<unknown | undefined>;
3
+ export declare function loadJsonFileSync(info: LocatorInfo): unknown | undefined;
@@ -1,4 +1,9 @@
1
- import { LoaderFilterFn, ScriptFileExportItem } from './type';
2
1
  import { LocatorInfo } from '../../../locator';
3
- export declare function loadScriptFile(data: LocatorInfo | string): Promise<unknown | undefined>;
2
+ import { LoaderFilterFn, ScriptFileExportItem } from './type';
3
+ type ScriptFileLoadOptions = {
4
+ withExtension?: boolean;
5
+ withFilePrefix?: boolean;
6
+ };
7
+ export declare function loadScriptFile(data: LocatorInfo | string, options?: ScriptFileLoadOptions): Promise<unknown | undefined>;
4
8
  export declare function loadScriptFileExport(data: LocatorInfo | string, filterFn?: LoaderFilterFn): Promise<ScriptFileExportItem | undefined>;
9
+ export {};
@@ -1,3 +1,3 @@
1
1
  import { LocatorInfo } from '../locator';
2
- export declare function loadFile(info: LocatorInfo): Promise<unknown | undefined>;
3
- export declare function loadFileSync(info: LocatorInfo): unknown | undefined;
2
+ export declare function loadFile(input: LocatorInfo | string): Promise<unknown | undefined>;
3
+ export declare function loadFileSync(input: LocatorInfo | string): unknown | undefined;
@@ -1,2 +1,2 @@
1
1
  import { LocatorInfo } from '../locator';
2
- export declare function buildLoaderFilePath(info: LocatorInfo): string;
2
+ export declare function buildLoaderFilePath(info: LocatorInfo, withExtension?: boolean): string;
@@ -1,3 +1,4 @@
1
1
  import { LocatorInfo, LocatorOptions } from './type';
2
2
  export declare function buildLocatorOptions(options?: Partial<LocatorOptions>): LocatorOptions;
3
3
  export declare function isLocatorInfo(data: unknown): data is LocatorInfo;
4
+ export declare function pathToLocatorInfo(input: string, skipResolve?: boolean): LocatorInfo;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locter",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "A library to locate a file by criteria and load it",
5
5
  "author": {
6
6
  "name": "Peter Placzek",
@@ -47,14 +47,15 @@
47
47
  "@rollup/plugin-babel": "^6.0.3",
48
48
  "@rollup/plugin-commonjs": "^23.0.2",
49
49
  "@rollup/plugin-node-resolve": "^15.0.0",
50
- "@semantic-release/changelog": "^6.0.1",
50
+ "@semantic-release/changelog": "^6.0.2",
51
51
  "@semantic-release/commit-analyzer": "^9.0.2",
52
52
  "@semantic-release/git": "^10.0.1",
53
- "@semantic-release/github": "^8.0.6",
53
+ "@semantic-release/github": "^8.0.7",
54
54
  "@semantic-release/npm": "^9.0.1",
55
55
  "@semantic-release/release-notes-generator": "^10.0.3",
56
56
  "@tada5hi/eslint-config-typescript": "^1.0.10",
57
57
  "@types/glob": "^8.0.0",
58
+ "@types/is-file-esm": "^1.0.0",
58
59
  "@types/jest": "^27.5.0",
59
60
  "@types/node": "^18.11.9",
60
61
  "@types/yargs": "^17.0.14",