locter 0.5.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,12 +1,32 @@
1
1
  'use strict';
2
2
 
3
- var isFileEsm = require('is-file-esm');
4
3
  var url = require('url');
5
4
  var glob = require('glob');
6
5
  var util = require('util');
7
6
  var path = require('path');
8
7
  var fs = require('fs');
9
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
+ }
10
30
  function _regeneratorRuntime() {
11
31
  _regeneratorRuntime = function () {
12
32
  return exports;
@@ -347,6 +367,34 @@ function _asyncToGenerator(fn) {
347
367
  });
348
368
  };
349
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
+ }
350
398
 
351
399
  /*
352
400
  * Copyright (c) 2022.
@@ -399,34 +447,6 @@ function toArray(input) {
399
447
  return Array.isArray(input) ? input : [input];
400
448
  }
401
449
 
402
- /*
403
- * Copyright (c) 2022.
404
- * Author Peter Placzek (tada5hi)
405
- * For the full copyright and license information,
406
- * view the LICENSE file that was distributed with this source code.
407
- */
408
- function getExportItem(data, filterFn) {
409
- if (filterFn) {
410
- var keys = Object.keys(data);
411
- for (var i = 0; i < keys.length; i++) {
412
- if (filterFn(keys[i], data[keys[i]])) {
413
- return {
414
- key: keys[i],
415
- value: data[keys[i]]
416
- };
417
- }
418
- }
419
- } else {
420
- return {
421
- key: 'default',
422
- value: hasOwnProperty(data, 'default') ? data["default"] : data
423
- };
424
- }
425
-
426
- /* istanbul ignore next */
427
- return undefined;
428
- }
429
-
430
450
  function buildLocatorOptions(options) {
431
451
  var _options, _options$ignore;
432
452
  options = options || {};
@@ -638,59 +658,93 @@ function buildLoaderFilePath(info, withExtension) {
638
658
  return path.join(info.path, info.name);
639
659
  }
640
660
 
641
- 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) {
642
690
  return _loadScriptFile.apply(this, arguments);
643
691
  }
644
692
  function _loadScriptFile() {
645
- _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data) {
646
- var locatorInfo, filePath, check;
693
+ _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data, options) {
694
+ var locatorInfo, filePath;
647
695
  return _regeneratorRuntime().wrap(function _callee$(_context) {
648
696
  while (1) {
649
697
  switch (_context.prev = _context.next) {
650
698
  case 0:
651
699
  if (typeof data === 'string') {
652
- filePath = data;
653
700
  locatorInfo = pathToLocatorInfo(data);
654
701
  } else {
655
- filePath = buildLoaderFilePath(data, true);
656
702
  locatorInfo = data;
657
703
  }
658
- _context.prev = 1;
659
- if (!(['.js', '.mjs', '.cjs'].indexOf(locatorInfo.extension) !== -1)) {
660
- _context.next = 10;
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;
711
+ return import(filePath);
712
+ case 7:
713
+ return _context.abrupt("return", _context.sent);
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;
661
719
  break;
662
720
  }
663
- _context.next = 5;
664
- return isFileEsm(filePath);
665
- case 5:
666
- check = _context.sent;
667
- if (!check.esm) {
668
- _context.next = 10;
721
+ if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND')) {
722
+ _context.next = 15;
669
723
  break;
670
724
  }
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;
677
- return import(filePath);
678
- case 12:
679
- return _context.abrupt("return", _context.sent);
725
+ return _context.abrupt("return", loadScriptFile(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
726
+ withExtension: true
727
+ })));
680
728
  case 15:
681
- _context.prev = 15;
682
- _context.t0 = _context["catch"](1);
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:
683
737
  return _context.abrupt("return", handleFileLoadError(_context.t0));
684
738
  case 18:
685
739
  case "end":
686
740
  return _context.stop();
687
741
  }
688
742
  }
689
- }, _callee, null, [[1, 15]]);
743
+ }, _callee, null, [[4, 10]]);
690
744
  }));
691
745
  return _loadScriptFile.apply(this, arguments);
692
746
  }
693
- function loadScriptFileExport(_x2, _x3) {
747
+ function loadScriptFileExport(_x3, _x4) {
694
748
  return _loadScriptFileExport.apply(this, arguments);
695
749
  }
696
750
  function _loadScriptFileExport() {
package/dist/index.esm.js CHANGED
@@ -1,10 +1,30 @@
1
- import isFileEsm from 'is-file-esm';
2
1
  import { pathToFileURL } from 'url';
3
2
  import { glob, sync } from 'glob';
4
3
  import { promisify } from 'util';
5
4
  import path from 'path';
6
5
  import fs from 'fs';
7
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
+ }
8
28
  function _regeneratorRuntime() {
9
29
  _regeneratorRuntime = function () {
10
30
  return exports;
@@ -345,6 +365,34 @@ function _asyncToGenerator(fn) {
345
365
  });
346
366
  };
347
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
+ }
348
396
 
349
397
  /*
350
398
  * Copyright (c) 2022.
@@ -397,34 +445,6 @@ function toArray(input) {
397
445
  return Array.isArray(input) ? input : [input];
398
446
  }
399
447
 
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
448
  function buildLocatorOptions(options) {
429
449
  var _options, _options$ignore;
430
450
  options = options || {};
@@ -636,59 +656,93 @@ function buildLoaderFilePath(info, withExtension) {
636
656
  return path.join(info.path, info.name);
637
657
  }
638
658
 
639
- 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) {
640
688
  return _loadScriptFile.apply(this, arguments);
641
689
  }
642
690
  function _loadScriptFile() {
643
- _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data) {
644
- var locatorInfo, filePath, check;
691
+ _loadScriptFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(data, options) {
692
+ var locatorInfo, filePath;
645
693
  return _regeneratorRuntime().wrap(function _callee$(_context) {
646
694
  while (1) {
647
695
  switch (_context.prev = _context.next) {
648
696
  case 0:
649
697
  if (typeof data === 'string') {
650
- filePath = data;
651
698
  locatorInfo = pathToLocatorInfo(data);
652
699
  } else {
653
- filePath = buildLoaderFilePath(data, true);
654
700
  locatorInfo = data;
655
701
  }
656
- _context.prev = 1;
657
- if (!(['.js', '.mjs', '.cjs'].indexOf(locatorInfo.extension) !== -1)) {
658
- _context.next = 10;
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;
709
+ return import(filePath);
710
+ case 7:
711
+ return _context.abrupt("return", _context.sent);
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;
659
717
  break;
660
718
  }
661
- _context.next = 5;
662
- return isFileEsm(filePath);
663
- case 5:
664
- check = _context.sent;
665
- if (!check.esm) {
666
- _context.next = 10;
719
+ if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND')) {
720
+ _context.next = 15;
667
721
  break;
668
722
  }
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;
675
- return import(filePath);
676
- case 12:
677
- return _context.abrupt("return", _context.sent);
723
+ return _context.abrupt("return", loadScriptFile(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
724
+ withExtension: true
725
+ })));
678
726
  case 15:
679
- _context.prev = 15;
680
- _context.t0 = _context["catch"](1);
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:
681
735
  return _context.abrupt("return", handleFileLoadError(_context.t0));
682
736
  case 18:
683
737
  case "end":
684
738
  return _context.stop();
685
739
  }
686
740
  }
687
- }, _callee, null, [[1, 15]]);
741
+ }, _callee, null, [[4, 10]]);
688
742
  }));
689
743
  return _loadScriptFile.apply(this, arguments);
690
744
  }
691
- function loadScriptFileExport(_x2, _x3) {
745
+ function loadScriptFileExport(_x3, _x4) {
692
746
  return _loadScriptFileExport.apply(this, arguments);
693
747
  }
694
748
  function _loadScriptFileExport() {
@@ -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 {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locter",
3
- "version": "0.5.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",
@@ -72,8 +72,7 @@
72
72
  "typescript": "^4.9.3"
73
73
  },
74
74
  "dependencies": {
75
- "glob": "^8.0.3",
76
- "is-file-esm": "^1.0.0"
75
+ "glob": "^8.0.3"
77
76
  },
78
77
  "config": {
79
78
  "commitizen": {