locter 0.5.2 → 0.6.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,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var ebec = require('ebec');
3
4
  var url = require('url');
4
5
  var glob = require('glob');
5
6
  var util = require('util');
@@ -435,6 +436,9 @@ function removeFileNameExtension(input, extensions) {
435
436
  function hasOwnProperty(obj, prop) {
436
437
  return Object.prototype.hasOwnProperty.call(obj, prop);
437
438
  }
439
+ function hasStringProperty(obj, prop) {
440
+ return hasOwnProperty(obj, prop) && typeof obj[prop] === 'string';
441
+ }
438
442
 
439
443
  /*
440
444
  * Copyright (c) 2022.
@@ -725,11 +729,11 @@ function _loadScriptFile() {
725
729
  case 10:
726
730
  _context.prev = 10;
727
731
  _context.t0 = _context["catch"](4);
728
- if (!(isObject(_context.t0) && hasOwnProperty(_context.t0, 'code'))) {
729
- _context.next = 17;
732
+ if (!(isObject(_context.t0) && hasStringProperty(_context.t0, 'code'))) {
733
+ _context.next = 18;
730
734
  break;
731
735
  }
732
- if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND')) {
736
+ if (!(!options.withExtension && (_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND'))) {
733
737
  _context.next = 15;
734
738
  break;
735
739
  }
@@ -737,7 +741,7 @@ function _loadScriptFile() {
737
741
  withExtension: true
738
742
  })));
739
743
  case 15:
740
- if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || _context.t0.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
744
+ if (!(!options.withFilePrefix && (_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || _context.t0.code === 'UNSUPPORTED_ESM_URL_SCHEME'))) {
741
745
  _context.next = 17;
742
746
  break;
743
747
  }
@@ -745,8 +749,14 @@ function _loadScriptFile() {
745
749
  withFilePrefix: true
746
750
  })));
747
751
  case 17:
748
- return _context.abrupt("return", handleFileLoadError(_context.t0));
752
+ throw new ebec.BaseError({
753
+ code: _context.t0.code,
754
+ message: hasStringProperty(_context.t0, 'message') ? _context.t0.message : undefined,
755
+ stack: hasStringProperty(_context.t0, 'stack') ? _context.t0.stack : undefined
756
+ });
749
757
  case 18:
758
+ return _context.abrupt("return", handleFileLoadError(_context.t0));
759
+ case 19:
750
760
  case "end":
751
761
  return _context.stop();
752
762
  }
@@ -804,12 +814,18 @@ function loadScriptFileSync(data, options) {
804
814
  // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require
805
815
  return require(filePath);
806
816
  } catch (e) {
807
- if (isObject(e) && hasOwnProperty(e, 'code')) {
808
- if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') {
817
+ /* istanbul ignore next */
818
+ if (isObject(e) && hasStringProperty(e, 'code')) {
819
+ if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
809
820
  return loadScriptFileSync(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
810
821
  withExtension: true
811
822
  }));
812
823
  }
824
+ throw new ebec.BaseError({
825
+ code: e.code,
826
+ message: hasStringProperty(e, 'message') ? e.message : undefined,
827
+ stack: hasStringProperty(e, 'stack') ? e.stack : undefined
828
+ });
813
829
  }
814
830
  return handleFileLoadError(e);
815
831
  }
@@ -831,13 +847,17 @@ function loadJsonFile(_x) {
831
847
  return _loadJsonFile.apply(this, arguments);
832
848
  }
833
849
  function _loadJsonFile() {
834
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
850
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
835
851
  var filePath, file;
836
852
  return _regeneratorRuntime().wrap(function _callee$(_context) {
837
853
  while (1) {
838
854
  switch (_context.prev = _context.next) {
839
855
  case 0:
840
- filePath = buildLoaderFilePath(info, true);
856
+ if (typeof input === 'string') {
857
+ filePath = path.isAbsolute(input) ? input : path.resolve(process.cwd(), input);
858
+ } else {
859
+ filePath = buildLoaderFilePath(input, true);
860
+ }
841
861
  _context.prev = 1;
842
862
  _context.next = 4;
843
863
  return fs.promises.readFile(filePath);
@@ -857,8 +877,13 @@ function _loadJsonFile() {
857
877
  }));
858
878
  return _loadJsonFile.apply(this, arguments);
859
879
  }
860
- function loadJsonFileSync(info) {
861
- var filePath = buildLoaderFilePath(info, true);
880
+ function loadJsonFileSync(input) {
881
+ var filePath;
882
+ if (typeof input === 'string') {
883
+ filePath = path.isAbsolute(input) ? input : path.resolve(process.cwd(), input);
884
+ } else {
885
+ filePath = buildLoaderFilePath(input, true);
886
+ }
862
887
  try {
863
888
  var file = fs.readFileSync(filePath);
864
889
  return JSON.parse(file.toString('utf-8'));
@@ -925,6 +950,7 @@ exports.buildLocatorOptions = buildLocatorOptions;
925
950
  exports.getExportItem = getExportItem;
926
951
  exports.handleFileLoadError = handleFileLoadError;
927
952
  exports.hasOwnProperty = hasOwnProperty;
953
+ exports.hasStringProperty = hasStringProperty;
928
954
  exports.isLocatorInfo = isLocatorInfo;
929
955
  exports.isObject = isObject;
930
956
  exports.loadFile = loadFile;
package/dist/index.esm.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { BaseError } from 'ebec';
1
2
  import { pathToFileURL } from 'url';
2
3
  import { glob, sync } from 'glob';
3
4
  import { promisify } from 'util';
@@ -433,6 +434,9 @@ function removeFileNameExtension(input, extensions) {
433
434
  function hasOwnProperty(obj, prop) {
434
435
  return Object.prototype.hasOwnProperty.call(obj, prop);
435
436
  }
437
+ function hasStringProperty(obj, prop) {
438
+ return hasOwnProperty(obj, prop) && typeof obj[prop] === 'string';
439
+ }
436
440
 
437
441
  /*
438
442
  * Copyright (c) 2022.
@@ -723,11 +727,11 @@ function _loadScriptFile() {
723
727
  case 10:
724
728
  _context.prev = 10;
725
729
  _context.t0 = _context["catch"](4);
726
- if (!(isObject(_context.t0) && hasOwnProperty(_context.t0, 'code'))) {
727
- _context.next = 17;
730
+ if (!(isObject(_context.t0) && hasStringProperty(_context.t0, 'code'))) {
731
+ _context.next = 18;
728
732
  break;
729
733
  }
730
- if (!(_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND')) {
734
+ if (!(!options.withExtension && (_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND'))) {
731
735
  _context.next = 15;
732
736
  break;
733
737
  }
@@ -735,7 +739,7 @@ function _loadScriptFile() {
735
739
  withExtension: true
736
740
  })));
737
741
  case 15:
738
- if (!(_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || _context.t0.code === 'UNSUPPORTED_ESM_URL_SCHEME')) {
742
+ if (!(!options.withFilePrefix && (_context.t0.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || _context.t0.code === 'UNSUPPORTED_ESM_URL_SCHEME'))) {
739
743
  _context.next = 17;
740
744
  break;
741
745
  }
@@ -743,8 +747,14 @@ function _loadScriptFile() {
743
747
  withFilePrefix: true
744
748
  })));
745
749
  case 17:
746
- return _context.abrupt("return", handleFileLoadError(_context.t0));
750
+ throw new BaseError({
751
+ code: _context.t0.code,
752
+ message: hasStringProperty(_context.t0, 'message') ? _context.t0.message : undefined,
753
+ stack: hasStringProperty(_context.t0, 'stack') ? _context.t0.stack : undefined
754
+ });
747
755
  case 18:
756
+ return _context.abrupt("return", handleFileLoadError(_context.t0));
757
+ case 19:
748
758
  case "end":
749
759
  return _context.stop();
750
760
  }
@@ -802,12 +812,18 @@ function loadScriptFileSync(data, options) {
802
812
  // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require
803
813
  return require(filePath);
804
814
  } catch (e) {
805
- if (isObject(e) && hasOwnProperty(e, 'code')) {
806
- if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') {
815
+ /* istanbul ignore next */
816
+ if (isObject(e) && hasStringProperty(e, 'code')) {
817
+ if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
807
818
  return loadScriptFileSync(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
808
819
  withExtension: true
809
820
  }));
810
821
  }
822
+ throw new BaseError({
823
+ code: e.code,
824
+ message: hasStringProperty(e, 'message') ? e.message : undefined,
825
+ stack: hasStringProperty(e, 'stack') ? e.stack : undefined
826
+ });
811
827
  }
812
828
  return handleFileLoadError(e);
813
829
  }
@@ -829,13 +845,17 @@ function loadJsonFile(_x) {
829
845
  return _loadJsonFile.apply(this, arguments);
830
846
  }
831
847
  function _loadJsonFile() {
832
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
848
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
833
849
  var filePath, file;
834
850
  return _regeneratorRuntime().wrap(function _callee$(_context) {
835
851
  while (1) {
836
852
  switch (_context.prev = _context.next) {
837
853
  case 0:
838
- filePath = buildLoaderFilePath(info, true);
854
+ if (typeof input === 'string') {
855
+ filePath = path.isAbsolute(input) ? input : path.resolve(process.cwd(), input);
856
+ } else {
857
+ filePath = buildLoaderFilePath(input, true);
858
+ }
839
859
  _context.prev = 1;
840
860
  _context.next = 4;
841
861
  return fs.promises.readFile(filePath);
@@ -855,8 +875,13 @@ function _loadJsonFile() {
855
875
  }));
856
876
  return _loadJsonFile.apply(this, arguments);
857
877
  }
858
- function loadJsonFileSync(info) {
859
- var filePath = buildLoaderFilePath(info, true);
878
+ function loadJsonFileSync(input) {
879
+ var filePath;
880
+ if (typeof input === 'string') {
881
+ filePath = path.isAbsolute(input) ? input : path.resolve(process.cwd(), input);
882
+ } else {
883
+ filePath = buildLoaderFilePath(input, true);
884
+ }
860
885
  try {
861
886
  var file = fs.readFileSync(filePath);
862
887
  return JSON.parse(file.toString('utf-8'));
@@ -918,4 +943,4 @@ function loadFileSync(input) {
918
943
  return loadScriptFileSync(info);
919
944
  }
920
945
 
921
- export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, isLocatorInfo, isObject, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, pathToLocatorInfo, removeFileNameExtension, toArray };
946
+ export { buildLoaderFilePath, buildLocatorOptions, getExportItem, handleFileLoadError, hasOwnProperty, hasStringProperty, isLocatorInfo, isObject, loadFile, loadFileSync, loadJsonFile, loadJsonFileSync, loadScriptFile, loadScriptFileExport, loadScriptFileExportSync, loadScriptFileSync, locateFile, locateFileSync, locateFiles, locateFilesSync, pathToLocatorInfo, removeFileNameExtension, toArray };
@@ -1,3 +1,3 @@
1
1
  import { LocatorInfo } from '../../locator';
2
- export declare function loadJsonFile(info: LocatorInfo): Promise<unknown | undefined>;
3
- export declare function loadJsonFileSync(info: LocatorInfo): unknown | undefined;
2
+ export declare function loadJsonFile(input: LocatorInfo | string): Promise<unknown | undefined>;
3
+ export declare function loadJsonFileSync(input: LocatorInfo | string): unknown | undefined;
@@ -0,0 +1,2 @@
1
+ export declare function hasOwnProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
2
+ export declare function hasStringProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, string>;
@@ -1,5 +1,5 @@
1
1
  export * from './error';
2
2
  export * from './file-name';
3
- export * from './has-own-property';
3
+ export * from './has-property';
4
4
  export * from './object';
5
5
  export * from './to-array';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locter",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "A library to locate a file by criteria and load it",
5
5
  "author": {
6
6
  "name": "Peter Placzek",
@@ -57,7 +57,7 @@
57
57
  "@types/glob": "^8.0.0",
58
58
  "@types/is-file-esm": "^1.0.0",
59
59
  "@types/jest": "^27.5.0",
60
- "@types/node": "^18.11.9",
60
+ "@types/node": "^18.11.10",
61
61
  "@types/yargs": "^17.0.15",
62
62
  "coveralls": "^3.1.0",
63
63
  "cross-env": "^7.0.3",
@@ -65,13 +65,14 @@
65
65
  "husky": "^8.0.2",
66
66
  "jest": "^27.5.1",
67
67
  "rimraf": "^3.0.2",
68
- "rollup": "^3.5.0",
68
+ "rollup": "^3.5.1",
69
69
  "semantic-release": "^19.0.5",
70
70
  "ts-jest": "^27.1.4",
71
71
  "ts-node": "^10.9.1",
72
72
  "typescript": "^4.9.3"
73
73
  },
74
74
  "dependencies": {
75
+ "ebec": "^0.1.0",
75
76
  "glob": "^8.0.3"
76
77
  },
77
78
  "config": {
@@ -1 +0,0 @@
1
- export declare function hasOwnProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;