util.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. var _a;
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.versionGteLt = exports.once = exports.getBasePathForProjectLocalDependencyResolution = exports.createProjectLocalResolveHelper = exports.attemptRequireWithV8CompileCache = exports.cachedLookup = exports.hasOwnProperty = exports.normalizeSlashes = exports.parse = exports.split = exports.assign = exports.yn = exports.createRequire = void 0;
  5. const module_1 = require("module");
  6. const ynModule = require("yn");
  7. const path_1 = require("path");
  8. /** @internal */
  9. exports.createRequire = (_a = module_1.createRequire !== null && module_1.createRequire !== void 0 ? module_1.createRequire : module_1.createRequireFromPath) !== null && _a !== void 0 ? _a : require('create-require');
  10. /**
  11. * Wrapper around yn module that returns `undefined` instead of `null`.
  12. * This is implemented by yn v4, but we're staying on v3 to avoid v4's node 10 requirement.
  13. * @internal
  14. */
  15. function yn(value) {
  16. var _a;
  17. return (_a = ynModule(value)) !== null && _a !== void 0 ? _a : undefined;
  18. }
  19. exports.yn = yn;
  20. /**
  21. * Like `Object.assign`, but ignores `undefined` properties.
  22. *
  23. * @internal
  24. */
  25. function assign(initialValue, ...sources) {
  26. for (const source of sources) {
  27. for (const key of Object.keys(source)) {
  28. const value = source[key];
  29. if (value !== undefined)
  30. initialValue[key] = value;
  31. }
  32. }
  33. return initialValue;
  34. }
  35. exports.assign = assign;
  36. /**
  37. * Split a string array of values
  38. * and remove empty strings from the resulting array.
  39. * @internal
  40. */
  41. function split(value) {
  42. return typeof value === 'string'
  43. ? value.split(/ *, */g).filter((v) => v !== '')
  44. : undefined;
  45. }
  46. exports.split = split;
  47. /**
  48. * Parse a string as JSON.
  49. * @internal
  50. */
  51. function parse(value) {
  52. return typeof value === 'string' ? JSON.parse(value) : undefined;
  53. }
  54. exports.parse = parse;
  55. const directorySeparator = '/';
  56. const backslashRegExp = /\\/g;
  57. /**
  58. * Replace backslashes with forward slashes.
  59. * @internal
  60. */
  61. function normalizeSlashes(value) {
  62. return value.replace(backslashRegExp, directorySeparator);
  63. }
  64. exports.normalizeSlashes = normalizeSlashes;
  65. /**
  66. * Safe `hasOwnProperty`
  67. * @internal
  68. */
  69. function hasOwnProperty(object, property) {
  70. return Object.prototype.hasOwnProperty.call(object, property);
  71. }
  72. exports.hasOwnProperty = hasOwnProperty;
  73. /**
  74. * Cached fs operation wrapper.
  75. */
  76. function cachedLookup(fn) {
  77. const cache = new Map();
  78. return (arg) => {
  79. if (!cache.has(arg)) {
  80. const v = fn(arg);
  81. cache.set(arg, v);
  82. return v;
  83. }
  84. return cache.get(arg);
  85. };
  86. }
  87. exports.cachedLookup = cachedLookup;
  88. /**
  89. * @internal
  90. * Require something with v8-compile-cache, which should make subsequent requires faster.
  91. * Do lots of error-handling so that, worst case, we require without the cache, and users are not blocked.
  92. */
  93. function attemptRequireWithV8CompileCache(requireFn, specifier) {
  94. try {
  95. const v8CC = require('v8-compile-cache-lib').install();
  96. try {
  97. return requireFn(specifier);
  98. }
  99. finally {
  100. v8CC === null || v8CC === void 0 ? void 0 : v8CC.uninstall();
  101. }
  102. }
  103. catch (e) {
  104. return requireFn(specifier);
  105. }
  106. }
  107. exports.attemptRequireWithV8CompileCache = attemptRequireWithV8CompileCache;
  108. /**
  109. * Helper to discover dependencies relative to a user's project, optionally
  110. * falling back to relative to ts-node. This supports global installations of
  111. * ts-node, for example where someone does `#!/usr/bin/env -S ts-node --swc` and
  112. * we need to fallback to a global install of @swc/core
  113. * @internal
  114. */
  115. function createProjectLocalResolveHelper(localDirectory) {
  116. return function projectLocalResolveHelper(specifier, fallbackToTsNodeRelative) {
  117. return require.resolve(specifier, {
  118. paths: fallbackToTsNodeRelative
  119. ? [localDirectory, __dirname]
  120. : [localDirectory],
  121. });
  122. };
  123. }
  124. exports.createProjectLocalResolveHelper = createProjectLocalResolveHelper;
  125. /**
  126. * Used as a reminder of all the factors we must consider when finding project-local dependencies and when a config file
  127. * on disk may or may not exist.
  128. * @internal
  129. */
  130. function getBasePathForProjectLocalDependencyResolution(configFilePath, projectSearchDirOption, projectOption, cwdOption) {
  131. var _a;
  132. if (configFilePath != null)
  133. return (0, path_1.dirname)(configFilePath);
  134. return (_a = projectSearchDirOption !== null && projectSearchDirOption !== void 0 ? projectSearchDirOption : projectOption) !== null && _a !== void 0 ? _a : cwdOption;
  135. // TODO technically breaks if projectOption is path to a file, not a directory,
  136. // and we attempt to resolve relative specifiers. By the time we resolve relative specifiers,
  137. // should have configFilePath, so not reach this codepath.
  138. }
  139. exports.getBasePathForProjectLocalDependencyResolution = getBasePathForProjectLocalDependencyResolution;
  140. /** @internal */
  141. function once(fn) {
  142. let value;
  143. let ran = false;
  144. function onceFn(...args) {
  145. if (ran)
  146. return value;
  147. value = fn(...args);
  148. ran = true;
  149. return value;
  150. }
  151. return onceFn;
  152. }
  153. exports.once = once;
  154. /** @internal */
  155. function versionGteLt(version, gteRequirement, ltRequirement) {
  156. const [major, minor, patch, extra] = parse(version);
  157. const [gteMajor, gteMinor, gtePatch] = parse(gteRequirement);
  158. const isGte = major > gteMajor ||
  159. (major === gteMajor &&
  160. (minor > gteMinor || (minor === gteMinor && patch >= gtePatch)));
  161. let isLt = true;
  162. if (ltRequirement) {
  163. const [ltMajor, ltMinor, ltPatch] = parse(ltRequirement);
  164. isLt =
  165. major < ltMajor ||
  166. (major === ltMajor &&
  167. (minor < ltMinor || (minor === ltMinor && patch < ltPatch)));
  168. }
  169. return isGte && isLt;
  170. function parse(requirement) {
  171. return requirement.split(/[\.-]/).map((s) => parseInt(s, 10));
  172. }
  173. }
  174. exports.versionGteLt = versionGteLt;
  175. //# sourceMappingURL=util.js.map