locter 0.5.1 → 0.5.2

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
@@ -436,6 +436,17 @@ function hasOwnProperty(obj, prop) {
436
436
  return Object.prototype.hasOwnProperty.call(obj, prop);
437
437
  }
438
438
 
439
+ /*
440
+ * Copyright (c) 2022.
441
+ * Author Peter Placzek (tada5hi)
442
+ * For the full copyright and license information,
443
+ * view the LICENSE file that was distributed with this source code.
444
+ */
445
+
446
+ function isObject(item) {
447
+ return !!item && _typeof(item) === 'object' && !Array.isArray(item);
448
+ }
449
+
439
450
  /*
440
451
  * Copyright (c) 2022.
441
452
  * Author Peter Placzek (tada5hi)
@@ -714,11 +725,11 @@ function _loadScriptFile() {
714
725
  case 10:
715
726
  _context.prev = 10;
716
727
  _context.t0 = _context["catch"](4);
717
- if (!(_context.t0 instanceof Error && hasOwnProperty(_context.t0, 'code'))) {
728
+ if (!(isObject(_context.t0) && hasOwnProperty(_context.t0, 'code'))) {
718
729
  _context.next = 17;
719
730
  break;
720
731
  }
721
- if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND')) {
732
+ if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND')) {
722
733
  _context.next = 15;
723
734
  break;
724
735
  }
@@ -726,7 +737,7 @@ function _loadScriptFile() {
726
737
  withExtension: true
727
738
  })));
728
739
  case 15:
729
- if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME')) {
740
+ if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || _context.t0.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
730
741
  _context.next = 17;
731
742
  break;
732
743
  }
@@ -780,12 +791,26 @@ function _loadScriptFileExport() {
780
791
  return _loadScriptFileExport.apply(this, arguments);
781
792
  }
782
793
 
783
- function loadScriptFileSync(data) {
784
- var filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
794
+ function loadScriptFileSync(data, options) {
795
+ var locatorInfo;
796
+ if (typeof data === 'string') {
797
+ locatorInfo = pathToLocatorInfo(data);
798
+ } else {
799
+ locatorInfo = data;
800
+ }
801
+ options = options || {};
802
+ var filePath = buildLoaderFilePath(locatorInfo, options.withExtension);
785
803
  try {
786
804
  // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require
787
805
  return require(filePath);
788
806
  } catch (e) {
807
+ if (isObject(e) && hasOwnProperty(e, 'code')) {
808
+ if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') {
809
+ return loadScriptFileSync(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
810
+ withExtension: true
811
+ }));
812
+ }
813
+ }
789
814
  return handleFileLoadError(e);
790
815
  }
791
816
  }
@@ -901,6 +926,7 @@ exports.getExportItem = getExportItem;
901
926
  exports.handleFileLoadError = handleFileLoadError;
902
927
  exports.hasOwnProperty = hasOwnProperty;
903
928
  exports.isLocatorInfo = isLocatorInfo;
929
+ exports.isObject = isObject;
904
930
  exports.loadFile = loadFile;
905
931
  exports.loadFileSync = loadFileSync;
906
932
  exports.loadJsonFile = loadJsonFile;
package/dist/index.esm.js CHANGED
@@ -434,6 +434,17 @@ function hasOwnProperty(obj, prop) {
434
434
  return Object.prototype.hasOwnProperty.call(obj, prop);
435
435
  }
436
436
 
437
+ /*
438
+ * Copyright (c) 2022.
439
+ * Author Peter Placzek (tada5hi)
440
+ * For the full copyright and license information,
441
+ * view the LICENSE file that was distributed with this source code.
442
+ */
443
+
444
+ function isObject(item) {
445
+ return !!item && _typeof(item) === 'object' && !Array.isArray(item);
446
+ }
447
+
437
448
  /*
438
449
  * Copyright (c) 2022.
439
450
  * Author Peter Placzek (tada5hi)
@@ -712,11 +723,11 @@ function _loadScriptFile() {
712
723
  case 10:
713
724
  _context.prev = 10;
714
725
  _context.t0 = _context["catch"](4);
715
- if (!(_context.t0 instanceof Error && hasOwnProperty(_context.t0, 'code'))) {
726
+ if (!(isObject(_context.t0) && hasOwnProperty(_context.t0, 'code'))) {
716
727
  _context.next = 17;
717
728
  break;
718
729
  }
719
- if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND')) {
730
+ if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND')) {
720
731
  _context.next = 15;
721
732
  break;
722
733
  }
@@ -724,7 +735,7 @@ function _loadScriptFile() {
724
735
  withExtension: true
725
736
  })));
726
737
  case 15:
727
- if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME')) {
738
+ if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || _context.t0.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
728
739
  _context.next = 17;
729
740
  break;
730
741
  }
@@ -778,12 +789,26 @@ function _loadScriptFileExport() {
778
789
  return _loadScriptFileExport.apply(this, arguments);
779
790
  }
780
791
 
781
- function loadScriptFileSync(data) {
782
- var filePath = isLocatorInfo(data) ? buildLoaderFilePath(data) : data;
792
+ function loadScriptFileSync(data, options) {
793
+ var locatorInfo;
794
+ if (typeof data === 'string') {
795
+ locatorInfo = pathToLocatorInfo(data);
796
+ } else {
797
+ locatorInfo = data;
798
+ }
799
+ options = options || {};
800
+ var filePath = buildLoaderFilePath(locatorInfo, options.withExtension);
783
801
  try {
784
802
  // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require
785
803
  return require(filePath);
786
804
  } catch (e) {
805
+ if (isObject(e) && hasOwnProperty(e, 'code')) {
806
+ if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') {
807
+ return loadScriptFileSync(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
808
+ withExtension: true
809
+ }));
810
+ }
811
+ }
787
812
  return handleFileLoadError(e);
788
813
  }
789
814
  }
@@ -893,4 +918,4 @@ function loadFileSync(input) {
893
918
  return loadScriptFileSync(info);
894
919
  }
895
920
 
896
- export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, isLocatorInfo, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, pathToLocatorInfo, removeFileNameExtension, toArray };
921
+ export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, isLocatorInfo, isObject, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, pathToLocatorInfo, removeFileNameExtension, toArray };
@@ -1,9 +1,4 @@
1
1
  import { LocatorInfo } from '../../../locator';
2
- import { LoaderFilterFn, ScriptFileExportItem } from './type';
3
- type ScriptFileLoadOptions = {
4
- withExtension?: boolean;
5
- withFilePrefix?: boolean;
6
- };
2
+ import { LoaderFilterFn, ScriptFileExportItem, ScriptFileLoadOptions } from './type';
7
3
  export declare function loadScriptFile(data: LocatorInfo | string, options?: ScriptFileLoadOptions): Promise<unknown | undefined>;
8
4
  export declare function loadScriptFileExport(data: LocatorInfo | string, filterFn?: LoaderFilterFn): Promise<ScriptFileExportItem | undefined>;
9
- export {};
@@ -1,4 +1,4 @@
1
- import { LoaderFilterFn, ScriptFileExportItem } from './type';
1
+ import { LoaderFilterFn, ScriptFileExportItem, ScriptFileLoadOptions } from './type';
2
2
  import { LocatorInfo } from '../../../locator';
3
- export declare function loadScriptFileSync(data: LocatorInfo | string): unknown | undefined;
3
+ export declare function loadScriptFileSync(data: LocatorInfo | string, options?: ScriptFileLoadOptions): unknown | undefined;
4
4
  export declare function loadScriptFileExportSync(data: LocatorInfo | string, filterFn?: LoaderFilterFn): ScriptFileExportItem | undefined;
@@ -3,3 +3,7 @@ export type ScriptFileExportItem = {
3
3
  key: string;
4
4
  value: unknown;
5
5
  };
6
+ export type ScriptFileLoadOptions = {
7
+ withExtension?: boolean;
8
+ withFilePrefix?: boolean;
9
+ };
@@ -1,4 +1,5 @@
1
1
  export * from './error';
2
2
  export * from './file-name';
3
3
  export * from './has-own-property';
4
+ export * from './object';
4
5
  export * from './to-array';
@@ -0,0 +1 @@
1
+ export declare function isObject(item: unknown): item is Record<string, any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locter",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "A library to locate a file by criteria and load it",
5
5
  "author": {
6
6
  "name": "Peter Placzek",
@@ -58,7 +58,7 @@
58
58
  "@types/is-file-esm": "^1.0.0",
59
59
  "@types/jest": "^27.5.0",
60
60
  "@types/node": "^18.11.9",
61
- "@types/yargs": "^17.0.14",
61
+ "@types/yargs": "^17.0.15",
62
62
  "coveralls": "^3.1.0",
63
63
  "cross-env": "^7.0.3",
64
64
  "eslint": "^8.28.0",