locter 0.4.0 → 0.5.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/dist/index.cjs.js CHANGED
@@ -1,8 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ var isFileEsm = require('is-file-esm');
4
+ var url = require('url');
3
5
  var glob = require('glob');
4
- var path = require('path');
5
6
  var util = require('util');
7
+ var path = require('path');
6
8
  var fs = require('fs');
7
9
 
8
10
  function _regeneratorRuntime() {
@@ -450,6 +452,17 @@ function isLocatorInfo(data) {
450
452
  }
451
453
  return !(!hasOwnProperty(data, 'extension') || typeof data.extension !== 'string');
452
454
  }
455
+ function pathToLocatorInfo(input, skipResolve) {
456
+ if (!skipResolve && !path.isAbsolute(input)) {
457
+ input = path.resolve(process.cwd(), input);
458
+ }
459
+ var info = path.parse(input);
460
+ return {
461
+ path: info.dir.split('/').join(path.sep),
462
+ name: info.name,
463
+ extension: info.ext
464
+ };
465
+ }
453
466
 
454
467
  var globAsync = util.promisify(glob.glob);
455
468
  function locateFiles(_x, _x2) {
@@ -457,7 +470,7 @@ function locateFiles(_x, _x2) {
457
470
  }
458
471
  function _locateFiles() {
459
472
  _locateFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern, options) {
460
- var patterns, items, i, j, files, k, fileInfo;
473
+ var patterns, items, i, j, files, k;
461
474
  return _regeneratorRuntime().wrap(function _callee$(_context) {
462
475
  while (1) {
463
476
  switch (_context.prev = _context.next) {
@@ -487,12 +500,7 @@ function _locateFiles() {
487
500
  case 9:
488
501
  files = _context.sent;
489
502
  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
- });
503
+ items.push(pathToLocatorInfo(files[k], true));
496
504
  }
497
505
  case 11:
498
506
  j++;
@@ -518,7 +526,7 @@ function locateFile(_x3, _x4) {
518
526
  }
519
527
  function _locateFile() {
520
528
  _locateFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(pattern, options) {
521
- var patterns, i, j, files, element, fileInfo;
529
+ var patterns, i, j, files, element;
522
530
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
523
531
  while (1) {
524
532
  switch (_context2.prev = _context2.next) {
@@ -528,13 +536,13 @@ function _locateFile() {
528
536
  i = 0;
529
537
  case 3:
530
538
  if (!(i < patterns.length)) {
531
- _context2.next = 19;
539
+ _context2.next = 18;
532
540
  break;
533
541
  }
534
542
  j = 0;
535
543
  case 5:
536
544
  if (!(j < options.path.length)) {
537
- _context2.next = 16;
545
+ _context2.next = 15;
538
546
  break;
539
547
  }
540
548
  _context2.next = 8;
@@ -548,26 +556,21 @@ function _locateFile() {
548
556
  files = _context2.sent;
549
557
  element = files.shift();
550
558
  if (!element) {
551
- _context2.next = 13;
559
+ _context2.next = 12;
552
560
  break;
553
561
  }
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:
562
+ return _context2.abrupt("return", pathToLocatorInfo(element, true));
563
+ case 12:
561
564
  j++;
562
565
  _context2.next = 5;
563
566
  break;
564
- case 16:
567
+ case 15:
565
568
  i++;
566
569
  _context2.next = 3;
567
570
  break;
568
- case 19:
571
+ case 18:
569
572
  return _context2.abrupt("return", undefined);
570
- case 20:
573
+ case 19:
571
574
  case "end":
572
575
  return _context2.stop();
573
576
  }
@@ -596,12 +599,7 @@ function locateFilesSync(pattern, options) {
596
599
  ignore: options.ignore
597
600
  });
598
601
  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
- });
602
+ items.push(pathToLocatorInfo(files[k], true));
605
603
  }
606
604
  }
607
605
  }
@@ -620,12 +618,7 @@ function locateFileSync(pattern, options) {
620
618
  });
621
619
  var element = files.shift();
622
620
  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
- };
621
+ return pathToLocatorInfo(element, true);
629
622
  }
630
623
  }
631
624
  }
@@ -638,8 +631,11 @@ function locateFileSync(pattern, options) {
638
631
  * For the full copyright and license information,
639
632
  * view the LICENSE file that was distributed with this source code.
640
633
  */
641
- function buildLoaderFilePath(info) {
642
- return path.join(info.path, info.name) + (info.extension === '.json' ? '.json' : '');
634
+ function buildLoaderFilePath(info, withExtension) {
635
+ if (withExtension) {
636
+ return path.join(info.path, info.name) + info.extension;
637
+ }
638
+ return path.join(info.path, info.name);
643
639
  }
644
640
 
645
641
  function loadScriptFile(_x) {
@@ -647,27 +643,50 @@ function loadScriptFile(_x) {
647
643
  }
648
644
  function _loadScriptFile() {
649
645
  _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data) {
650
- var filePath;
646
+ var locatorInfo, filePath, check;
651
647
  return _regeneratorRuntime().wrap(function _callee$(_context) {
652
648
  while (1) {
653
649
  switch (_context.prev = _context.next) {
654
650
  case 0:
655
- filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
651
+ if (typeof data === 'string') {
652
+ filePath = data;
653
+ locatorInfo = pathToLocatorInfo(data);
654
+ } else {
655
+ filePath = buildLoaderFilePath(data, true);
656
+ locatorInfo = data;
657
+ }
656
658
  _context.prev = 1;
657
- _context.next = 4;
659
+ if (!(['.js', '.mjs', '.cjs'].indexOf(locatorInfo.extension) !== -1)) {
660
+ _context.next = 10;
661
+ break;
662
+ }
663
+ _context.next = 5;
664
+ return isFileEsm(filePath);
665
+ case 5:
666
+ check = _context.sent;
667
+ if (!check.esm) {
668
+ _context.next = 10;
669
+ break;
670
+ }
671
+ _context.next = 9;
672
+ return import(url.pathToFileURL(filePath).href);
673
+ case 9:
674
+ return _context.abrupt("return", _context.sent);
675
+ case 10:
676
+ _context.next = 12;
658
677
  return import(filePath);
659
- case 4:
678
+ case 12:
660
679
  return _context.abrupt("return", _context.sent);
661
- case 7:
662
- _context.prev = 7;
680
+ case 15:
681
+ _context.prev = 15;
663
682
  _context.t0 = _context["catch"](1);
664
683
  return _context.abrupt("return", handleFileLoadError(_context.t0));
665
- case 10:
684
+ case 18:
666
685
  case "end":
667
686
  return _context.stop();
668
687
  }
669
688
  }
670
- }, _callee, null, [[1, 7]]);
689
+ }, _callee, null, [[1, 15]]);
671
690
  }));
672
691
  return _loadScriptFile.apply(this, arguments);
673
692
  }
@@ -676,34 +695,33 @@ function loadScriptFileExport(_x2, _x3) {
676
695
  }
677
696
  function _loadScriptFileExport() {
678
697
  _loadScriptFileExport = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(data, filterFn) {
679
- var filePath, _data;
698
+ var output;
680
699
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
681
700
  while (1) {
682
701
  switch (_context2.prev = _context2.next) {
683
702
  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;
703
+ _context2.prev = 0;
704
+ _context2.next = 3;
705
+ return loadScriptFile(data);
706
+ case 3:
707
+ output = _context2.sent;
708
+ if (!(_typeof(output) === 'object')) {
709
+ _context2.next = 6;
692
710
  break;
693
711
  }
694
- return _context2.abrupt("return", getExportItem(_data, filterFn));
695
- case 7:
712
+ return _context2.abrupt("return", getExportItem(output, filterFn));
713
+ case 6:
696
714
  return _context2.abrupt("return", undefined);
697
- case 10:
698
- _context2.prev = 10;
699
- _context2.t0 = _context2["catch"](1);
715
+ case 9:
716
+ _context2.prev = 9;
717
+ _context2.t0 = _context2["catch"](0);
700
718
  return _context2.abrupt("return", handleFileLoadError(_context2.t0));
701
- case 13:
719
+ case 12:
702
720
  case "end":
703
721
  return _context2.stop();
704
722
  }
705
723
  }
706
- }, _callee2, null, [[1, 10]]);
724
+ }, _callee2, null, [[0, 9]]);
707
725
  }));
708
726
  return _loadScriptFileExport.apply(this, arguments);
709
727
  }
@@ -734,32 +752,34 @@ function loadJsonFile(_x) {
734
752
  return _loadJsonFile.apply(this, arguments);
735
753
  }
736
754
  function _loadJsonFile() {
737
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(filePath) {
738
- var file;
755
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
756
+ var filePath, file;
739
757
  return _regeneratorRuntime().wrap(function _callee$(_context) {
740
758
  while (1) {
741
759
  switch (_context.prev = _context.next) {
742
760
  case 0:
743
- _context.prev = 0;
744
- _context.next = 3;
761
+ filePath = buildLoaderFilePath(info, true);
762
+ _context.prev = 1;
763
+ _context.next = 4;
745
764
  return fs.promises.readFile(filePath);
746
- case 3:
765
+ case 4:
747
766
  file = _context.sent;
748
767
  return _context.abrupt("return", JSON.parse(file.toString('utf-8')));
749
- case 7:
750
- _context.prev = 7;
751
- _context.t0 = _context["catch"](0);
768
+ case 8:
769
+ _context.prev = 8;
770
+ _context.t0 = _context["catch"](1);
752
771
  return _context.abrupt("return", handleFileLoadError(_context.t0));
753
- case 10:
772
+ case 11:
754
773
  case "end":
755
774
  return _context.stop();
756
775
  }
757
776
  }
758
- }, _callee, null, [[0, 7]]);
777
+ }, _callee, null, [[1, 8]]);
759
778
  }));
760
779
  return _loadJsonFile.apply(this, arguments);
761
780
  }
762
- function loadJsonFileSync(filePath) {
781
+ function loadJsonFileSync(info) {
782
+ var filePath = buildLoaderFilePath(info, true);
763
783
  try {
764
784
  var file = fs.readFileSync(filePath);
765
785
  return JSON.parse(file.toString('utf-8'));
@@ -772,26 +792,30 @@ function loadFile(_x) {
772
792
  return _loadFile.apply(this, arguments);
773
793
  }
774
794
  function _loadFile() {
775
- _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
776
- var filePath;
795
+ _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
796
+ var info;
777
797
  return _regeneratorRuntime().wrap(function _callee$(_context) {
778
798
  while (1) {
779
799
  switch (_context.prev = _context.next) {
780
800
  case 0:
781
- if (info) {
801
+ if (input) {
782
802
  _context.next = 2;
783
803
  break;
784
804
  }
785
805
  return _context.abrupt("return", undefined);
786
806
  case 2:
787
- filePath = buildLoaderFilePath(info);
807
+ if (typeof input === 'string') {
808
+ info = pathToLocatorInfo(input);
809
+ } else {
810
+ info = input;
811
+ }
788
812
  if (!(info.extension === '.json')) {
789
813
  _context.next = 5;
790
814
  break;
791
815
  }
792
- return _context.abrupt("return", loadJsonFile(filePath));
816
+ return _context.abrupt("return", loadJsonFile(info));
793
817
  case 5:
794
- return _context.abrupt("return", loadScriptFile(filePath));
818
+ return _context.abrupt("return", loadScriptFile(info));
795
819
  case 6:
796
820
  case "end":
797
821
  return _context.stop();
@@ -801,15 +825,20 @@ function _loadFile() {
801
825
  }));
802
826
  return _loadFile.apply(this, arguments);
803
827
  }
804
- function loadFileSync(info) {
805
- if (!info) {
828
+ function loadFileSync(input) {
829
+ if (!input) {
806
830
  return undefined;
807
831
  }
808
- var filePath = buildLoaderFilePath(info);
832
+ var info;
833
+ if (typeof input === 'string') {
834
+ info = pathToLocatorInfo(input);
835
+ } else {
836
+ info = input;
837
+ }
809
838
  if (info.extension === '.json') {
810
- return loadJsonFileSync(filePath);
839
+ return loadJsonFileSync(info);
811
840
  }
812
- return loadScriptFileSync(filePath);
841
+ return loadScriptFileSync(info);
813
842
  }
814
843
 
815
844
  exports.buildLoaderFilePath = buildLoaderFilePath;
@@ -830,5 +859,6 @@ exports.locateFile = locateFile;
830
859
  exports.locateFileSync = locateFileSync;
831
860
  exports.locateFiles = locateFiles;
832
861
  exports.locateFilesSync = locateFilesSync;
862
+ exports.pathToLocatorInfo = pathToLocatorInfo;
833
863
  exports.removeFileNameExtension = removeFileNameExtension;
834
864
  exports.toArray = toArray;
package/dist/index.esm.js CHANGED
@@ -1,6 +1,8 @@
1
+ import isFileEsm from 'is-file-esm';
2
+ import { pathToFileURL } from 'url';
1
3
  import { glob, sync } from 'glob';
2
- import path from 'path';
3
4
  import { promisify } from 'util';
5
+ import path from 'path';
4
6
  import fs from 'fs';
5
7
 
6
8
  function _regeneratorRuntime() {
@@ -448,6 +450,17 @@ function isLocatorInfo(data) {
448
450
  }
449
451
  return !(!hasOwnProperty(data, 'extension') || typeof data.extension !== 'string');
450
452
  }
453
+ function pathToLocatorInfo(input, skipResolve) {
454
+ if (!skipResolve && !path.isAbsolute(input)) {
455
+ input = path.resolve(process.cwd(), input);
456
+ }
457
+ var info = path.parse(input);
458
+ return {
459
+ path: info.dir.split('/').join(path.sep),
460
+ name: info.name,
461
+ extension: info.ext
462
+ };
463
+ }
451
464
 
452
465
  var globAsync = promisify(glob);
453
466
  function locateFiles(_x, _x2) {
@@ -455,7 +468,7 @@ function locateFiles(_x, _x2) {
455
468
  }
456
469
  function _locateFiles() {
457
470
  _locateFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern, options) {
458
- var patterns, items, i, j, files, k, fileInfo;
471
+ var patterns, items, i, j, files, k;
459
472
  return _regeneratorRuntime().wrap(function _callee$(_context) {
460
473
  while (1) {
461
474
  switch (_context.prev = _context.next) {
@@ -485,12 +498,7 @@ function _locateFiles() {
485
498
  case 9:
486
499
  files = _context.sent;
487
500
  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
- });
501
+ items.push(pathToLocatorInfo(files[k], true));
494
502
  }
495
503
  case 11:
496
504
  j++;
@@ -516,7 +524,7 @@ function locateFile(_x3, _x4) {
516
524
  }
517
525
  function _locateFile() {
518
526
  _locateFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(pattern, options) {
519
- var patterns, i, j, files, element, fileInfo;
527
+ var patterns, i, j, files, element;
520
528
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
521
529
  while (1) {
522
530
  switch (_context2.prev = _context2.next) {
@@ -526,13 +534,13 @@ function _locateFile() {
526
534
  i = 0;
527
535
  case 3:
528
536
  if (!(i < patterns.length)) {
529
- _context2.next = 19;
537
+ _context2.next = 18;
530
538
  break;
531
539
  }
532
540
  j = 0;
533
541
  case 5:
534
542
  if (!(j < options.path.length)) {
535
- _context2.next = 16;
543
+ _context2.next = 15;
536
544
  break;
537
545
  }
538
546
  _context2.next = 8;
@@ -546,26 +554,21 @@ function _locateFile() {
546
554
  files = _context2.sent;
547
555
  element = files.shift();
548
556
  if (!element) {
549
- _context2.next = 13;
557
+ _context2.next = 12;
550
558
  break;
551
559
  }
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:
560
+ return _context2.abrupt("return", pathToLocatorInfo(element, true));
561
+ case 12:
559
562
  j++;
560
563
  _context2.next = 5;
561
564
  break;
562
- case 16:
565
+ case 15:
563
566
  i++;
564
567
  _context2.next = 3;
565
568
  break;
566
- case 19:
569
+ case 18:
567
570
  return _context2.abrupt("return", undefined);
568
- case 20:
571
+ case 19:
569
572
  case "end":
570
573
  return _context2.stop();
571
574
  }
@@ -594,12 +597,7 @@ function locateFilesSync(pattern, options) {
594
597
  ignore: options.ignore
595
598
  });
596
599
  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
- });
600
+ items.push(pathToLocatorInfo(files[k], true));
603
601
  }
604
602
  }
605
603
  }
@@ -618,12 +616,7 @@ function locateFileSync(pattern, options) {
618
616
  });
619
617
  var element = files.shift();
620
618
  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
- };
619
+ return pathToLocatorInfo(element, true);
627
620
  }
628
621
  }
629
622
  }
@@ -636,8 +629,11 @@ function locateFileSync(pattern, options) {
636
629
  * For the full copyright and license information,
637
630
  * view the LICENSE file that was distributed with this source code.
638
631
  */
639
- function buildLoaderFilePath(info) {
640
- return path.join(info.path, info.name) + (info.extension === '.json' ? '.json' : '');
632
+ function buildLoaderFilePath(info, withExtension) {
633
+ if (withExtension) {
634
+ return path.join(info.path, info.name) + info.extension;
635
+ }
636
+ return path.join(info.path, info.name);
641
637
  }
642
638
 
643
639
  function loadScriptFile(_x) {
@@ -645,27 +641,50 @@ function loadScriptFile(_x) {
645
641
  }
646
642
  function _loadScriptFile() {
647
643
  _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data) {
648
- var filePath;
644
+ var locatorInfo, filePath, check;
649
645
  return _regeneratorRuntime().wrap(function _callee$(_context) {
650
646
  while (1) {
651
647
  switch (_context.prev = _context.next) {
652
648
  case 0:
653
- filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
649
+ if (typeof data === 'string') {
650
+ filePath = data;
651
+ locatorInfo = pathToLocatorInfo(data);
652
+ } else {
653
+ filePath = buildLoaderFilePath(data, true);
654
+ locatorInfo = data;
655
+ }
654
656
  _context.prev = 1;
655
- _context.next = 4;
657
+ if (!(['.js', '.mjs', '.cjs'].indexOf(locatorInfo.extension) !== -1)) {
658
+ _context.next = 10;
659
+ break;
660
+ }
661
+ _context.next = 5;
662
+ return isFileEsm(filePath);
663
+ case 5:
664
+ check = _context.sent;
665
+ if (!check.esm) {
666
+ _context.next = 10;
667
+ break;
668
+ }
669
+ _context.next = 9;
670
+ return import(pathToFileURL(filePath).href);
671
+ case 9:
672
+ return _context.abrupt("return", _context.sent);
673
+ case 10:
674
+ _context.next = 12;
656
675
  return import(filePath);
657
- case 4:
676
+ case 12:
658
677
  return _context.abrupt("return", _context.sent);
659
- case 7:
660
- _context.prev = 7;
678
+ case 15:
679
+ _context.prev = 15;
661
680
  _context.t0 = _context["catch"](1);
662
681
  return _context.abrupt("return", handleFileLoadError(_context.t0));
663
- case 10:
682
+ case 18:
664
683
  case "end":
665
684
  return _context.stop();
666
685
  }
667
686
  }
668
- }, _callee, null, [[1, 7]]);
687
+ }, _callee, null, [[1, 15]]);
669
688
  }));
670
689
  return _loadScriptFile.apply(this, arguments);
671
690
  }
@@ -674,34 +693,33 @@ function loadScriptFileExport(_x2, _x3) {
674
693
  }
675
694
  function _loadScriptFileExport() {
676
695
  _loadScriptFileExport = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(data, filterFn) {
677
- var filePath, _data;
696
+ var output;
678
697
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
679
698
  while (1) {
680
699
  switch (_context2.prev = _context2.next) {
681
700
  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;
701
+ _context2.prev = 0;
702
+ _context2.next = 3;
703
+ return loadScriptFile(data);
704
+ case 3:
705
+ output = _context2.sent;
706
+ if (!(_typeof(output) === 'object')) {
707
+ _context2.next = 6;
690
708
  break;
691
709
  }
692
- return _context2.abrupt("return", getExportItem(_data, filterFn));
693
- case 7:
710
+ return _context2.abrupt("return", getExportItem(output, filterFn));
711
+ case 6:
694
712
  return _context2.abrupt("return", undefined);
695
- case 10:
696
- _context2.prev = 10;
697
- _context2.t0 = _context2["catch"](1);
713
+ case 9:
714
+ _context2.prev = 9;
715
+ _context2.t0 = _context2["catch"](0);
698
716
  return _context2.abrupt("return", handleFileLoadError(_context2.t0));
699
- case 13:
717
+ case 12:
700
718
  case "end":
701
719
  return _context2.stop();
702
720
  }
703
721
  }
704
- }, _callee2, null, [[1, 10]]);
722
+ }, _callee2, null, [[0, 9]]);
705
723
  }));
706
724
  return _loadScriptFileExport.apply(this, arguments);
707
725
  }
@@ -732,32 +750,34 @@ function loadJsonFile(_x) {
732
750
  return _loadJsonFile.apply(this, arguments);
733
751
  }
734
752
  function _loadJsonFile() {
735
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(filePath) {
736
- var file;
753
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
754
+ var filePath, file;
737
755
  return _regeneratorRuntime().wrap(function _callee$(_context) {
738
756
  while (1) {
739
757
  switch (_context.prev = _context.next) {
740
758
  case 0:
741
- _context.prev = 0;
742
- _context.next = 3;
759
+ filePath = buildLoaderFilePath(info, true);
760
+ _context.prev = 1;
761
+ _context.next = 4;
743
762
  return fs.promises.readFile(filePath);
744
- case 3:
763
+ case 4:
745
764
  file = _context.sent;
746
765
  return _context.abrupt("return", JSON.parse(file.toString('utf-8')));
747
- case 7:
748
- _context.prev = 7;
749
- _context.t0 = _context["catch"](0);
766
+ case 8:
767
+ _context.prev = 8;
768
+ _context.t0 = _context["catch"](1);
750
769
  return _context.abrupt("return", handleFileLoadError(_context.t0));
751
- case 10:
770
+ case 11:
752
771
  case "end":
753
772
  return _context.stop();
754
773
  }
755
774
  }
756
- }, _callee, null, [[0, 7]]);
775
+ }, _callee, null, [[1, 8]]);
757
776
  }));
758
777
  return _loadJsonFile.apply(this, arguments);
759
778
  }
760
- function loadJsonFileSync(filePath) {
779
+ function loadJsonFileSync(info) {
780
+ var filePath = buildLoaderFilePath(info, true);
761
781
  try {
762
782
  var file = fs.readFileSync(filePath);
763
783
  return JSON.parse(file.toString('utf-8'));
@@ -770,26 +790,30 @@ function loadFile(_x) {
770
790
  return _loadFile.apply(this, arguments);
771
791
  }
772
792
  function _loadFile() {
773
- _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
774
- var filePath;
793
+ _loadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
794
+ var info;
775
795
  return _regeneratorRuntime().wrap(function _callee$(_context) {
776
796
  while (1) {
777
797
  switch (_context.prev = _context.next) {
778
798
  case 0:
779
- if (info) {
799
+ if (input) {
780
800
  _context.next = 2;
781
801
  break;
782
802
  }
783
803
  return _context.abrupt("return", undefined);
784
804
  case 2:
785
- filePath = buildLoaderFilePath(info);
805
+ if (typeof input === 'string') {
806
+ info = pathToLocatorInfo(input);
807
+ } else {
808
+ info = input;
809
+ }
786
810
  if (!(info.extension === '.json')) {
787
811
  _context.next = 5;
788
812
  break;
789
813
  }
790
- return _context.abrupt("return", loadJsonFile(filePath));
814
+ return _context.abrupt("return", loadJsonFile(info));
791
815
  case 5:
792
- return _context.abrupt("return", loadScriptFile(filePath));
816
+ return _context.abrupt("return", loadScriptFile(info));
793
817
  case 6:
794
818
  case "end":
795
819
  return _context.stop();
@@ -799,15 +823,20 @@ function _loadFile() {
799
823
  }));
800
824
  return _loadFile.apply(this, arguments);
801
825
  }
802
- function loadFileSync(info) {
803
- if (!info) {
826
+ function loadFileSync(input) {
827
+ if (!input) {
804
828
  return undefined;
805
829
  }
806
- var filePath = buildLoaderFilePath(info);
830
+ var info;
831
+ if (typeof input === 'string') {
832
+ info = pathToLocatorInfo(input);
833
+ } else {
834
+ info = input;
835
+ }
807
836
  if (info.extension === '.json') {
808
- return loadJsonFileSync(filePath);
837
+ return loadJsonFileSync(info);
809
838
  }
810
- return loadScriptFileSync(filePath);
839
+ return loadScriptFileSync(info);
811
840
  }
812
841
 
813
- export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, isLocatorInfo, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, removeFileNameExtension, toArray };
842
+ 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,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.0",
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",
@@ -71,7 +72,8 @@
71
72
  "typescript": "^4.9.3"
72
73
  },
73
74
  "dependencies": {
74
- "glob": "^8.0.3"
75
+ "glob": "^8.0.3",
76
+ "is-file-esm": "^1.0.0"
75
77
  },
76
78
  "config": {
77
79
  "commitizen": {