util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.isDirectorySync = exports.isDirectory = exports.removeUndefinedValuesFromObject = exports.getPropertyByPath = exports.emplace = void 0;
  27. const fs_1 = __importStar(require("fs"));
  28. /**
  29. * @internal
  30. */
  31. function emplace(map, key, fn) {
  32. const cached = map.get(key);
  33. if (cached !== undefined) {
  34. return cached;
  35. }
  36. const result = fn();
  37. map.set(key, result);
  38. return result;
  39. }
  40. exports.emplace = emplace;
  41. // Resolves property names or property paths defined with period-delimited
  42. // strings or arrays of strings. Property names that are found on the source
  43. // object are used directly (even if they include a period).
  44. // Nested property names that include periods, within a path, are only
  45. // understood in array paths.
  46. /**
  47. * @internal
  48. */
  49. function getPropertyByPath(source, path) {
  50. if (typeof path === 'string' &&
  51. Object.prototype.hasOwnProperty.call(source, path)) {
  52. return source[path];
  53. }
  54. const parsedPath = typeof path === 'string' ? path.split('.') : path;
  55. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  56. return parsedPath.reduce((previous, key) => {
  57. if (previous === undefined) {
  58. return previous;
  59. }
  60. return previous[key];
  61. }, source);
  62. }
  63. exports.getPropertyByPath = getPropertyByPath;
  64. /** @internal */
  65. function removeUndefinedValuesFromObject(options) {
  66. return Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined));
  67. }
  68. exports.removeUndefinedValuesFromObject = removeUndefinedValuesFromObject;
  69. /** @internal */
  70. /* istanbul ignore next -- @preserve */
  71. async function isDirectory(path) {
  72. try {
  73. const stat = await fs_1.promises.stat(path);
  74. return stat.isDirectory();
  75. }
  76. catch (e) {
  77. if (e.code === 'ENOENT') {
  78. return false;
  79. }
  80. throw e;
  81. }
  82. }
  83. exports.isDirectory = isDirectory;
  84. /** @internal */
  85. /* istanbul ignore next -- @preserve */
  86. function isDirectorySync(path) {
  87. try {
  88. const stat = fs_1.default.statSync(path);
  89. return stat.isDirectory();
  90. }
  91. catch (e) {
  92. if (e.code === 'ENOENT') {
  93. return false;
  94. }
  95. throw e;
  96. }
  97. }
  98. exports.isDirectorySync = isDirectorySync;
  99. //# sourceMappingURL=util.js.map