@stryke/fs 0.33.31 → 0.33.33
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/CHANGELOG.md +18 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/buffer.cjs +60 -1
- package/dist/buffer.mjs +56 -1
- package/dist/buffer.mjs.map +1 -1
- package/dist/chmod-x.cjs +93 -1
- package/dist/chmod-x.d.cts.map +1 -1
- package/dist/chmod-x.mjs +87 -1
- package/dist/chmod-x.mjs.map +1 -1
- package/dist/command-exists.cjs +102 -1
- package/dist/command-exists.mjs +100 -1
- package/dist/command-exists.mjs.map +1 -1
- package/dist/compress.cjs +29 -1
- package/dist/compress.mjs +28 -1
- package/dist/compress.mjs.map +1 -1
- package/dist/constants.mjs +1 -1
- package/dist/convert/src/array-buffer-to-string.cjs +19 -1
- package/dist/convert/src/array-buffer-to-string.mjs +18 -1
- package/dist/convert/src/array-buffer-to-string.mjs.map +1 -1
- package/dist/convert/src/to-array.cjs +15 -1
- package/dist/convert/src/to-array.mjs +14 -1
- package/dist/convert/src/to-array.mjs.map +1 -1
- package/dist/convert/src/uint8-array-to-string.cjs +15 -1
- package/dist/convert/src/uint8-array-to-string.mjs +15 -1
- package/dist/convert/src/uint8-array-to-string.mjs.map +1 -1
- package/dist/copy-file.cjs +87 -1
- package/dist/copy-file.mjs +83 -1
- package/dist/copy-file.mjs.map +1 -1
- package/dist/exists.cjs +27 -1
- package/dist/exists.mjs +25 -1
- package/dist/exists.mjs.map +1 -1
- package/dist/get-parent-path.cjs +35 -1
- package/dist/get-parent-path.mjs +34 -1
- package/dist/get-parent-path.mjs.map +1 -1
- package/dist/get-workspace-root.cjs +158 -1
- package/dist/get-workspace-root.mjs +150 -1
- package/dist/get-workspace-root.mjs.map +1 -1
- package/dist/helpers/src/get-unique.cjs +14 -1
- package/dist/helpers/src/get-unique.mjs +13 -1
- package/dist/helpers/src/get-unique.mjs.map +1 -1
- package/dist/helpers.cjs +83 -1
- package/dist/helpers.mjs +77 -1
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.cjs +122 -1
- package/dist/index.mjs +25 -1
- package/dist/install.cjs +35 -1
- package/dist/install.mjs +33 -1
- package/dist/install.mjs.map +1 -1
- package/dist/is-file.cjs +51 -1
- package/dist/is-file.mjs +47 -1
- package/dist/is-file.mjs.map +1 -1
- package/dist/json/src/storm-json.cjs +117 -1
- package/dist/json/src/storm-json.mjs +115 -1
- package/dist/json/src/storm-json.mjs.map +1 -1
- package/dist/json/src/utils/code-frames.cjs +77 -3
- package/dist/json/src/utils/code-frames.mjs +76 -3
- package/dist/json/src/utils/code-frames.mjs.map +1 -1
- package/dist/json/src/utils/parse-error.cjs +34 -1
- package/dist/json/src/utils/parse-error.mjs +33 -1
- package/dist/json/src/utils/parse-error.mjs.map +1 -1
- package/dist/json/src/utils/parse.cjs +45 -1
- package/dist/json/src/utils/parse.mjs +45 -1
- package/dist/json/src/utils/parse.mjs.map +1 -1
- package/dist/json/src/utils/stringify.cjs +67 -1
- package/dist/json/src/utils/stringify.mjs +67 -1
- package/dist/json/src/utils/stringify.mjs.map +1 -1
- package/dist/json/src/utils/strip-comments.cjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs.map +1 -1
- package/dist/json.cjs +74 -1
- package/dist/json.mjs +71 -1
- package/dist/json.mjs.map +1 -1
- package/dist/list-files.cjs +86 -1
- package/dist/list-files.mjs +79 -1
- package/dist/list-files.mjs.map +1 -1
- package/dist/package-fns.cjs +148 -1
- package/dist/package-fns.mjs +141 -1
- package/dist/package-fns.mjs.map +1 -1
- package/dist/path/src/append.cjs +57 -1
- package/dist/path/src/append.mjs +56 -1
- package/dist/path/src/append.mjs.map +1 -1
- package/dist/path/src/correct-path.cjs +138 -1
- package/dist/path/src/correct-path.mjs +134 -1
- package/dist/path/src/correct-path.mjs.map +1 -1
- package/dist/path/src/cwd.cjs +17 -1
- package/dist/path/src/cwd.mjs +16 -1
- package/dist/path/src/cwd.mjs.map +1 -1
- package/dist/path/src/file-path-fns.cjs +169 -1
- package/dist/path/src/file-path-fns.mjs +164 -1
- package/dist/path/src/file-path-fns.mjs.map +1 -1
- package/dist/path/src/is-parent-path.cjs +32 -1
- package/dist/path/src/is-parent-path.mjs +32 -1
- package/dist/path/src/is-parent-path.mjs.map +1 -1
- package/dist/path/src/is-root-dir.cjs +14 -1
- package/dist/path/src/is-root-dir.mjs +13 -1
- package/dist/path/src/is-root-dir.mjs.map +1 -1
- package/dist/path/src/is-type.cjs +68 -1
- package/dist/path/src/is-type.mjs +66 -1
- package/dist/path/src/is-type.mjs.map +1 -1
- package/dist/path/src/join-paths.cjs +106 -1
- package/dist/path/src/join-paths.mjs +106 -1
- package/dist/path/src/join-paths.mjs.map +1 -1
- package/dist/path/src/regex.cjs +18 -1
- package/dist/path/src/regex.mjs +11 -1
- package/dist/path/src/regex.mjs.map +1 -1
- package/dist/path/src/replace.cjs +24 -1
- package/dist/path/src/replace.mjs +24 -1
- package/dist/path/src/replace.mjs.map +1 -1
- package/dist/path/src/resolve-parent-path.cjs +18 -1
- package/dist/path/src/resolve-parent-path.mjs +18 -1
- package/dist/path/src/resolve-parent-path.mjs.map +1 -1
- package/dist/path/src/slash.cjs +15 -1
- package/dist/path/src/slash.mjs +14 -1
- package/dist/path/src/slash.mjs.map +1 -1
- package/dist/read-file.cjs +47 -1
- package/dist/read-file.mjs +43 -1
- package/dist/read-file.mjs.map +1 -1
- package/dist/registry.cjs +142 -1
- package/dist/registry.mjs +137 -1
- package/dist/registry.mjs.map +1 -1
- package/dist/remove-file.cjs +27 -1
- package/dist/remove-file.mjs +25 -1
- package/dist/remove-file.mjs.map +1 -1
- package/dist/resolve.cjs +228 -3
- package/dist/resolve.mjs +217 -3
- package/dist/resolve.mjs.map +1 -1
- package/dist/semver-fns.cjs +129 -1
- package/dist/semver-fns.mjs +119 -1
- package/dist/semver-fns.mjs.map +1 -1
- package/dist/string-format/src/package.cjs +92 -1
- package/dist/string-format/src/package.mjs +91 -1
- package/dist/string-format/src/package.mjs.map +1 -1
- package/dist/toml.cjs +66 -3
- package/dist/toml.mjs +61 -3
- package/dist/toml.mjs.map +1 -1
- package/dist/tsconfig.cjs +37 -1
- package/dist/tsconfig.mjs +35 -1
- package/dist/tsconfig.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/index.cjs +14 -1
- package/dist/type-checks/src/index.mjs +16 -1
- package/dist/type-checks/src/is-buffer.cjs +12 -1
- package/dist/type-checks/src/is-buffer.mjs +11 -1
- package/dist/type-checks/src/is-buffer.mjs.map +1 -1
- package/dist/type-checks/src/is-collection.cjs +1 -1
- package/dist/type-checks/src/is-collection.mjs +3 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-error.cjs +29 -1
- package/dist/type-checks/src/is-error.mjs +29 -1
- package/dist/type-checks/src/is-error.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-number.cjs +18 -1
- package/dist/type-checks/src/is-number.mjs +17 -1
- package/dist/type-checks/src/is-number.mjs.map +1 -1
- package/dist/type-checks/src/is-object.cjs +19 -1
- package/dist/type-checks/src/is-object.mjs +19 -1
- package/dist/type-checks/src/is-object.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +64 -1
- package/dist/type-checks/src/is-plain-object.mjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-set-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/type-checks/src/type-detect.cjs +15 -1
- package/dist/type-checks/src/type-detect.mjs +16 -1
- package/dist/type-checks/src/type-detect.mjs.map +1 -1
- package/dist/types/src/base.cjs +6 -1
- package/dist/types/src/base.mjs +5 -1
- package/dist/types/src/base.mjs.map +1 -1
- package/dist/write-file.cjs +40 -1
- package/dist/write-file.mjs +38 -1
- package/dist/write-file.mjs.map +1 -1
- package/dist/yaml.cjs +29 -1
- package/dist/yaml.mjs +27 -1
- package/dist/yaml.mjs.map +1 -1
- package/package.json +6 -6
package/dist/resolve.mjs
CHANGED
|
@@ -1,4 +1,218 @@
|
|
|
1
|
-
import{
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { cwd } from "./path/src/cwd.mjs";
|
|
2
|
+
import { isAbsolutePath, isNpmScopedPackage } from "./path/src/is-type.mjs";
|
|
3
|
+
import { joinPaths } from "./path/src/join-paths.mjs";
|
|
4
|
+
import { appendExtension } from "./path/src/append.mjs";
|
|
5
|
+
import { correctPath, toAbsolutePath } from "./path/src/correct-path.mjs";
|
|
6
|
+
import { findFileExtension, findFileName, findFilePath, findFolderName } from "./path/src/file-path-fns.mjs";
|
|
7
|
+
import { existsSync } from "./exists.mjs";
|
|
8
|
+
import { getWorkspaceRoot } from "./get-workspace-root.mjs";
|
|
9
|
+
import { getUnique } from "./helpers/src/get-unique.mjs";
|
|
10
|
+
import { interopDefault, resolvePath, resolvePathSync } from "mlly";
|
|
11
|
+
|
|
12
|
+
//#region src/resolve.ts
|
|
13
|
+
const DEFAULT_EXTENSIONS = [
|
|
14
|
+
"js",
|
|
15
|
+
"jsx",
|
|
16
|
+
"mjs",
|
|
17
|
+
"cjs",
|
|
18
|
+
"ts",
|
|
19
|
+
"tsx",
|
|
20
|
+
"mts",
|
|
21
|
+
"cts",
|
|
22
|
+
"json",
|
|
23
|
+
"jsonc",
|
|
24
|
+
"json5",
|
|
25
|
+
"node"
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* Get the resolution paths based on the provided paths, current working directory, and workspace root.
|
|
29
|
+
*
|
|
30
|
+
* @param paths - An array of paths to include in the resolution.
|
|
31
|
+
* @returns An array of unique, corrected resolution paths.
|
|
32
|
+
*/
|
|
33
|
+
function getResolutionPaths(paths = []) {
|
|
34
|
+
let resolutionPaths = paths;
|
|
35
|
+
if (!resolutionPaths.includes(cwd())) resolutionPaths.push(cwd());
|
|
36
|
+
const workspaceRoot = getWorkspaceRoot();
|
|
37
|
+
if (!resolutionPaths.includes(workspaceRoot)) resolutionPaths.push(workspaceRoot);
|
|
38
|
+
resolutionPaths = getUnique(resolutionPaths.filter(Boolean).map((path) => correctPath(path)).reduce((ret, path, _, arr) => {
|
|
39
|
+
ret.push(path);
|
|
40
|
+
if (!isAbsolutePath(path)) {
|
|
41
|
+
ret.push(toAbsolutePath(path, cwd()));
|
|
42
|
+
ret.push(toAbsolutePath(path, workspaceRoot));
|
|
43
|
+
arr.forEach((existing) => {
|
|
44
|
+
ret.push(toAbsolutePath(path, existing));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return ret;
|
|
48
|
+
}, []));
|
|
49
|
+
return resolutionPaths;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get the node_modules resolution paths based on the provided paths.
|
|
53
|
+
*
|
|
54
|
+
* @param paths - An array of paths to include in the resolution.
|
|
55
|
+
* @returns An array of unique, corrected node_modules resolution paths.
|
|
56
|
+
*/
|
|
57
|
+
function getNodeModulesPaths(paths = []) {
|
|
58
|
+
return getUnique(paths.reduce((ret, path) => {
|
|
59
|
+
if (findFolderName(path) === "node_modules") ret.push(correctPath(path));
|
|
60
|
+
if (existsSync(joinPaths(path, "node_modules"))) ret.push(correctPath(joinPaths(path, "node_modules")));
|
|
61
|
+
return ret;
|
|
62
|
+
}, []));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get all combinations of resolution paths for a given path and options.
|
|
66
|
+
*
|
|
67
|
+
* @param path - The base path to combine with resolution paths.
|
|
68
|
+
* @param options - The options containing resolution paths.
|
|
69
|
+
* @returns An array of unique, corrected resolution paths.
|
|
70
|
+
*/
|
|
71
|
+
function getResolutionCombinations(path, options = {}) {
|
|
72
|
+
let paths = getResolutionPaths(options.paths);
|
|
73
|
+
if (isNpmScopedPackage(path)) paths = getNodeModulesPaths(paths);
|
|
74
|
+
else paths.push(...getNodeModulesPaths(paths));
|
|
75
|
+
const extensions = options.extensions ?? DEFAULT_EXTENSIONS;
|
|
76
|
+
let combinations = paths.map((base) => joinPaths(base, path));
|
|
77
|
+
if (findFileName(path, { withExtension: false }) !== "index") combinations = combinations.reduce((ret, combination) => {
|
|
78
|
+
ret.push(combination);
|
|
79
|
+
ret.push(joinPaths(combination, "index"));
|
|
80
|
+
return ret;
|
|
81
|
+
}, []);
|
|
82
|
+
if (!findFileExtension(path)) combinations = combinations.reduce((ret, combination) => {
|
|
83
|
+
ret.push(combination);
|
|
84
|
+
extensions.forEach((ext) => {
|
|
85
|
+
ret.push(appendExtension(combination, ext));
|
|
86
|
+
});
|
|
87
|
+
return ret;
|
|
88
|
+
}, []);
|
|
89
|
+
return getUnique(combinations.filter(Boolean)).map((p) => correctPath(p));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Resolve the path to a specified module
|
|
93
|
+
*
|
|
94
|
+
* @param path - The path to the module
|
|
95
|
+
* @param options - The options to use when resolving the module
|
|
96
|
+
* @returns A promise for the path to the module
|
|
97
|
+
*/
|
|
98
|
+
async function resolve(path, options = {}) {
|
|
99
|
+
let paths = getResolutionPaths(options.paths);
|
|
100
|
+
if (isNpmScopedPackage(path)) paths = getNodeModulesPaths(paths);
|
|
101
|
+
else paths.push(...getNodeModulesPaths(paths));
|
|
102
|
+
let result;
|
|
103
|
+
let error;
|
|
104
|
+
try {
|
|
105
|
+
result = await resolvePath(path, {
|
|
106
|
+
url: paths,
|
|
107
|
+
extensions: options.extensions ?? DEFAULT_EXTENSIONS,
|
|
108
|
+
conditions: options.conditions
|
|
109
|
+
});
|
|
110
|
+
} catch (err) {
|
|
111
|
+
error = err;
|
|
112
|
+
}
|
|
113
|
+
if (!result) throw new Error(`Unable to resolve module "${path}". The following import paths were tried: \n${paths.join("\n")}`, { cause: error });
|
|
114
|
+
return correctPath(result);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Resolve the path to a specified module
|
|
118
|
+
*
|
|
119
|
+
* @param path - The path to the module
|
|
120
|
+
* @param options - The options to use when resolving the module
|
|
121
|
+
* @returns The path to the module or undefined
|
|
122
|
+
*/
|
|
123
|
+
function resolveSync(path, options = {}) {
|
|
124
|
+
let paths = getResolutionPaths(options.paths);
|
|
125
|
+
if (isNpmScopedPackage(path)) paths = getNodeModulesPaths(paths);
|
|
126
|
+
else paths.push(...getNodeModulesPaths(paths));
|
|
127
|
+
let result;
|
|
128
|
+
let error;
|
|
129
|
+
try {
|
|
130
|
+
result = resolvePathSync(path, {
|
|
131
|
+
url: paths,
|
|
132
|
+
extensions: options.extensions ?? DEFAULT_EXTENSIONS,
|
|
133
|
+
conditions: options.conditions
|
|
134
|
+
});
|
|
135
|
+
} catch (err) {
|
|
136
|
+
error = err;
|
|
137
|
+
}
|
|
138
|
+
if (!result) throw new Error(`Unable to resolve module "${path}". The following import paths were tried: \n${paths.join("\n")}`, { cause: error });
|
|
139
|
+
return correctPath(result);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Resolve the path to a specified module with error handling
|
|
143
|
+
*
|
|
144
|
+
* @param name - The name of the module
|
|
145
|
+
* @param options - The options to use when resolving the module
|
|
146
|
+
* @returns A promise for the path to the module
|
|
147
|
+
*/
|
|
148
|
+
async function resolveSafe(name, options = {}) {
|
|
149
|
+
try {
|
|
150
|
+
return await resolve(name, options);
|
|
151
|
+
} catch {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Resolve the path to a specified module with error handling
|
|
157
|
+
*
|
|
158
|
+
* @param name - The name of the module
|
|
159
|
+
* @param options - The options to use when resolving the module
|
|
160
|
+
* @returns The path to the module or undefined
|
|
161
|
+
*/
|
|
162
|
+
function resolveSafeSync(name, options = {}) {
|
|
163
|
+
try {
|
|
164
|
+
return resolveSync(name, options);
|
|
165
|
+
} catch {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Import a module from a specified path
|
|
171
|
+
*
|
|
172
|
+
* @param path - The path to the module
|
|
173
|
+
* @returns The module
|
|
174
|
+
*/
|
|
175
|
+
async function importModule(path) {
|
|
176
|
+
const i = await import(path);
|
|
177
|
+
if (i) return interopDefault(i);
|
|
178
|
+
return i;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Resolve the path to a specified package asynchronously
|
|
182
|
+
*
|
|
183
|
+
* @remarks
|
|
184
|
+
* This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.
|
|
185
|
+
*
|
|
186
|
+
* @param name - The name of the module
|
|
187
|
+
* @returns A promise for the module or undefined
|
|
188
|
+
*/
|
|
189
|
+
async function resolvePackage(name, options = {}) {
|
|
190
|
+
let result = await resolveSafe(joinPaths(name, "package.json"), options);
|
|
191
|
+
if (!result) {
|
|
192
|
+
result = await resolveSafe(joinPaths(name, "index.js"), options);
|
|
193
|
+
if (!result) result = await resolveSafe(name, options);
|
|
194
|
+
}
|
|
195
|
+
return result ? findFilePath(result) : void 0;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Resolve the path to a specified package synchronously
|
|
199
|
+
*
|
|
200
|
+
* @remarks
|
|
201
|
+
* This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.
|
|
202
|
+
*
|
|
203
|
+
* @param name - The name of the module
|
|
204
|
+
* @param options - The options to use when resolving the module
|
|
205
|
+
* @returns The module or undefined
|
|
206
|
+
*/
|
|
207
|
+
function resolvePackageSync(name, options = {}) {
|
|
208
|
+
let result = resolveSafeSync(joinPaths(name, "package.json"), options);
|
|
209
|
+
if (!result) {
|
|
210
|
+
result = resolveSafeSync(joinPaths(name, "index.js"), options);
|
|
211
|
+
if (!result) result = resolveSafeSync(name, options);
|
|
212
|
+
}
|
|
213
|
+
return result ? findFilePath(result) : void 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
//#endregion
|
|
217
|
+
export { DEFAULT_EXTENSIONS, getNodeModulesPaths, getResolutionCombinations, getResolutionPaths, importModule, resolve, resolvePackage, resolvePackageSync, resolveSafe, resolveSafeSync, resolveSync };
|
|
4
218
|
//# sourceMappingURL=resolve.mjs.map
|
package/dist/resolve.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.mjs","names":["result: string | undefined","error: Error | undefined","result!: string"],"sources":["../src/resolve.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { getUnique } from \"@stryke/helpers/get-unique\";\nimport { appendExtension } from \"@stryke/path\";\nimport { correctPath, toAbsolutePath } from \"@stryke/path/correct-path\";\nimport { cwd } from \"@stryke/path/cwd\";\nimport {\n findFileExtension,\n findFileName,\n findFilePath,\n findFolderName\n} from \"@stryke/path/file-path-fns\";\nimport { isAbsolutePath, isNpmScopedPackage } from \"@stryke/path/is-type\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { interopDefault, resolvePath, resolvePathSync } from \"mlly\";\nimport { existsSync } from \"./exists\";\nimport { getWorkspaceRoot } from \"./get-workspace-root\";\n\nexport const DEFAULT_EXTENSIONS = [\n \"js\",\n \"jsx\",\n \"mjs\",\n \"cjs\",\n \"ts\",\n \"tsx\",\n \"mts\",\n \"cts\",\n \"json\",\n \"jsonc\",\n \"json5\",\n \"node\"\n];\n\nexport interface ResolveOptions {\n /**\n * Paths to resolve the package from\n */\n paths?: string[];\n\n /**\n * File extensions to consider when resolving the module\n *\n * @remarks\n * Extensions can be provided with or without the leading dot. The resolver utilities will handle both cases.\n *\n * @defaultValue [\"js\", \"jsx\", \"mjs\", \"cjs\", \"ts\", \"tsx\", \"mts\", \"cts\", \"json\", \"jsonc\", \"json5\", \"node\", \"wasm\"]\n */\n extensions?: string[];\n\n /**\n * Conditions to consider when resolving package exports.\n */\n conditions?: string[];\n}\n\n/**\n * Get the resolution paths based on the provided paths, current working directory, and workspace root.\n *\n * @param paths - An array of paths to include in the resolution.\n * @returns An array of unique, corrected resolution paths.\n */\nexport function getResolutionPaths(paths: string[] = []) {\n let resolutionPaths = paths;\n if (!resolutionPaths.includes(cwd())) {\n resolutionPaths.push(cwd());\n }\n\n const workspaceRoot = getWorkspaceRoot();\n if (!resolutionPaths.includes(workspaceRoot)) {\n resolutionPaths.push(workspaceRoot);\n }\n\n resolutionPaths = getUnique(\n resolutionPaths\n .filter(Boolean)\n .map(path => correctPath(path))\n .reduce((ret, path, _, arr) => {\n ret.push(path);\n if (!isAbsolutePath(path)) {\n ret.push(toAbsolutePath(path, cwd()));\n ret.push(toAbsolutePath(path, workspaceRoot));\n\n arr.forEach(existing => {\n ret.push(toAbsolutePath(path, existing));\n });\n }\n\n return ret;\n }, [] as string[])\n );\n\n return resolutionPaths;\n}\n\n/**\n * Get the node_modules resolution paths based on the provided paths.\n *\n * @param paths - An array of paths to include in the resolution.\n * @returns An array of unique, corrected node_modules resolution paths.\n */\nexport function getNodeModulesPaths(paths: string[] = []) {\n return getUnique(\n paths.reduce((ret, path) => {\n if (findFolderName(path) === \"node_modules\") {\n ret.push(correctPath(path));\n }\n\n if (existsSync(joinPaths(path, \"node_modules\"))) {\n ret.push(correctPath(joinPaths(path, \"node_modules\")));\n }\n\n return ret;\n }, [] as string[])\n );\n}\n\n/**\n * Get all combinations of resolution paths for a given path and options.\n *\n * @param path - The base path to combine with resolution paths.\n * @param options - The options containing resolution paths.\n * @returns An array of unique, corrected resolution paths.\n */\nexport function getResolutionCombinations(\n path: string,\n options: ResolveOptions = {}\n) {\n let paths = getResolutionPaths(options.paths);\n if (isNpmScopedPackage(path)) {\n paths = getNodeModulesPaths(paths);\n } else {\n paths.push(...getNodeModulesPaths(paths));\n }\n\n const extensions = options.extensions ?? DEFAULT_EXTENSIONS;\n\n let combinations = paths.map(base => joinPaths(base, path));\n if (findFileName(path, { withExtension: false }) !== \"index\") {\n combinations = combinations.reduce((ret, combination) => {\n ret.push(combination);\n ret.push(joinPaths(combination, \"index\"));\n\n return ret;\n }, [] as string[]);\n }\n\n if (!findFileExtension(path)) {\n combinations = combinations.reduce((ret, combination) => {\n ret.push(combination);\n extensions.forEach(ext => {\n ret.push(appendExtension(combination, ext));\n });\n\n return ret;\n }, [] as string[]);\n }\n\n return getUnique(combinations.filter(Boolean)).map(p => correctPath(p));\n}\n\n/**\n * Resolve the path to a specified module\n *\n * @param path - The path to the module\n * @param options - The options to use when resolving the module\n * @returns A promise for the path to the module\n */\nexport async function resolve(\n path: string,\n options: ResolveOptions = {}\n): Promise<string> {\n let paths = getResolutionPaths(options.paths);\n if (isNpmScopedPackage(path)) {\n paths = getNodeModulesPaths(paths);\n } else {\n paths.push(...getNodeModulesPaths(paths));\n }\n\n let result: string | undefined;\n let error: Error | undefined;\n\n try {\n result = await resolvePath(path, {\n url: paths,\n extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n conditions: options.conditions\n });\n } catch (err) {\n error = err as Error;\n }\n\n // if (!result) {\n // for (let i = 0; i < paths.length && !result; i++) {\n // try {\n // result = await resolvePath(replacePath(path, paths[i]), {\n // url: paths,\n // extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n // conditions: options.conditions\n // });\n // } catch (err) {\n // error = err as Error;\n // }\n // }\n // }\n\n if (!result) {\n throw new Error(\n `Unable to resolve module \"${\n path\n }\". The following import paths were tried: \\n${paths.join(\"\\n\")}`,\n {\n cause: error\n }\n );\n }\n\n return correctPath(result);\n}\n\n/**\n * Resolve the path to a specified module\n *\n * @param path - The path to the module\n * @param options - The options to use when resolving the module\n * @returns The path to the module or undefined\n */\nexport function resolveSync(path: string, options: ResolveOptions = {}) {\n let paths = getResolutionPaths(options.paths);\n if (isNpmScopedPackage(path)) {\n paths = getNodeModulesPaths(paths);\n } else {\n paths.push(...getNodeModulesPaths(paths));\n }\n\n let result!: string;\n let error: Error | undefined;\n\n try {\n result = resolvePathSync(path, {\n url: paths,\n extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n conditions: options.conditions\n });\n } catch (err) {\n error = err as Error;\n }\n\n // if (!result) {\n // for (let i = 0; i < paths.length && !result; i++) {\n // try {\n // result = resolvePathSync(replacePath(path, paths[i]), {\n // url: paths,\n // extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n // conditions: options.conditions\n // });\n // } catch (err) {\n // error = err as Error;\n // }\n // }\n // }\n\n // if (!result && findFileName(path, { withExtension: false }) !== \"index\") {\n // try {\n // result = resolveSync(joinPaths(path, \"index\"), {\n // ...options,\n // paths\n // });\n // if (result) {\n // // Remove the previously added index file from the path\n // result = findFilePath(result);\n // }\n // } catch (err) {\n // error = err as Error;\n // }\n // }\n\n if (!result) {\n throw new Error(\n `Unable to resolve module \"${\n path\n }\". The following import paths were tried: \\n${paths.join(\"\\n\")}`,\n {\n cause: error\n }\n );\n }\n\n return correctPath(result);\n}\n\n/**\n * Resolve the path to a specified module with error handling\n *\n * @param name - The name of the module\n * @param options - The options to use when resolving the module\n * @returns A promise for the path to the module\n */\nexport async function resolveSafe(name: string, options: ResolveOptions = {}) {\n try {\n return await resolve(name, options);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Resolve the path to a specified module with error handling\n *\n * @param name - The name of the module\n * @param options - The options to use when resolving the module\n * @returns The path to the module or undefined\n */\nexport function resolveSafeSync(name: string, options: ResolveOptions = {}) {\n try {\n return resolveSync(name, options);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Import a module from a specified path\n *\n * @param path - The path to the module\n * @returns The module\n */\nexport async function importModule<T = any>(path: string): Promise<T> {\n const i = await import(path);\n\n if (i) {\n return interopDefault(i);\n }\n\n return i;\n}\n\n/**\n * Resolve the path to a specified package asynchronously\n *\n * @remarks\n * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.\n *\n * @param name - The name of the module\n * @returns A promise for the module or undefined\n */\nexport async function resolvePackage(\n name: string,\n options: ResolveOptions = {}\n) {\n let result = await resolveSafe(joinPaths(name, \"package.json\"), options);\n if (!result) {\n result = await resolveSafe(joinPaths(name, \"index.js\"), options);\n if (!result) {\n result = await resolveSafe(name, options);\n }\n }\n\n return result ? findFilePath(result) : undefined;\n}\n\n/**\n * Resolve the path to a specified package synchronously\n *\n * @remarks\n * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.\n *\n * @param name - The name of the module\n * @param options - The options to use when resolving the module\n * @returns The module or undefined\n */\nexport function resolvePackageSync(name: string, options: ResolveOptions = {}) {\n let result = resolveSafeSync(joinPaths(name, \"package.json\"), options);\n if (!result) {\n result = resolveSafeSync(joinPaths(name, \"index.js\"), options);\n if (!result) {\n result = resolveSafeSync(name, options);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CAA8C,EAAM,KAAK;EAAK,GAC/D,CACE,MAAO,EACR,CACF,CAGH,OAAO,EAAY,EAAO,CAU5B,eAAsB,EAAY,EAAc,EAA0B,EAAE,CAAE,CAC5E,GAAI,CACF,OAAO,MAAM,EAAQ,EAAM,EAAQ,MAC7B,CACN,QAWJ,SAAgB,EAAgB,EAAc,EAA0B,EAAE,CAAE,CAC1E,GAAI,CACF,OAAO,EAAY,EAAM,EAAQ,MAC3B,CACN,QAUJ,eAAsB,EAAsB,EAA0B,CACpE,IAAM,EAAI,MAAM,OAAO,GAMvB,OAJI,GACK,EAAe,EAAE,CAe5B,eAAsB,EACpB,EACA,EAA0B,EAAE,CAC5B,CACA,IAAI,EAAS,MAAM,EAAY,EAAU,EAAM,eAAe,CAAE,EAAQ,CAQxE,OAPK,IACH,EAAS,MAAM,EAAY,EAAU,EAAM,WAAW,CAAE,EAAQ,CAChE,AACE,IAAS,MAAM,EAAY,EAAM,EAAQ,EAItC,EAAS,EAAa,EAAO,CAAG,IAAA,GAazC,SAAgB,EAAmB,EAAc,EAA0B,EAAE,CAAE,CAC7E,IAAI,EAAS,EAAgB,EAAU,EAAM,eAAe,CAAE,EAAQ,CAQtE,OAPK,IACH,EAAS,EAAgB,EAAU,EAAM,WAAW,CAAE,EAAQ,CAC9D,AACE,IAAS,EAAgB,EAAM,EAAQ,EAIpC,EAAS,EAAa,EAAO,CAAG,IAAA"}
|
|
1
|
+
{"version":3,"file":"resolve.mjs","names":["result: string | undefined","error: Error | undefined","result!: string"],"sources":["../src/resolve.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { getUnique } from \"@stryke/helpers/get-unique\";\nimport { appendExtension } from \"@stryke/path\";\nimport { correctPath, toAbsolutePath } from \"@stryke/path/correct-path\";\nimport { cwd } from \"@stryke/path/cwd\";\nimport {\n findFileExtension,\n findFileName,\n findFilePath,\n findFolderName\n} from \"@stryke/path/file-path-fns\";\nimport { isAbsolutePath, isNpmScopedPackage } from \"@stryke/path/is-type\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { interopDefault, resolvePath, resolvePathSync } from \"mlly\";\nimport { existsSync } from \"./exists\";\nimport { getWorkspaceRoot } from \"./get-workspace-root\";\n\nexport const DEFAULT_EXTENSIONS = [\n \"js\",\n \"jsx\",\n \"mjs\",\n \"cjs\",\n \"ts\",\n \"tsx\",\n \"mts\",\n \"cts\",\n \"json\",\n \"jsonc\",\n \"json5\",\n \"node\"\n];\n\nexport interface ResolveOptions {\n /**\n * Paths to resolve the package from\n */\n paths?: string[];\n\n /**\n * File extensions to consider when resolving the module\n *\n * @remarks\n * Extensions can be provided with or without the leading dot. The resolver utilities will handle both cases.\n *\n * @defaultValue [\"js\", \"jsx\", \"mjs\", \"cjs\", \"ts\", \"tsx\", \"mts\", \"cts\", \"json\", \"jsonc\", \"json5\", \"node\", \"wasm\"]\n */\n extensions?: string[];\n\n /**\n * Conditions to consider when resolving package exports.\n */\n conditions?: string[];\n}\n\n/**\n * Get the resolution paths based on the provided paths, current working directory, and workspace root.\n *\n * @param paths - An array of paths to include in the resolution.\n * @returns An array of unique, corrected resolution paths.\n */\nexport function getResolutionPaths(paths: string[] = []) {\n let resolutionPaths = paths;\n if (!resolutionPaths.includes(cwd())) {\n resolutionPaths.push(cwd());\n }\n\n const workspaceRoot = getWorkspaceRoot();\n if (!resolutionPaths.includes(workspaceRoot)) {\n resolutionPaths.push(workspaceRoot);\n }\n\n resolutionPaths = getUnique(\n resolutionPaths\n .filter(Boolean)\n .map(path => correctPath(path))\n .reduce((ret, path, _, arr) => {\n ret.push(path);\n if (!isAbsolutePath(path)) {\n ret.push(toAbsolutePath(path, cwd()));\n ret.push(toAbsolutePath(path, workspaceRoot));\n\n arr.forEach(existing => {\n ret.push(toAbsolutePath(path, existing));\n });\n }\n\n return ret;\n }, [] as string[])\n );\n\n return resolutionPaths;\n}\n\n/**\n * Get the node_modules resolution paths based on the provided paths.\n *\n * @param paths - An array of paths to include in the resolution.\n * @returns An array of unique, corrected node_modules resolution paths.\n */\nexport function getNodeModulesPaths(paths: string[] = []) {\n return getUnique(\n paths.reduce((ret, path) => {\n if (findFolderName(path) === \"node_modules\") {\n ret.push(correctPath(path));\n }\n\n if (existsSync(joinPaths(path, \"node_modules\"))) {\n ret.push(correctPath(joinPaths(path, \"node_modules\")));\n }\n\n return ret;\n }, [] as string[])\n );\n}\n\n/**\n * Get all combinations of resolution paths for a given path and options.\n *\n * @param path - The base path to combine with resolution paths.\n * @param options - The options containing resolution paths.\n * @returns An array of unique, corrected resolution paths.\n */\nexport function getResolutionCombinations(\n path: string,\n options: ResolveOptions = {}\n) {\n let paths = getResolutionPaths(options.paths);\n if (isNpmScopedPackage(path)) {\n paths = getNodeModulesPaths(paths);\n } else {\n paths.push(...getNodeModulesPaths(paths));\n }\n\n const extensions = options.extensions ?? DEFAULT_EXTENSIONS;\n\n let combinations = paths.map(base => joinPaths(base, path));\n if (findFileName(path, { withExtension: false }) !== \"index\") {\n combinations = combinations.reduce((ret, combination) => {\n ret.push(combination);\n ret.push(joinPaths(combination, \"index\"));\n\n return ret;\n }, [] as string[]);\n }\n\n if (!findFileExtension(path)) {\n combinations = combinations.reduce((ret, combination) => {\n ret.push(combination);\n extensions.forEach(ext => {\n ret.push(appendExtension(combination, ext));\n });\n\n return ret;\n }, [] as string[]);\n }\n\n return getUnique(combinations.filter(Boolean)).map(p => correctPath(p));\n}\n\n/**\n * Resolve the path to a specified module\n *\n * @param path - The path to the module\n * @param options - The options to use when resolving the module\n * @returns A promise for the path to the module\n */\nexport async function resolve(\n path: string,\n options: ResolveOptions = {}\n): Promise<string> {\n let paths = getResolutionPaths(options.paths);\n if (isNpmScopedPackage(path)) {\n paths = getNodeModulesPaths(paths);\n } else {\n paths.push(...getNodeModulesPaths(paths));\n }\n\n let result: string | undefined;\n let error: Error | undefined;\n\n try {\n result = await resolvePath(path, {\n url: paths,\n extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n conditions: options.conditions\n });\n } catch (err) {\n error = err as Error;\n }\n\n // if (!result) {\n // for (let i = 0; i < paths.length && !result; i++) {\n // try {\n // result = await resolvePath(replacePath(path, paths[i]), {\n // url: paths,\n // extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n // conditions: options.conditions\n // });\n // } catch (err) {\n // error = err as Error;\n // }\n // }\n // }\n\n if (!result) {\n throw new Error(\n `Unable to resolve module \"${\n path\n }\". The following import paths were tried: \\n${paths.join(\"\\n\")}`,\n {\n cause: error\n }\n );\n }\n\n return correctPath(result);\n}\n\n/**\n * Resolve the path to a specified module\n *\n * @param path - The path to the module\n * @param options - The options to use when resolving the module\n * @returns The path to the module or undefined\n */\nexport function resolveSync(path: string, options: ResolveOptions = {}) {\n let paths = getResolutionPaths(options.paths);\n if (isNpmScopedPackage(path)) {\n paths = getNodeModulesPaths(paths);\n } else {\n paths.push(...getNodeModulesPaths(paths));\n }\n\n let result!: string;\n let error: Error | undefined;\n\n try {\n result = resolvePathSync(path, {\n url: paths,\n extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n conditions: options.conditions\n });\n } catch (err) {\n error = err as Error;\n }\n\n // if (!result) {\n // for (let i = 0; i < paths.length && !result; i++) {\n // try {\n // result = resolvePathSync(replacePath(path, paths[i]), {\n // url: paths,\n // extensions: options.extensions ?? DEFAULT_EXTENSIONS,\n // conditions: options.conditions\n // });\n // } catch (err) {\n // error = err as Error;\n // }\n // }\n // }\n\n // if (!result && findFileName(path, { withExtension: false }) !== \"index\") {\n // try {\n // result = resolveSync(joinPaths(path, \"index\"), {\n // ...options,\n // paths\n // });\n // if (result) {\n // // Remove the previously added index file from the path\n // result = findFilePath(result);\n // }\n // } catch (err) {\n // error = err as Error;\n // }\n // }\n\n if (!result) {\n throw new Error(\n `Unable to resolve module \"${\n path\n }\". The following import paths were tried: \\n${paths.join(\"\\n\")}`,\n {\n cause: error\n }\n );\n }\n\n return correctPath(result);\n}\n\n/**\n * Resolve the path to a specified module with error handling\n *\n * @param name - The name of the module\n * @param options - The options to use when resolving the module\n * @returns A promise for the path to the module\n */\nexport async function resolveSafe(name: string, options: ResolveOptions = {}) {\n try {\n return await resolve(name, options);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Resolve the path to a specified module with error handling\n *\n * @param name - The name of the module\n * @param options - The options to use when resolving the module\n * @returns The path to the module or undefined\n */\nexport function resolveSafeSync(name: string, options: ResolveOptions = {}) {\n try {\n return resolveSync(name, options);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Import a module from a specified path\n *\n * @param path - The path to the module\n * @returns The module\n */\nexport async function importModule<T = any>(path: string): Promise<T> {\n const i = await import(path);\n\n if (i) {\n return interopDefault(i);\n }\n\n return i;\n}\n\n/**\n * Resolve the path to a specified package asynchronously\n *\n * @remarks\n * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.\n *\n * @param name - The name of the module\n * @returns A promise for the module or undefined\n */\nexport async function resolvePackage(\n name: string,\n options: ResolveOptions = {}\n) {\n let result = await resolveSafe(joinPaths(name, \"package.json\"), options);\n if (!result) {\n result = await resolveSafe(joinPaths(name, \"index.js\"), options);\n if (!result) {\n result = await resolveSafe(name, options);\n }\n }\n\n return result ? findFilePath(result) : undefined;\n}\n\n/**\n * Resolve the path to a specified package synchronously\n *\n * @remarks\n * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.\n *\n * @param name - The name of the module\n * @param options - The options to use when resolving the module\n * @returns The module or undefined\n */\nexport function resolvePackageSync(name: string, options: ResolveOptions = {}) {\n let result = resolveSafeSync(joinPaths(name, \"package.json\"), options);\n if (!result) {\n result = resolveSafeSync(joinPaths(name, \"index.js\"), options);\n if (!result) {\n result = resolveSafeSync(name, options);\n }\n }\n\n return result ? findFilePath(result) : undefined;\n}\n"],"mappings":";;;;;;;;;;;;AAkCA,MAAa,qBAAqB;CAChC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;;;;;;AA8BD,SAAgB,mBAAmB,QAAkB,EAAE,EAAE;CACvD,IAAI,kBAAkB;AACtB,KAAI,CAAC,gBAAgB,SAAS,KAAK,CAAC,CAClC,iBAAgB,KAAK,KAAK,CAAC;CAG7B,MAAM,gBAAgB,kBAAkB;AACxC,KAAI,CAAC,gBAAgB,SAAS,cAAc,CAC1C,iBAAgB,KAAK,cAAc;AAGrC,mBAAkB,UAChB,gBACG,OAAO,QAAQ,CACf,KAAI,SAAQ,YAAY,KAAK,CAAC,CAC9B,QAAQ,KAAK,MAAM,GAAG,QAAQ;AAC7B,MAAI,KAAK,KAAK;AACd,MAAI,CAAC,eAAe,KAAK,EAAE;AACzB,OAAI,KAAK,eAAe,MAAM,KAAK,CAAC,CAAC;AACrC,OAAI,KAAK,eAAe,MAAM,cAAc,CAAC;AAE7C,OAAI,SAAQ,aAAY;AACtB,QAAI,KAAK,eAAe,MAAM,SAAS,CAAC;KACxC;;AAGJ,SAAO;IACN,EAAE,CAAa,CACrB;AAED,QAAO;;;;;;;;AAST,SAAgB,oBAAoB,QAAkB,EAAE,EAAE;AACxD,QAAO,UACL,MAAM,QAAQ,KAAK,SAAS;AAC1B,MAAI,eAAe,KAAK,KAAK,eAC3B,KAAI,KAAK,YAAY,KAAK,CAAC;AAG7B,MAAI,WAAW,UAAU,MAAM,eAAe,CAAC,CAC7C,KAAI,KAAK,YAAY,UAAU,MAAM,eAAe,CAAC,CAAC;AAGxD,SAAO;IACN,EAAE,CAAa,CACnB;;;;;;;;;AAUH,SAAgB,0BACd,MACA,UAA0B,EAAE,EA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,CAAC,QAAQ;AACX,WAAS,gBAAgB,UAAU,MAAM,WAAW,EAAE,QAAQ;AAC9D,MAAI,CAAC,OACH,UAAS,gBAAgB,MAAM,QAAQ;;AAI3C,QAAO,SAAS,aAAa,OAAO,GAAG"}
|
package/dist/semver-fns.cjs
CHANGED
|
@@ -1 +1,129 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_is_string = require('./type-checks/src/is-string.cjs');
|
|
3
|
+
const require_is_object = require('./type-checks/src/is-object.cjs');
|
|
4
|
+
let semver = require("semver");
|
|
5
|
+
|
|
6
|
+
//#region src/semver-fns.ts
|
|
7
|
+
/**
|
|
8
|
+
* Parse a semver string into a SemVer object
|
|
9
|
+
*
|
|
10
|
+
* @param semver - The semver string to parse
|
|
11
|
+
* @param loose - Whether to use loose parsing
|
|
12
|
+
* @returns The parsed SemVer object
|
|
13
|
+
*/
|
|
14
|
+
const parseVersion = (semver$1, loose = true) => (0, semver.parse)(semver$1, loose);
|
|
15
|
+
/**
|
|
16
|
+
* Coerce a version string into a valid SemVer string
|
|
17
|
+
*
|
|
18
|
+
* @param version - The version string or number or {@link SemVer} to coerce
|
|
19
|
+
* @param options - Options to use when coercing the version
|
|
20
|
+
* @returns The coerced SemVer string or null if invalid
|
|
21
|
+
*/
|
|
22
|
+
const coerceVersion = (version, options) => {
|
|
23
|
+
return (0, semver.coerce)(version, options);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Type check for {@link SemVer}
|
|
27
|
+
*
|
|
28
|
+
* @param val - The value to check
|
|
29
|
+
* @returns Whether the value is a valid {@link SemVer}
|
|
30
|
+
*/
|
|
31
|
+
const isSemver = (val) => {
|
|
32
|
+
return require_is_object.isObject(val) && "version" in val;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Type check for {@link Range}
|
|
36
|
+
*
|
|
37
|
+
* @param val - The value to check
|
|
38
|
+
* @returns Whether the value is a valid {@link Range}
|
|
39
|
+
*/
|
|
40
|
+
const isRange = (val) => {
|
|
41
|
+
return require_is_object.isObject(val) && "range" in val;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Check if a {@link SemVer} string is valid
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* If you're looking for type checking, please use the {@link isSemver} function.
|
|
48
|
+
*
|
|
49
|
+
* @param semver - The semver string to check
|
|
50
|
+
* @param loose - Whether to use loose parsing
|
|
51
|
+
* @returns Whether the semver string is valid
|
|
52
|
+
*/
|
|
53
|
+
const isValidSemver = (semver$1, loose = true) => {
|
|
54
|
+
return (require_is_string.isString(semver$1) || isSemver(semver$1)) && (0, semver.valid)(semver$1, loose) !== null;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Check if a {@link Range} string is valid
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* If you're looking for type checking, please use the {@link isRange} function.
|
|
61
|
+
*
|
|
62
|
+
* @param range - The range string to check
|
|
63
|
+
* @param loose - Whether to use loose parsing
|
|
64
|
+
* @returns Whether the range string is valid
|
|
65
|
+
*/
|
|
66
|
+
const isValidRange = (range, loose = true) => {
|
|
67
|
+
return (require_is_string.isString(range) || isRange(range)) && (0, semver.validRange)(range, loose) !== null;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Check if a {@link SemVer} or {@link Range} string is valid
|
|
71
|
+
*
|
|
72
|
+
* @param version - The semver string to check
|
|
73
|
+
* @param loose - Whether to use loose parsing
|
|
74
|
+
* @returns Whether the semver string is valid
|
|
75
|
+
*/
|
|
76
|
+
const isValidVersion = (version, loose = true) => {
|
|
77
|
+
return isValidSemver(version, loose) || isValidRange(version, loose);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Check if a semver string satisfies a range
|
|
81
|
+
*
|
|
82
|
+
* @param version - The semver string to check
|
|
83
|
+
* @param range - The range to check against
|
|
84
|
+
* @param loose - Whether to use loose parsing
|
|
85
|
+
* @returns Whether the semver string satisfies the range
|
|
86
|
+
*/
|
|
87
|
+
const satisfiesVersion = (version, range, loose = true) => {
|
|
88
|
+
if (!version || !range || !isValidSemver(version, loose) || !isValidRange(range, loose)) return false;
|
|
89
|
+
return (0, semver.satisfies)(version, range, { loose });
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Check if a string is a valid relative version keyword
|
|
93
|
+
*
|
|
94
|
+
* @param val - The string to check
|
|
95
|
+
* @returns Whether the string is a valid relative version keyword
|
|
96
|
+
*/
|
|
97
|
+
const isRelativeVersionKeyword = (val) => {
|
|
98
|
+
return semver.RELEASE_TYPES.includes(val);
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Derive a new semver version from the current version and a version specifier
|
|
102
|
+
*
|
|
103
|
+
* @param currentSemverVersion - The current semver version
|
|
104
|
+
* @param semverSpecifier - The semver specifier to use
|
|
105
|
+
* @param preid - The pre-release identifier to use
|
|
106
|
+
* @returns The derived new semver version
|
|
107
|
+
*/
|
|
108
|
+
const deriveNewSemverVersion = (currentSemverVersion, semverSpecifier, preid) => {
|
|
109
|
+
if (!(0, semver.valid)(currentSemverVersion)) throw new Error(`Invalid semver version "${currentSemverVersion}" provided.`);
|
|
110
|
+
let newVersion = semverSpecifier;
|
|
111
|
+
if (isRelativeVersionKeyword(semverSpecifier)) {
|
|
112
|
+
const derivedVersion = (0, semver.inc)(currentSemverVersion, semverSpecifier, preid);
|
|
113
|
+
if (!derivedVersion) throw new Error(`Unable to derive new version from current version "${currentSemverVersion}" and version specifier "${semverSpecifier}"`);
|
|
114
|
+
newVersion = derivedVersion;
|
|
115
|
+
} else if (!(0, semver.valid)(semverSpecifier)) throw new Error(`Invalid semver version specifier "${semverSpecifier}" provided. Please provide either a valid semver version or a valid semver version keyword.`);
|
|
116
|
+
return newVersion;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
exports.coerceVersion = coerceVersion;
|
|
121
|
+
exports.deriveNewSemverVersion = deriveNewSemverVersion;
|
|
122
|
+
exports.isRange = isRange;
|
|
123
|
+
exports.isRelativeVersionKeyword = isRelativeVersionKeyword;
|
|
124
|
+
exports.isSemver = isSemver;
|
|
125
|
+
exports.isValidRange = isValidRange;
|
|
126
|
+
exports.isValidSemver = isValidSemver;
|
|
127
|
+
exports.isValidVersion = isValidVersion;
|
|
128
|
+
exports.parseVersion = parseVersion;
|
|
129
|
+
exports.satisfiesVersion = satisfiesVersion;
|
package/dist/semver-fns.mjs
CHANGED
|
@@ -1,2 +1,120 @@
|
|
|
1
|
-
import{isString
|
|
1
|
+
import { isString } from "./type-checks/src/is-string.mjs";
|
|
2
|
+
import { isObject } from "./type-checks/src/is-object.mjs";
|
|
3
|
+
import { RELEASE_TYPES, coerce, inc, parse, satisfies, valid, validRange } from "semver";
|
|
4
|
+
|
|
5
|
+
//#region src/semver-fns.ts
|
|
6
|
+
/**
|
|
7
|
+
* Parse a semver string into a SemVer object
|
|
8
|
+
*
|
|
9
|
+
* @param semver - The semver string to parse
|
|
10
|
+
* @param loose - Whether to use loose parsing
|
|
11
|
+
* @returns The parsed SemVer object
|
|
12
|
+
*/
|
|
13
|
+
const parseVersion = (semver, loose = true) => parse(semver, loose);
|
|
14
|
+
/**
|
|
15
|
+
* Coerce a version string into a valid SemVer string
|
|
16
|
+
*
|
|
17
|
+
* @param version - The version string or number or {@link SemVer} to coerce
|
|
18
|
+
* @param options - Options to use when coercing the version
|
|
19
|
+
* @returns The coerced SemVer string or null if invalid
|
|
20
|
+
*/
|
|
21
|
+
const coerceVersion = (version, options) => {
|
|
22
|
+
return coerce(version, options);
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Type check for {@link SemVer}
|
|
26
|
+
*
|
|
27
|
+
* @param val - The value to check
|
|
28
|
+
* @returns Whether the value is a valid {@link SemVer}
|
|
29
|
+
*/
|
|
30
|
+
const isSemver = (val) => {
|
|
31
|
+
return isObject(val) && "version" in val;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Type check for {@link Range}
|
|
35
|
+
*
|
|
36
|
+
* @param val - The value to check
|
|
37
|
+
* @returns Whether the value is a valid {@link Range}
|
|
38
|
+
*/
|
|
39
|
+
const isRange = (val) => {
|
|
40
|
+
return isObject(val) && "range" in val;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Check if a {@link SemVer} string is valid
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* If you're looking for type checking, please use the {@link isSemver} function.
|
|
47
|
+
*
|
|
48
|
+
* @param semver - The semver string to check
|
|
49
|
+
* @param loose - Whether to use loose parsing
|
|
50
|
+
* @returns Whether the semver string is valid
|
|
51
|
+
*/
|
|
52
|
+
const isValidSemver = (semver, loose = true) => {
|
|
53
|
+
return (isString(semver) || isSemver(semver)) && valid(semver, loose) !== null;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Check if a {@link Range} string is valid
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* If you're looking for type checking, please use the {@link isRange} function.
|
|
60
|
+
*
|
|
61
|
+
* @param range - The range string to check
|
|
62
|
+
* @param loose - Whether to use loose parsing
|
|
63
|
+
* @returns Whether the range string is valid
|
|
64
|
+
*/
|
|
65
|
+
const isValidRange = (range, loose = true) => {
|
|
66
|
+
return (isString(range) || isRange(range)) && validRange(range, loose) !== null;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Check if a {@link SemVer} or {@link Range} string is valid
|
|
70
|
+
*
|
|
71
|
+
* @param version - The semver string to check
|
|
72
|
+
* @param loose - Whether to use loose parsing
|
|
73
|
+
* @returns Whether the semver string is valid
|
|
74
|
+
*/
|
|
75
|
+
const isValidVersion = (version, loose = true) => {
|
|
76
|
+
return isValidSemver(version, loose) || isValidRange(version, loose);
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Check if a semver string satisfies a range
|
|
80
|
+
*
|
|
81
|
+
* @param version - The semver string to check
|
|
82
|
+
* @param range - The range to check against
|
|
83
|
+
* @param loose - Whether to use loose parsing
|
|
84
|
+
* @returns Whether the semver string satisfies the range
|
|
85
|
+
*/
|
|
86
|
+
const satisfiesVersion = (version, range, loose = true) => {
|
|
87
|
+
if (!version || !range || !isValidSemver(version, loose) || !isValidRange(range, loose)) return false;
|
|
88
|
+
return satisfies(version, range, { loose });
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Check if a string is a valid relative version keyword
|
|
92
|
+
*
|
|
93
|
+
* @param val - The string to check
|
|
94
|
+
* @returns Whether the string is a valid relative version keyword
|
|
95
|
+
*/
|
|
96
|
+
const isRelativeVersionKeyword = (val) => {
|
|
97
|
+
return RELEASE_TYPES.includes(val);
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Derive a new semver version from the current version and a version specifier
|
|
101
|
+
*
|
|
102
|
+
* @param currentSemverVersion - The current semver version
|
|
103
|
+
* @param semverSpecifier - The semver specifier to use
|
|
104
|
+
* @param preid - The pre-release identifier to use
|
|
105
|
+
* @returns The derived new semver version
|
|
106
|
+
*/
|
|
107
|
+
const deriveNewSemverVersion = (currentSemverVersion, semverSpecifier, preid) => {
|
|
108
|
+
if (!valid(currentSemverVersion)) throw new Error(`Invalid semver version "${currentSemverVersion}" provided.`);
|
|
109
|
+
let newVersion = semverSpecifier;
|
|
110
|
+
if (isRelativeVersionKeyword(semverSpecifier)) {
|
|
111
|
+
const derivedVersion = inc(currentSemverVersion, semverSpecifier, preid);
|
|
112
|
+
if (!derivedVersion) throw new Error(`Unable to derive new version from current version "${currentSemverVersion}" and version specifier "${semverSpecifier}"`);
|
|
113
|
+
newVersion = derivedVersion;
|
|
114
|
+
} else if (!valid(semverSpecifier)) throw new Error(`Invalid semver version specifier "${semverSpecifier}" provided. Please provide either a valid semver version or a valid semver version keyword.`);
|
|
115
|
+
return newVersion;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
119
|
+
export { coerceVersion, deriveNewSemverVersion, isRange, isRelativeVersionKeyword, isSemver, isValidRange, isValidSemver, isValidVersion, parseVersion, satisfiesVersion };
|
|
2
120
|
//# sourceMappingURL=semver-fns.mjs.map
|
package/dist/semver-fns.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"semver-fns.mjs","names":[],"sources":["../src/semver-fns.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { CoerceOptions, Range, ReleaseType, SemVer } from \"semver\";\nimport {\n RELEASE_TYPES,\n coerce,\n inc,\n parse,\n satisfies,\n valid,\n validRange\n} from \"semver\";\n\n/**\n * Parse a semver string into a SemVer object\n *\n * @param semver - The semver string to parse\n * @param loose - Whether to use loose parsing\n * @returns The parsed SemVer object\n */\nexport const parseVersion = (semver: string, loose = true) =>\n parse(semver, loose);\n\n/**\n * Coerce a version string into a valid SemVer string\n *\n * @param version - The version string or number or {@link SemVer} to coerce\n * @param options - Options to use when coercing the version\n * @returns The coerced SemVer string or null if invalid\n */\nexport const coerceVersion = (\n version: string | number | SemVer | null | undefined,\n options?: CoerceOptions\n): SemVer | null => {\n return coerce(version, options);\n};\n\n/**\n * Type check for {@link SemVer}\n *\n * @param val - The value to check\n * @returns Whether the value is a valid {@link SemVer}\n */\nexport const isSemver = (val: any): val is SemVer => {\n return isObject(val) && \"version\" in val;\n};\n\n/**\n * Type check for {@link Range}\n *\n * @param val - The value to check\n * @returns Whether the value is a valid {@link Range}\n */\nexport const isRange = (val: any): val is Range => {\n return isObject(val) && \"range\" in val;\n};\n\n/**\n * Check if a {@link SemVer} string is valid\n *\n * @remarks\n * If you're looking for type checking, please use the {@link isSemver} function.\n *\n * @param semver - The semver string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string is valid\n */\nexport const isValidSemver = (semver: any, loose = true): boolean => {\n return (\n (isString(semver) || isSemver(semver)) && valid(semver, loose) !== null\n );\n};\n\n/**\n * Check if a {@link Range} string is valid\n *\n * @remarks\n * If you're looking for type checking, please use the {@link isRange} function.\n *\n * @param range - The range string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the range string is valid\n */\nexport const isValidRange = (range: any, loose = true): boolean => {\n return (\n (isString(range) || isRange(range)) && validRange(range, loose) !== null\n );\n};\n\n/**\n * Check if a {@link SemVer} or {@link Range} string is valid\n *\n * @param version - The semver string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string is valid\n */\nexport const isValidVersion = (\n version: string | SemVer | Range | null | undefined,\n loose = true\n) => {\n return isValidSemver(version, loose) || isValidRange(version, loose);\n};\n\n/**\n * Check if a semver string satisfies a range\n *\n * @param version - The semver string to check\n * @param range - The range to check against\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string satisfies the range\n */\nexport const satisfiesVersion = (\n version: string | SemVer | null | undefined,\n range: string | Range | null | undefined,\n loose = true\n) => {\n if (\n !version ||\n !range ||\n !isValidSemver(version, loose) ||\n !isValidRange(range, loose)\n ) {\n return false;\n }\n\n return satisfies(version, range, { loose });\n};\n\n/**\n * Check if a string is a valid relative version keyword\n *\n * @param val - The string to check\n * @returns Whether the string is a valid relative version keyword\n */\nexport const isRelativeVersionKeyword = (val: string): val is ReleaseType => {\n return RELEASE_TYPES.includes(val as ReleaseType);\n};\n\n/**\n * Derive a new semver version from the current version and a version specifier\n *\n * @param currentSemverVersion - The current semver version\n * @param semverSpecifier - The semver specifier to use\n * @param preid - The pre-release identifier to use\n * @returns The derived new semver version\n */\nexport const deriveNewSemverVersion = (\n currentSemverVersion: string,\n semverSpecifier: string,\n preid?: string\n) => {\n if (!valid(currentSemverVersion)) {\n throw new Error(\n `Invalid semver version \"${currentSemverVersion}\" provided.`\n );\n }\n\n let newVersion = semverSpecifier;\n\n if (isRelativeVersionKeyword(semverSpecifier)) {\n // Derive the new version from the current version combined with the new version specifier.\n const derivedVersion = inc(currentSemverVersion, semverSpecifier, preid!);\n\n if (!derivedVersion) {\n throw new Error(\n `Unable to derive new version from current version \"${currentSemverVersion}\" and version specifier \"${semverSpecifier}\"`\n );\n }\n newVersion = derivedVersion;\n } else if (!valid(semverSpecifier)) {\n // Ensure the new version specifier is a valid semver version, given it is not a valid semver keyword\n throw new Error(\n `Invalid semver version specifier \"${semverSpecifier}\" provided. Please provide either a valid semver version or a valid semver version keyword.`\n );\n }\n\n return newVersion;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"semver-fns.mjs","names":[],"sources":["../src/semver-fns.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { CoerceOptions, Range, ReleaseType, SemVer } from \"semver\";\nimport {\n RELEASE_TYPES,\n coerce,\n inc,\n parse,\n satisfies,\n valid,\n validRange\n} from \"semver\";\n\n/**\n * Parse a semver string into a SemVer object\n *\n * @param semver - The semver string to parse\n * @param loose - Whether to use loose parsing\n * @returns The parsed SemVer object\n */\nexport const parseVersion = (semver: string, loose = true) =>\n parse(semver, loose);\n\n/**\n * Coerce a version string into a valid SemVer string\n *\n * @param version - The version string or number or {@link SemVer} to coerce\n * @param options - Options to use when coercing the version\n * @returns The coerced SemVer string or null if invalid\n */\nexport const coerceVersion = (\n version: string | number | SemVer | null | undefined,\n options?: CoerceOptions\n): SemVer | null => {\n return coerce(version, options);\n};\n\n/**\n * Type check for {@link SemVer}\n *\n * @param val - The value to check\n * @returns Whether the value is a valid {@link SemVer}\n */\nexport const isSemver = (val: any): val is SemVer => {\n return isObject(val) && \"version\" in val;\n};\n\n/**\n * Type check for {@link Range}\n *\n * @param val - The value to check\n * @returns Whether the value is a valid {@link Range}\n */\nexport const isRange = (val: any): val is Range => {\n return isObject(val) && \"range\" in val;\n};\n\n/**\n * Check if a {@link SemVer} string is valid\n *\n * @remarks\n * If you're looking for type checking, please use the {@link isSemver} function.\n *\n * @param semver - The semver string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string is valid\n */\nexport const isValidSemver = (semver: any, loose = true): boolean => {\n return (\n (isString(semver) || isSemver(semver)) && valid(semver, loose) !== null\n );\n};\n\n/**\n * Check if a {@link Range} string is valid\n *\n * @remarks\n * If you're looking for type checking, please use the {@link isRange} function.\n *\n * @param range - The range string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the range string is valid\n */\nexport const isValidRange = (range: any, loose = true): boolean => {\n return (\n (isString(range) || isRange(range)) && validRange(range, loose) !== null\n );\n};\n\n/**\n * Check if a {@link SemVer} or {@link Range} string is valid\n *\n * @param version - The semver string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string is valid\n */\nexport const isValidVersion = (\n version: string | SemVer | Range | null | undefined,\n loose = true\n) => {\n return isValidSemver(version, loose) || isValidRange(version, loose);\n};\n\n/**\n * Check if a semver string satisfies a range\n *\n * @param version - The semver string to check\n * @param range - The range to check against\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string satisfies the range\n */\nexport const satisfiesVersion = (\n version: string | SemVer | null | undefined,\n range: string | Range | null | undefined,\n loose = true\n) => {\n if (\n !version ||\n !range ||\n !isValidSemver(version, loose) ||\n !isValidRange(range, loose)\n ) {\n return false;\n }\n\n return satisfies(version, range, { loose });\n};\n\n/**\n * Check if a string is a valid relative version keyword\n *\n * @param val - The string to check\n * @returns Whether the string is a valid relative version keyword\n */\nexport const isRelativeVersionKeyword = (val: string): val is ReleaseType => {\n return RELEASE_TYPES.includes(val as ReleaseType);\n};\n\n/**\n * Derive a new semver version from the current version and a version specifier\n *\n * @param currentSemverVersion - The current semver version\n * @param semverSpecifier - The semver specifier to use\n * @param preid - The pre-release identifier to use\n * @returns The derived new semver version\n */\nexport const deriveNewSemverVersion = (\n currentSemverVersion: string,\n semverSpecifier: string,\n preid?: string\n) => {\n if (!valid(currentSemverVersion)) {\n throw new Error(\n `Invalid semver version \"${currentSemverVersion}\" provided.`\n );\n }\n\n let newVersion = semverSpecifier;\n\n if (isRelativeVersionKeyword(semverSpecifier)) {\n // Derive the new version from the current version combined with the new version specifier.\n const derivedVersion = inc(currentSemverVersion, semverSpecifier, preid!);\n\n if (!derivedVersion) {\n throw new Error(\n `Unable to derive new version from current version \"${currentSemverVersion}\" and version specifier \"${semverSpecifier}\"`\n );\n }\n newVersion = derivedVersion;\n } else if (!valid(semverSpecifier)) {\n // Ensure the new version specifier is a valid semver version, given it is not a valid semver keyword\n throw new Error(\n `Invalid semver version specifier \"${semverSpecifier}\" provided. Please provide either a valid semver version or a valid semver version keyword.`\n );\n }\n\n return newVersion;\n};\n"],"mappings":";;;;;;;;;;;;AAsCA,MAAa,gBAAgB,QAAgB,QAAQ,SACnD,MAAM,QAAQ,MAAM;;;;;;;;AAStB,MAAa,iBACX,SACA,YACkB;AAClB,QAAO,OAAO,SAAS,QAAQ;;;;;;;;AASjC,MAAa,YAAY,QAA4B;AACnD,QAAO,SAAS,IAAI,IAAI,aAAa;;;;;;;;AASvC,MAAa,WAAW,QAA2B;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