locter 0.5.3 → 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,8 +729,8 @@ 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
736
  if (!(!options.withExtension && (_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND'))) {
@@ -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
  }
@@ -805,12 +815,17 @@ function loadScriptFileSync(data, options) {
805
815
  return require(filePath);
806
816
  } catch (e) {
807
817
  /* istanbul ignore next */
808
- if (isObject(e) && hasOwnProperty(e, 'code')) {
818
+ if (isObject(e) && hasStringProperty(e, 'code')) {
809
819
  if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
810
820
  return loadScriptFileSync(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
811
821
  withExtension: true
812
822
  }));
813
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
+ });
814
829
  }
815
830
  return handleFileLoadError(e);
816
831
  }
@@ -832,13 +847,17 @@ function loadJsonFile(_x) {
832
847
  return _loadJsonFile.apply(this, arguments);
833
848
  }
834
849
  function _loadJsonFile() {
835
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
850
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
836
851
  var filePath, file;
837
852
  return _regeneratorRuntime().wrap(function _callee$(_context) {
838
853
  while (1) {
839
854
  switch (_context.prev = _context.next) {
840
855
  case 0:
841
- 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
+ }
842
861
  _context.prev = 1;
843
862
  _context.next = 4;
844
863
  return fs.promises.readFile(filePath);
@@ -858,8 +877,13 @@ function _loadJsonFile() {
858
877
  }));
859
878
  return _loadJsonFile.apply(this, arguments);
860
879
  }
861
- function loadJsonFileSync(info) {
862
- 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
+ }
863
887
  try {
864
888
  var file = fs.readFileSync(filePath);
865
889
  return JSON.parse(file.toString('utf-8'));
@@ -926,6 +950,7 @@ exports.buildLocatorOptions = buildLocatorOptions;
926
950
  exports.getExportItem = getExportItem;
927
951
  exports.handleFileLoadError = handleFileLoadError;
928
952
  exports.hasOwnProperty = hasOwnProperty;
953
+ exports.hasStringProperty = hasStringProperty;
929
954
  exports.isLocatorInfo = isLocatorInfo;
930
955
  exports.isObject = isObject;
931
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,8 +727,8 @@ 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
734
  if (!(!options.withExtension && (_context.t0.code === 'ERR_MODULE_NOT_FOUND' || _context.t0.code === 'MODULE_NOT_FOUND'))) {
@@ -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
  }
@@ -803,12 +813,17 @@ function loadScriptFileSync(data, options) {
803
813
  return require(filePath);
804
814
  } catch (e) {
805
815
  /* istanbul ignore next */
806
- if (isObject(e) && hasOwnProperty(e, 'code')) {
816
+ if (isObject(e) && hasStringProperty(e, 'code')) {
807
817
  if (!options.withExtension && (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND')) {
808
818
  return loadScriptFileSync(locatorInfo, _objectSpread2(_objectSpread2({}, options), {}, {
809
819
  withExtension: true
810
820
  }));
811
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
+ });
812
827
  }
813
828
  return handleFileLoadError(e);
814
829
  }
@@ -830,13 +845,17 @@ function loadJsonFile(_x) {
830
845
  return _loadJsonFile.apply(this, arguments);
831
846
  }
832
847
  function _loadJsonFile() {
833
- _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(info) {
848
+ _loadJsonFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
834
849
  var filePath, file;
835
850
  return _regeneratorRuntime().wrap(function _callee$(_context) {
836
851
  while (1) {
837
852
  switch (_context.prev = _context.next) {
838
853
  case 0:
839
- 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
+ }
840
859
  _context.prev = 1;
841
860
  _context.next = 4;
842
861
  return fs.promises.readFile(filePath);
@@ -856,8 +875,13 @@ function _loadJsonFile() {
856
875
  }));
857
876
  return _loadJsonFile.apply(this, arguments);
858
877
  }
859
- function loadJsonFileSync(info) {
860
- 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
+ }
861
885
  try {
862
886
  var file = fs.readFileSync(filePath);
863
887
  return JSON.parse(file.toString('utf-8'));
@@ -919,4 +943,4 @@ function loadFileSync(input) {
919
943
  return loadScriptFileSync(info);
920
944
  }
921
945
 
922
- 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.3",
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>;