@stryke/path 0.25.0 → 0.25.1
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 +7 -0
- package/dist/append.cjs +59 -1
- package/dist/append.mjs +57 -1
- package/dist/append.mjs.map +1 -1
- package/dist/asset-extensions.cjs +40 -1
- package/dist/asset-extensions.mjs +39 -1
- package/dist/asset-extensions.mjs.map +1 -1
- package/dist/common.cjs +34 -1
- package/dist/common.mjs +34 -1
- package/dist/common.mjs.map +1 -1
- package/dist/correct-path.cjs +176 -1
- package/dist/correct-path.mjs +169 -1
- package/dist/correct-path.mjs.map +1 -1
- package/dist/cwd.cjs +17 -1
- package/dist/cwd.mjs +16 -1
- package/dist/cwd.mjs.map +1 -1
- package/dist/delimiter.cjs +27 -1
- package/dist/delimiter.mjs +24 -1
- package/dist/delimiter.mjs.map +1 -1
- package/dist/file-path-fns.cjs +332 -1
- package/dist/file-path-fns.mjs +311 -1
- package/dist/file-path-fns.mjs.map +1 -1
- package/dist/find.cjs +24 -1
- package/dist/find.mjs +3 -1
- package/dist/glob-to-regex.cjs +97 -1
- package/dist/glob-to-regex.mjs +97 -1
- package/dist/glob-to-regex.mjs.map +1 -1
- package/dist/index.cjs +73 -1
- package/dist/index.mjs +17 -1
- package/dist/is-parent-path.cjs +32 -1
- package/dist/is-parent-path.mjs +32 -1
- package/dist/is-parent-path.mjs.map +1 -1
- package/dist/is-root-dir.cjs +14 -1
- package/dist/is-root-dir.mjs +13 -1
- package/dist/is-root-dir.mjs.map +1 -1
- package/dist/is-type.cjs +92 -1
- package/dist/is-type.mjs +87 -1
- package/dist/is-type.mjs.map +1 -1
- package/dist/join-paths.cjs +108 -1
- package/dist/join-paths.mjs +107 -1
- package/dist/join-paths.mjs.map +1 -1
- package/dist/join.cjs +4 -1
- package/dist/join.mjs +3 -1
- package/dist/normalize.cjs +10 -1
- package/dist/normalize.mjs +3 -1
- package/dist/regex.cjs +20 -1
- package/dist/regex.mjs +12 -1
- package/dist/regex.mjs.map +1 -1
- package/dist/replace.cjs +44 -1
- package/dist/replace.mjs +43 -1
- package/dist/replace.mjs.map +1 -1
- package/dist/resolve-parent-path.cjs +18 -1
- package/dist/resolve-parent-path.mjs +18 -1
- package/dist/resolve-parent-path.mjs.map +1 -1
- package/dist/slash.cjs +27 -1
- package/dist/slash.mjs +26 -1
- package/dist/slash.mjs.map +1 -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-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-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/types/src/base.cjs +6 -1
- package/dist/types/src/base.mjs +5 -1
- package/dist/types/src/base.mjs.map +1 -1
- package/package.json +5 -5
package/dist/file-path-fns.cjs
CHANGED
|
@@ -1 +1,332 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_cwd = require('./cwd.cjs');
|
|
2
|
+
const require_regex = require('./regex.cjs');
|
|
3
|
+
const require_is_type = require('./is-type.cjs');
|
|
4
|
+
const require_join_paths = require('./join-paths.cjs');
|
|
5
|
+
const require_correct_path = require('./correct-path.cjs');
|
|
6
|
+
const require_is_set_string = require('./type-checks/src/is-set-string.cjs');
|
|
7
|
+
const require_base = require('./types/src/base.cjs');
|
|
8
|
+
|
|
9
|
+
//#region src/file-path-fns.ts
|
|
10
|
+
/**
|
|
11
|
+
* Find the file name from a file path.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const fileName = findFileName("C:\\Users\\user\\Documents\\file.txt");
|
|
16
|
+
* // fileName = "file.txt"
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param filePath - The file path to process
|
|
20
|
+
* @param options - Options to control the file name extraction
|
|
21
|
+
* @returns The file name
|
|
22
|
+
*/
|
|
23
|
+
function findFileName(filePath, options = {}) {
|
|
24
|
+
const { requireExtension = false, withExtension = true } = options;
|
|
25
|
+
const result = require_correct_path.normalizeWindowsPath(filePath)?.split(filePath?.includes("\\") ? "\\" : "/")?.pop() ?? "";
|
|
26
|
+
if (requireExtension === true && !result.includes(".")) return require_base.EMPTY_STRING;
|
|
27
|
+
if (withExtension === false && result.includes(".")) return result.replace(`.${findFileExtension(result) ?? ""}`, "") || require_base.EMPTY_STRING;
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Find the full file path's directories from a file path.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* The functionality of this method is similar to the {@link path.dirname} function in Node's path module.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const folderPath = findFilePath("C:\\Users\\user\\Documents\\file.txt");
|
|
39
|
+
* // folderPath = "C:\\Users\\user\\Documents"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param filePath - The file path to process
|
|
43
|
+
* @param options - Options to control the file name extraction
|
|
44
|
+
* @returns The full file path's directories
|
|
45
|
+
*/
|
|
46
|
+
function findFilePath(filePath, options = {}) {
|
|
47
|
+
const normalizedPath = require_correct_path.normalizeWindowsPath(filePath);
|
|
48
|
+
const result = normalizedPath.replace(findFileName(normalizedPath, {
|
|
49
|
+
requireExtension: false,
|
|
50
|
+
...options
|
|
51
|
+
}), "");
|
|
52
|
+
return result === "/" ? result : result.replace(/\/$/, "");
|
|
53
|
+
}
|
|
54
|
+
const dirname = findFilePath;
|
|
55
|
+
/**
|
|
56
|
+
* Find the top most folder containing the file from a file path.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* The functionality of this method is similar to the {@link path.basename} function in Node's path module.
|
|
60
|
+
* If you're looking for the full path of the folder (for example: `C:\\Users\\user\\Documents` instead of just `Documents`) containing the file, use {@link findFilePath} instead.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const folderPath = findFolderName("C:\\Users\\user\\Documents\\file.txt");
|
|
64
|
+
* // folderPath = "Documents"
|
|
65
|
+
*
|
|
66
|
+
* @param filePath - The file path to process
|
|
67
|
+
* @param options - Options to control the file name extraction
|
|
68
|
+
* @returns The folder containing the file
|
|
69
|
+
*/
|
|
70
|
+
function findFolderName(filePath, options) {
|
|
71
|
+
const segments = findFilePath(filePath, options).split("/");
|
|
72
|
+
let lastSegment = "";
|
|
73
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
74
|
+
const val = segments[i];
|
|
75
|
+
if (val) {
|
|
76
|
+
lastSegment = val;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return lastSegment ?? require_base.EMPTY_STRING;
|
|
81
|
+
}
|
|
82
|
+
const basename = findFolderName;
|
|
83
|
+
/**
|
|
84
|
+
* Find the file extension from a file path.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* The functionality of this method is similar to the {@link path.extname} function in Node's path module.
|
|
88
|
+
* The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
89
|
+
*
|
|
90
|
+
* The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* findFileExtension("C:\\Users\\user\\Documents\\file.config.ts");
|
|
95
|
+
* // Returns "ts"
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param filePath - The file path to process
|
|
99
|
+
* @returns The file extension or undefined if no extension is found
|
|
100
|
+
*/
|
|
101
|
+
function findFileExtension(filePath) {
|
|
102
|
+
if (filePath.endsWith(".") || filePath.endsWith("/")) return;
|
|
103
|
+
const match = require_regex.FILE_EXTENSION_REGEX.exec(require_correct_path.normalizeWindowsPath(filePath));
|
|
104
|
+
return match && match.length > 0 && require_is_set_string.isSetString(match[0]) ? match[0].replace(".", "") : void 0;
|
|
105
|
+
}
|
|
106
|
+
const extname = findFileExtension;
|
|
107
|
+
/**
|
|
108
|
+
* Find the file extension including the `"."` character prefix from a file path.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* The file extension is the part of the file name that comes after (and including) the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
112
|
+
*
|
|
113
|
+
* The returned extension **will** include the dot, for example `.txt` or `.js` instead of `txt` or `js`.
|
|
114
|
+
*
|
|
115
|
+
* @param filePath - The file path to process
|
|
116
|
+
* @returns The file extension (including the `"."` prefix) or undefined if no extension is found
|
|
117
|
+
*/
|
|
118
|
+
function findFileDotExtension(filePath) {
|
|
119
|
+
const ext = findFileExtension(filePath);
|
|
120
|
+
return ext ? `.${ext}` : void 0;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Find the file extension from a file path or an empty string.
|
|
124
|
+
*
|
|
125
|
+
* @remarks
|
|
126
|
+
* The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
127
|
+
*
|
|
128
|
+
* The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
|
|
129
|
+
*
|
|
130
|
+
* @param filePath - The file path to process
|
|
131
|
+
* @returns The file extension or an empty string if no extension is found
|
|
132
|
+
*/
|
|
133
|
+
function findFileExtensionSafe(filePath) {
|
|
134
|
+
return findFileExtension(filePath) ?? require_base.EMPTY_STRING;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Find the file extension including the `"."` character prefix from a file path or an empty string.
|
|
138
|
+
*
|
|
139
|
+
* @remarks
|
|
140
|
+
* The file extension is the part of the file name that comes after (and including) the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
141
|
+
*
|
|
142
|
+
* The returned extension **will** include the dot, for example `.txt` or `.js` instead of `txt` or `js`.
|
|
143
|
+
*
|
|
144
|
+
* @param filePath - The file path to process
|
|
145
|
+
* @returns The file extension (including the `"."` prefix) or an empty string if no extension is found
|
|
146
|
+
*/
|
|
147
|
+
function findFileDotExtensionSafe(filePath) {
|
|
148
|
+
const ext = findFileExtension(filePath);
|
|
149
|
+
return ext ? `.${ext}` : "";
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if a file path has a file name.
|
|
153
|
+
*
|
|
154
|
+
* @param filePath - The file path to process
|
|
155
|
+
* @returns An indicator specifying if the file path has a file name
|
|
156
|
+
*/
|
|
157
|
+
function hasFileName(filePath) {
|
|
158
|
+
return Boolean(findFileName(filePath));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Check if a file path has a file path.
|
|
162
|
+
*
|
|
163
|
+
* @param filePath - The file path to process
|
|
164
|
+
* @returns An indicator specifying if the file path has a file path
|
|
165
|
+
*/
|
|
166
|
+
function hasFilePath(filePath) {
|
|
167
|
+
return Boolean(findFilePath(filePath));
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Check if a file path has a folder name.
|
|
171
|
+
*
|
|
172
|
+
* @param filePath - The file path to process
|
|
173
|
+
* @returns An indicator specifying if the file path has a folder name
|
|
174
|
+
*/
|
|
175
|
+
function hasFolderName(filePath) {
|
|
176
|
+
return Boolean(findFolderName(filePath));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Check if a file path has a file extension.
|
|
180
|
+
*
|
|
181
|
+
* @param filePath - The file path to process
|
|
182
|
+
* @returns An indicator specifying if the file path has a file extension
|
|
183
|
+
*/
|
|
184
|
+
function hasFileExtension(filePath) {
|
|
185
|
+
return Boolean(findFileExtension(filePath));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Resolve the file path to an absolute path.
|
|
189
|
+
*
|
|
190
|
+
* @param path - The path to resolve
|
|
191
|
+
* @param cwd - The current working directory
|
|
192
|
+
* @returns The resolved path
|
|
193
|
+
*/
|
|
194
|
+
function resolvePath(path, cwd$1 = require_cwd.cwd()) {
|
|
195
|
+
const paths = require_correct_path.normalizeWindowsPath(path).split("/");
|
|
196
|
+
let resolvedPath = "";
|
|
197
|
+
let resolvedAbsolute = false;
|
|
198
|
+
for (let index = paths.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
199
|
+
const path$1 = index >= 0 ? paths[index] : cwd$1;
|
|
200
|
+
if (!path$1 || path$1.length === 0) continue;
|
|
201
|
+
resolvedPath = require_join_paths.joinPaths(path$1, resolvedPath);
|
|
202
|
+
resolvedAbsolute = require_is_type.isAbsolutePath(path$1);
|
|
203
|
+
}
|
|
204
|
+
resolvedPath = require_correct_path.normalizeString(resolvedPath, !resolvedAbsolute);
|
|
205
|
+
if (resolvedAbsolute && !require_is_type.isAbsolutePath(resolvedPath)) return `/${resolvedPath}`;
|
|
206
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
207
|
+
}
|
|
208
|
+
function resolve(...paths) {
|
|
209
|
+
paths = paths.map((argument) => require_correct_path.normalizeWindowsPath(argument));
|
|
210
|
+
let resolvedPath = "";
|
|
211
|
+
let resolvedAbsolute = false;
|
|
212
|
+
for (let index = paths.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
213
|
+
const path = index >= 0 ? paths[index] : require_cwd.cwd();
|
|
214
|
+
if (!path || path.length === 0) continue;
|
|
215
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
216
|
+
resolvedAbsolute = require_is_type.isAbsolute(path);
|
|
217
|
+
}
|
|
218
|
+
resolvedPath = require_correct_path.normalizeString(resolvedPath, !resolvedAbsolute);
|
|
219
|
+
if (resolvedAbsolute && !require_is_type.isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
220
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Resolve the file path to an absolute path.
|
|
224
|
+
*
|
|
225
|
+
* @param paths - The paths to resolve
|
|
226
|
+
* @returns The resolved path
|
|
227
|
+
*/
|
|
228
|
+
function resolvePaths(...paths) {
|
|
229
|
+
return resolvePath(require_join_paths.joinPaths(...paths.map((path) => require_correct_path.normalizeWindowsPath(path))));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Get the relative path from one file to another.
|
|
233
|
+
*
|
|
234
|
+
* @remarks
|
|
235
|
+
* This function is similar to the `path.relative` function in Node's path module.
|
|
236
|
+
*
|
|
237
|
+
* @param from - The base path to start from
|
|
238
|
+
* @param to - The target path to resolve relative to the base path
|
|
239
|
+
* @returns The relative path from the base path to the target path
|
|
240
|
+
*/
|
|
241
|
+
function relative(from, to) {
|
|
242
|
+
const _from = resolve(from).replace(require_regex.ROOT_FOLDER_REGEX, "$1").split("/");
|
|
243
|
+
const _to = resolve(to).replace(require_regex.ROOT_FOLDER_REGEX, "$1").split("/");
|
|
244
|
+
if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) return _to.join("/");
|
|
245
|
+
const _fromCopy = [..._from];
|
|
246
|
+
for (const segment of _fromCopy) {
|
|
247
|
+
if (_to[0] !== segment) break;
|
|
248
|
+
_from.shift();
|
|
249
|
+
_to.shift();
|
|
250
|
+
}
|
|
251
|
+
return [..._from.map(() => ".."), ..._to].join("/");
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Get the relative path from one file to another.
|
|
255
|
+
*
|
|
256
|
+
* @remarks
|
|
257
|
+
* This function wraps the `path.relative` function in Node's path module.
|
|
258
|
+
*
|
|
259
|
+
* @param from - The base path to start from
|
|
260
|
+
* @param to - The target path to resolve relative to the base path
|
|
261
|
+
* @param withEndSlash - Whether to include a trailing slash at the end of the path
|
|
262
|
+
* @returns The relative path from the base path to the target path
|
|
263
|
+
*/
|
|
264
|
+
function relativePath(from, to, withEndSlash = false) {
|
|
265
|
+
return relative(withEndSlash !== true ? from.replace(/\/$/, "") : from, withEndSlash !== true ? to.replace(/\/$/, "") : to);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Find the file path relative to the workspace root path.
|
|
269
|
+
*
|
|
270
|
+
* @param filePath - The file path to process
|
|
271
|
+
* @returns The resolved file path
|
|
272
|
+
*/
|
|
273
|
+
function relativeToCurrentDir(filePath) {
|
|
274
|
+
return relativePath(filePath, require_cwd.cwd());
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Check if the path is a relative path.
|
|
278
|
+
*
|
|
279
|
+
* @param path - The path to check
|
|
280
|
+
* @returns An indicator specifying if the path is a relative path
|
|
281
|
+
*/
|
|
282
|
+
function parsePath(path) {
|
|
283
|
+
const root = /^[/\\]|^[a-z]:[/\\]/i.exec(path)?.[0]?.replace(/\\/g, "/") || "";
|
|
284
|
+
const normalizedPath = require_correct_path.normalizeWindowsPath(path);
|
|
285
|
+
const segments = normalizedPath.replace(/\/$/, "").split("/").slice(0, -1);
|
|
286
|
+
if (segments.length === 1 && /^[A-Z]:$/i.test(segments[0])) segments[0] += "/";
|
|
287
|
+
const base = findFolderName(normalizedPath);
|
|
288
|
+
const dir = segments.join("/") || (require_is_type.isAbsolutePath(path) ? "/" : ".");
|
|
289
|
+
const ext = findFileExtensionSafe(path);
|
|
290
|
+
return {
|
|
291
|
+
root,
|
|
292
|
+
dir,
|
|
293
|
+
base,
|
|
294
|
+
ext,
|
|
295
|
+
name: base.slice(0, base.length - ext.length)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Rename the file name with a new name.
|
|
300
|
+
*
|
|
301
|
+
* @param filePath - The current file path being processed
|
|
302
|
+
* @param newFileName - The updated file name being processed
|
|
303
|
+
* @returns The modified or unmodified file path.
|
|
304
|
+
*/
|
|
305
|
+
function renameFile(filePath, newFileName) {
|
|
306
|
+
const file = parsePath(filePath);
|
|
307
|
+
return require_join_paths.joinPaths(file.dir, newFileName.includes(".") ? newFileName : newFileName + file.ext);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
//#endregion
|
|
311
|
+
exports.basename = basename;
|
|
312
|
+
exports.dirname = dirname;
|
|
313
|
+
exports.extname = extname;
|
|
314
|
+
exports.findFileDotExtension = findFileDotExtension;
|
|
315
|
+
exports.findFileDotExtensionSafe = findFileDotExtensionSafe;
|
|
316
|
+
exports.findFileExtension = findFileExtension;
|
|
317
|
+
exports.findFileExtensionSafe = findFileExtensionSafe;
|
|
318
|
+
exports.findFileName = findFileName;
|
|
319
|
+
exports.findFilePath = findFilePath;
|
|
320
|
+
exports.findFolderName = findFolderName;
|
|
321
|
+
exports.hasFileExtension = hasFileExtension;
|
|
322
|
+
exports.hasFileName = hasFileName;
|
|
323
|
+
exports.hasFilePath = hasFilePath;
|
|
324
|
+
exports.hasFolderName = hasFolderName;
|
|
325
|
+
exports.parsePath = parsePath;
|
|
326
|
+
exports.relative = relative;
|
|
327
|
+
exports.relativePath = relativePath;
|
|
328
|
+
exports.relativeToCurrentDir = relativeToCurrentDir;
|
|
329
|
+
exports.renameFile = renameFile;
|
|
330
|
+
exports.resolve = resolve;
|
|
331
|
+
exports.resolvePath = resolvePath;
|
|
332
|
+
exports.resolvePaths = resolvePaths;
|
package/dist/file-path-fns.mjs
CHANGED
|
@@ -1,2 +1,312 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { cwd } from "./cwd.mjs";
|
|
2
|
+
import { FILE_EXTENSION_REGEX, ROOT_FOLDER_REGEX } from "./regex.mjs";
|
|
3
|
+
import { isAbsolute, isAbsolutePath } from "./is-type.mjs";
|
|
4
|
+
import { joinPaths } from "./join-paths.mjs";
|
|
5
|
+
import { normalizeString, normalizeWindowsPath } from "./correct-path.mjs";
|
|
6
|
+
import { isSetString } from "./type-checks/src/is-set-string.mjs";
|
|
7
|
+
import { EMPTY_STRING } from "./types/src/base.mjs";
|
|
8
|
+
|
|
9
|
+
//#region src/file-path-fns.ts
|
|
10
|
+
/**
|
|
11
|
+
* Find the file name from a file path.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const fileName = findFileName("C:\\Users\\user\\Documents\\file.txt");
|
|
16
|
+
* // fileName = "file.txt"
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param filePath - The file path to process
|
|
20
|
+
* @param options - Options to control the file name extraction
|
|
21
|
+
* @returns The file name
|
|
22
|
+
*/
|
|
23
|
+
function findFileName(filePath, options = {}) {
|
|
24
|
+
const { requireExtension = false, withExtension = true } = options;
|
|
25
|
+
const result = normalizeWindowsPath(filePath)?.split(filePath?.includes("\\") ? "\\" : "/")?.pop() ?? "";
|
|
26
|
+
if (requireExtension === true && !result.includes(".")) return EMPTY_STRING;
|
|
27
|
+
if (withExtension === false && result.includes(".")) return result.replace(`.${findFileExtension(result) ?? ""}`, "") || EMPTY_STRING;
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Find the full file path's directories from a file path.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* The functionality of this method is similar to the {@link path.dirname} function in Node's path module.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const folderPath = findFilePath("C:\\Users\\user\\Documents\\file.txt");
|
|
39
|
+
* // folderPath = "C:\\Users\\user\\Documents"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param filePath - The file path to process
|
|
43
|
+
* @param options - Options to control the file name extraction
|
|
44
|
+
* @returns The full file path's directories
|
|
45
|
+
*/
|
|
46
|
+
function findFilePath(filePath, options = {}) {
|
|
47
|
+
const normalizedPath = normalizeWindowsPath(filePath);
|
|
48
|
+
const result = normalizedPath.replace(findFileName(normalizedPath, {
|
|
49
|
+
requireExtension: false,
|
|
50
|
+
...options
|
|
51
|
+
}), "");
|
|
52
|
+
return result === "/" ? result : result.replace(/\/$/, "");
|
|
53
|
+
}
|
|
54
|
+
const dirname = findFilePath;
|
|
55
|
+
/**
|
|
56
|
+
* Find the top most folder containing the file from a file path.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* The functionality of this method is similar to the {@link path.basename} function in Node's path module.
|
|
60
|
+
* If you're looking for the full path of the folder (for example: `C:\\Users\\user\\Documents` instead of just `Documents`) containing the file, use {@link findFilePath} instead.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const folderPath = findFolderName("C:\\Users\\user\\Documents\\file.txt");
|
|
64
|
+
* // folderPath = "Documents"
|
|
65
|
+
*
|
|
66
|
+
* @param filePath - The file path to process
|
|
67
|
+
* @param options - Options to control the file name extraction
|
|
68
|
+
* @returns The folder containing the file
|
|
69
|
+
*/
|
|
70
|
+
function findFolderName(filePath, options) {
|
|
71
|
+
const segments = findFilePath(filePath, options).split("/");
|
|
72
|
+
let lastSegment = "";
|
|
73
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
74
|
+
const val = segments[i];
|
|
75
|
+
if (val) {
|
|
76
|
+
lastSegment = val;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return lastSegment ?? EMPTY_STRING;
|
|
81
|
+
}
|
|
82
|
+
const basename = findFolderName;
|
|
83
|
+
/**
|
|
84
|
+
* Find the file extension from a file path.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* The functionality of this method is similar to the {@link path.extname} function in Node's path module.
|
|
88
|
+
* The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
89
|
+
*
|
|
90
|
+
* The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* findFileExtension("C:\\Users\\user\\Documents\\file.config.ts");
|
|
95
|
+
* // Returns "ts"
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param filePath - The file path to process
|
|
99
|
+
* @returns The file extension or undefined if no extension is found
|
|
100
|
+
*/
|
|
101
|
+
function findFileExtension(filePath) {
|
|
102
|
+
if (filePath.endsWith(".") || filePath.endsWith("/")) return;
|
|
103
|
+
const match = FILE_EXTENSION_REGEX.exec(normalizeWindowsPath(filePath));
|
|
104
|
+
return match && match.length > 0 && isSetString(match[0]) ? match[0].replace(".", "") : void 0;
|
|
105
|
+
}
|
|
106
|
+
const extname = findFileExtension;
|
|
107
|
+
/**
|
|
108
|
+
* Find the file extension including the `"."` character prefix from a file path.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* The file extension is the part of the file name that comes after (and including) the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
112
|
+
*
|
|
113
|
+
* The returned extension **will** include the dot, for example `.txt` or `.js` instead of `txt` or `js`.
|
|
114
|
+
*
|
|
115
|
+
* @param filePath - The file path to process
|
|
116
|
+
* @returns The file extension (including the `"."` prefix) or undefined if no extension is found
|
|
117
|
+
*/
|
|
118
|
+
function findFileDotExtension(filePath) {
|
|
119
|
+
const ext = findFileExtension(filePath);
|
|
120
|
+
return ext ? `.${ext}` : void 0;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Find the file extension from a file path or an empty string.
|
|
124
|
+
*
|
|
125
|
+
* @remarks
|
|
126
|
+
* The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
127
|
+
*
|
|
128
|
+
* The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
|
|
129
|
+
*
|
|
130
|
+
* @param filePath - The file path to process
|
|
131
|
+
* @returns The file extension or an empty string if no extension is found
|
|
132
|
+
*/
|
|
133
|
+
function findFileExtensionSafe(filePath) {
|
|
134
|
+
return findFileExtension(filePath) ?? EMPTY_STRING;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Find the file extension including the `"."` character prefix from a file path or an empty string.
|
|
138
|
+
*
|
|
139
|
+
* @remarks
|
|
140
|
+
* The file extension is the part of the file name that comes after (and including) the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
141
|
+
*
|
|
142
|
+
* The returned extension **will** include the dot, for example `.txt` or `.js` instead of `txt` or `js`.
|
|
143
|
+
*
|
|
144
|
+
* @param filePath - The file path to process
|
|
145
|
+
* @returns The file extension (including the `"."` prefix) or an empty string if no extension is found
|
|
146
|
+
*/
|
|
147
|
+
function findFileDotExtensionSafe(filePath) {
|
|
148
|
+
const ext = findFileExtension(filePath);
|
|
149
|
+
return ext ? `.${ext}` : "";
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if a file path has a file name.
|
|
153
|
+
*
|
|
154
|
+
* @param filePath - The file path to process
|
|
155
|
+
* @returns An indicator specifying if the file path has a file name
|
|
156
|
+
*/
|
|
157
|
+
function hasFileName(filePath) {
|
|
158
|
+
return Boolean(findFileName(filePath));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Check if a file path has a file path.
|
|
162
|
+
*
|
|
163
|
+
* @param filePath - The file path to process
|
|
164
|
+
* @returns An indicator specifying if the file path has a file path
|
|
165
|
+
*/
|
|
166
|
+
function hasFilePath(filePath) {
|
|
167
|
+
return Boolean(findFilePath(filePath));
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Check if a file path has a folder name.
|
|
171
|
+
*
|
|
172
|
+
* @param filePath - The file path to process
|
|
173
|
+
* @returns An indicator specifying if the file path has a folder name
|
|
174
|
+
*/
|
|
175
|
+
function hasFolderName(filePath) {
|
|
176
|
+
return Boolean(findFolderName(filePath));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Check if a file path has a file extension.
|
|
180
|
+
*
|
|
181
|
+
* @param filePath - The file path to process
|
|
182
|
+
* @returns An indicator specifying if the file path has a file extension
|
|
183
|
+
*/
|
|
184
|
+
function hasFileExtension(filePath) {
|
|
185
|
+
return Boolean(findFileExtension(filePath));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Resolve the file path to an absolute path.
|
|
189
|
+
*
|
|
190
|
+
* @param path - The path to resolve
|
|
191
|
+
* @param cwd - The current working directory
|
|
192
|
+
* @returns The resolved path
|
|
193
|
+
*/
|
|
194
|
+
function resolvePath(path, cwd$1 = cwd()) {
|
|
195
|
+
const paths = normalizeWindowsPath(path).split("/");
|
|
196
|
+
let resolvedPath = "";
|
|
197
|
+
let resolvedAbsolute = false;
|
|
198
|
+
for (let index = paths.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
199
|
+
const path$1 = index >= 0 ? paths[index] : cwd$1;
|
|
200
|
+
if (!path$1 || path$1.length === 0) continue;
|
|
201
|
+
resolvedPath = joinPaths(path$1, resolvedPath);
|
|
202
|
+
resolvedAbsolute = isAbsolutePath(path$1);
|
|
203
|
+
}
|
|
204
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
205
|
+
if (resolvedAbsolute && !isAbsolutePath(resolvedPath)) return `/${resolvedPath}`;
|
|
206
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
207
|
+
}
|
|
208
|
+
function resolve(...paths) {
|
|
209
|
+
paths = paths.map((argument) => normalizeWindowsPath(argument));
|
|
210
|
+
let resolvedPath = "";
|
|
211
|
+
let resolvedAbsolute = false;
|
|
212
|
+
for (let index = paths.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
213
|
+
const path = index >= 0 ? paths[index] : cwd();
|
|
214
|
+
if (!path || path.length === 0) continue;
|
|
215
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
216
|
+
resolvedAbsolute = isAbsolute(path);
|
|
217
|
+
}
|
|
218
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
219
|
+
if (resolvedAbsolute && !isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
220
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Resolve the file path to an absolute path.
|
|
224
|
+
*
|
|
225
|
+
* @param paths - The paths to resolve
|
|
226
|
+
* @returns The resolved path
|
|
227
|
+
*/
|
|
228
|
+
function resolvePaths(...paths) {
|
|
229
|
+
return resolvePath(joinPaths(...paths.map((path) => normalizeWindowsPath(path))));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Get the relative path from one file to another.
|
|
233
|
+
*
|
|
234
|
+
* @remarks
|
|
235
|
+
* This function is similar to the `path.relative` function in Node's path module.
|
|
236
|
+
*
|
|
237
|
+
* @param from - The base path to start from
|
|
238
|
+
* @param to - The target path to resolve relative to the base path
|
|
239
|
+
* @returns The relative path from the base path to the target path
|
|
240
|
+
*/
|
|
241
|
+
function relative(from, to) {
|
|
242
|
+
const _from = resolve(from).replace(ROOT_FOLDER_REGEX, "$1").split("/");
|
|
243
|
+
const _to = resolve(to).replace(ROOT_FOLDER_REGEX, "$1").split("/");
|
|
244
|
+
if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) return _to.join("/");
|
|
245
|
+
const _fromCopy = [..._from];
|
|
246
|
+
for (const segment of _fromCopy) {
|
|
247
|
+
if (_to[0] !== segment) break;
|
|
248
|
+
_from.shift();
|
|
249
|
+
_to.shift();
|
|
250
|
+
}
|
|
251
|
+
return [..._from.map(() => ".."), ..._to].join("/");
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Get the relative path from one file to another.
|
|
255
|
+
*
|
|
256
|
+
* @remarks
|
|
257
|
+
* This function wraps the `path.relative` function in Node's path module.
|
|
258
|
+
*
|
|
259
|
+
* @param from - The base path to start from
|
|
260
|
+
* @param to - The target path to resolve relative to the base path
|
|
261
|
+
* @param withEndSlash - Whether to include a trailing slash at the end of the path
|
|
262
|
+
* @returns The relative path from the base path to the target path
|
|
263
|
+
*/
|
|
264
|
+
function relativePath(from, to, withEndSlash = false) {
|
|
265
|
+
return relative(withEndSlash !== true ? from.replace(/\/$/, "") : from, withEndSlash !== true ? to.replace(/\/$/, "") : to);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Find the file path relative to the workspace root path.
|
|
269
|
+
*
|
|
270
|
+
* @param filePath - The file path to process
|
|
271
|
+
* @returns The resolved file path
|
|
272
|
+
*/
|
|
273
|
+
function relativeToCurrentDir(filePath) {
|
|
274
|
+
return relativePath(filePath, cwd());
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Check if the path is a relative path.
|
|
278
|
+
*
|
|
279
|
+
* @param path - The path to check
|
|
280
|
+
* @returns An indicator specifying if the path is a relative path
|
|
281
|
+
*/
|
|
282
|
+
function parsePath(path) {
|
|
283
|
+
const root = /^[/\\]|^[a-z]:[/\\]/i.exec(path)?.[0]?.replace(/\\/g, "/") || "";
|
|
284
|
+
const normalizedPath = normalizeWindowsPath(path);
|
|
285
|
+
const segments = normalizedPath.replace(/\/$/, "").split("/").slice(0, -1);
|
|
286
|
+
if (segments.length === 1 && /^[A-Z]:$/i.test(segments[0])) segments[0] += "/";
|
|
287
|
+
const base = findFolderName(normalizedPath);
|
|
288
|
+
const dir = segments.join("/") || (isAbsolutePath(path) ? "/" : ".");
|
|
289
|
+
const ext = findFileExtensionSafe(path);
|
|
290
|
+
return {
|
|
291
|
+
root,
|
|
292
|
+
dir,
|
|
293
|
+
base,
|
|
294
|
+
ext,
|
|
295
|
+
name: base.slice(0, base.length - ext.length)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Rename the file name with a new name.
|
|
300
|
+
*
|
|
301
|
+
* @param filePath - The current file path being processed
|
|
302
|
+
* @param newFileName - The updated file name being processed
|
|
303
|
+
* @returns The modified or unmodified file path.
|
|
304
|
+
*/
|
|
305
|
+
function renameFile(filePath, newFileName) {
|
|
306
|
+
const file = parsePath(filePath);
|
|
307
|
+
return joinPaths(file.dir, newFileName.includes(".") ? newFileName : newFileName + file.ext);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
//#endregion
|
|
311
|
+
export { basename, dirname, extname, findFileDotExtension, findFileDotExtensionSafe, findFileExtension, findFileExtensionSafe, findFileName, findFilePath, findFolderName, hasFileExtension, hasFileName, hasFilePath, hasFolderName, parsePath, relative, relativePath, relativeToCurrentDir, renameFile, resolve, resolvePath, resolvePaths };
|
|
2
312
|
//# sourceMappingURL=file-path-fns.mjs.map
|