UNPKG

6.14 kBJavaScriptView Raw
1"use strict";
2var _a;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.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;
5const module_1 = require("module");
6const ynModule = require("yn");
7const path_1 = require("path");
8/** @internal */
9exports.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 */
15function yn(value) {
16 var _a;
17 return (_a = ynModule(value)) !== null && _a !== void 0 ? _a : undefined;
18}
19exports.yn = yn;
20/**
21 * Like `Object.assign`, but ignores `undefined` properties.
22 *
23 * @internal
24 */
25function 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}
35exports.assign = assign;
36/**
37 * Split a string array of values
38 * and remove empty strings from the resulting array.
39 * @internal
40 */
41function split(value) {
42 return typeof value === 'string'
43 ? value.split(/ *, */g).filter((v) => v !== '')
44 : undefined;
45}
46exports.split = split;
47/**
48 * Parse a string as JSON.
49 * @internal
50 */
51function parse(value) {
52 return typeof value === 'string' ? JSON.parse(value) : undefined;
53}
54exports.parse = parse;
55const directorySeparator = '/';
56const backslashRegExp = /\\/g;
57/**
58 * Replace backslashes with forward slashes.
59 * @internal
60 */
61function normalizeSlashes(value) {
62 return value.replace(backslashRegExp, directorySeparator);
63}
64exports.normalizeSlashes = normalizeSlashes;
65/**
66 * Safe `hasOwnProperty`
67 * @internal
68 */
69function hasOwnProperty(object, property) {
70 return Object.prototype.hasOwnProperty.call(object, property);
71}
72exports.hasOwnProperty = hasOwnProperty;
73/**
74 * Cached fs operation wrapper.
75 */
76function 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}
87exports.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 */
93function 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}
107exports.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 */
115function createProjectLocalResolveHelper(localDirectory) {
116 return function projectLocalResolveHelper(specifier, fallbackToTsNodeRelative) {
117 return require.resolve(specifier, {
118 paths: fallbackToTsNodeRelative
119 ? [localDirectory, __dirname]
120 : [localDirectory],
121 });
122 };
123}
124exports.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 */
130function 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}
139exports.getBasePathForProjectLocalDependencyResolution = getBasePathForProjectLocalDependencyResolution;
140/** @internal */
141function 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}
153exports.once = once;
154/** @internal */
155function 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}
174exports.versionGteLt = versionGteLt;
175//# sourceMappingURL=util.js.map
\No newline at end of file