@stryke/path 0.24.4 → 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.
Files changed (85) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/append.cjs +59 -1
  3. package/dist/append.mjs +57 -1
  4. package/dist/append.mjs.map +1 -1
  5. package/dist/asset-extensions.cjs +40 -1
  6. package/dist/asset-extensions.mjs +39 -1
  7. package/dist/asset-extensions.mjs.map +1 -1
  8. package/dist/common.cjs +34 -1
  9. package/dist/common.mjs +34 -1
  10. package/dist/common.mjs.map +1 -1
  11. package/dist/correct-path.cjs +176 -1
  12. package/dist/correct-path.mjs +169 -1
  13. package/dist/correct-path.mjs.map +1 -1
  14. package/dist/cwd.cjs +17 -1
  15. package/dist/cwd.mjs +16 -1
  16. package/dist/cwd.mjs.map +1 -1
  17. package/dist/delimiter.cjs +27 -1
  18. package/dist/delimiter.mjs +24 -1
  19. package/dist/delimiter.mjs.map +1 -1
  20. package/dist/file-path-fns.cjs +332 -1
  21. package/dist/file-path-fns.mjs +311 -1
  22. package/dist/file-path-fns.mjs.map +1 -1
  23. package/dist/find.cjs +24 -1
  24. package/dist/find.mjs +3 -1
  25. package/dist/glob-to-regex.cjs +97 -0
  26. package/dist/glob-to-regex.d.cts +50 -0
  27. package/dist/glob-to-regex.d.cts.map +1 -0
  28. package/dist/glob-to-regex.d.mts +50 -0
  29. package/dist/glob-to-regex.d.mts.map +1 -0
  30. package/dist/glob-to-regex.mjs +98 -0
  31. package/dist/glob-to-regex.mjs.map +1 -0
  32. package/dist/index.cjs +73 -1
  33. package/dist/index.d.cts +2 -1
  34. package/dist/index.d.mts +2 -1
  35. package/dist/index.mjs +17 -1
  36. package/dist/is-parent-path.cjs +32 -1
  37. package/dist/is-parent-path.mjs +32 -1
  38. package/dist/is-parent-path.mjs.map +1 -1
  39. package/dist/is-root-dir.cjs +14 -1
  40. package/dist/is-root-dir.mjs +13 -1
  41. package/dist/is-root-dir.mjs.map +1 -1
  42. package/dist/is-type.cjs +92 -1
  43. package/dist/is-type.mjs +87 -1
  44. package/dist/is-type.mjs.map +1 -1
  45. package/dist/join-paths.cjs +108 -1
  46. package/dist/join-paths.mjs +107 -1
  47. package/dist/join-paths.mjs.map +1 -1
  48. package/dist/join.cjs +4 -1
  49. package/dist/join.mjs +3 -1
  50. package/dist/normalize.cjs +10 -1
  51. package/dist/normalize.mjs +3 -1
  52. package/dist/regex.cjs +20 -1
  53. package/dist/regex.mjs +12 -1
  54. package/dist/regex.mjs.map +1 -1
  55. package/dist/replace.cjs +44 -1
  56. package/dist/replace.mjs +43 -1
  57. package/dist/replace.mjs.map +1 -1
  58. package/dist/resolve-parent-path.cjs +18 -1
  59. package/dist/resolve-parent-path.mjs +18 -1
  60. package/dist/resolve-parent-path.mjs.map +1 -1
  61. package/dist/slash.cjs +27 -1
  62. package/dist/slash.mjs +26 -1
  63. package/dist/slash.mjs.map +1 -1
  64. package/dist/type-checks/src/is-empty.cjs +20 -1
  65. package/dist/type-checks/src/is-empty.mjs +20 -1
  66. package/dist/type-checks/src/is-empty.mjs.map +1 -1
  67. package/dist/type-checks/src/is-null.cjs +12 -1
  68. package/dist/type-checks/src/is-null.mjs +11 -1
  69. package/dist/type-checks/src/is-null.mjs.map +1 -1
  70. package/dist/type-checks/src/is-set-string.cjs +20 -1
  71. package/dist/type-checks/src/is-set-string.mjs +20 -1
  72. package/dist/type-checks/src/is-set-string.mjs.map +1 -1
  73. package/dist/type-checks/src/is-set.cjs +19 -1
  74. package/dist/type-checks/src/is-set.mjs +19 -1
  75. package/dist/type-checks/src/is-set.mjs.map +1 -1
  76. package/dist/type-checks/src/is-string.cjs +12 -1
  77. package/dist/type-checks/src/is-string.mjs +11 -1
  78. package/dist/type-checks/src/is-string.mjs.map +1 -1
  79. package/dist/type-checks/src/is-undefined.cjs +8 -1
  80. package/dist/type-checks/src/is-undefined.mjs +7 -1
  81. package/dist/type-checks/src/is-undefined.mjs.map +1 -1
  82. package/dist/types/src/base.cjs +6 -1
  83. package/dist/types/src/base.mjs +5 -1
  84. package/dist/types/src/base.mjs.map +1 -1
  85. package/package.json +9 -5
@@ -1 +1,332 @@
1
- const e=require(`./cwd.cjs`),t=require(`./regex.cjs`),n=require(`./is-type.cjs`),r=require(`./join-paths.cjs`),i=require(`./correct-path.cjs`),a=require(`./type-checks/src/is-set-string.cjs`),o=require(`./types/src/base.cjs`);function s(e,t={}){let{requireExtension:n=!1,withExtension:r=!0}=t,a=i.normalizeWindowsPath(e)?.split(e?.includes(`\\`)?`\\`:`/`)?.pop()??``;return n===!0&&!a.includes(`.`)?o.EMPTY_STRING:r===!1&&a.includes(`.`)?a.replace(`.${f(a)??``}`,``)||o.EMPTY_STRING:a}function c(e,t={}){let n=i.normalizeWindowsPath(e),r=n.replace(s(n,{requireExtension:!1,...t}),``);return r===`/`?r:r.replace(/\/$/,``)}const l=c;function u(e,t){let n=c(e,t).split(`/`),r=``;for(let e=n.length-1;e>=0;e--){let t=n[e];if(t){r=t;break}}return r??o.EMPTY_STRING}const d=u;function f(e){if(e.endsWith(`.`)||e.endsWith(`/`))return;let n=t.FILE_EXTENSION_REGEX.exec(i.normalizeWindowsPath(e));return n&&n.length>0&&a.isSetString(n[0])?n[0].replace(`.`,``):void 0}const p=f;function m(e){let t=f(e);return t?`.${t}`:void 0}function h(e){return f(e)??o.EMPTY_STRING}function g(e){let t=f(e);return t?`.${t}`:``}function _(e){return!!s(e)}function v(e){return!!c(e)}function y(e){return!!u(e)}function b(e){return!!f(e)}function x(t,a=e.cwd()){let o=i.normalizeWindowsPath(t).split(`/`),s=``,c=!1;for(let e=o.length-1;e>=-1&&!c;e--){let t=e>=0?o[e]:a;!t||t.length===0||(s=r.joinPaths(t,s),c=n.isAbsolutePath(t))}return s=i.normalizeString(s,!c),c&&!n.isAbsolutePath(s)?`/${s}`:s.length>0?s:`.`}function S(...t){t=t.map(e=>i.normalizeWindowsPath(e));let r=``,a=!1;for(let i=t.length-1;i>=-1&&!a;i--){let o=i>=0?t[i]:e.cwd();!o||o.length===0||(r=`${o}/${r}`,a=n.isAbsolute(o))}return r=i.normalizeString(r,!a),a&&!n.isAbsolute(r)?`/${r}`:r.length>0?r:`.`}function C(...e){return x(r.joinPaths(...e.map(e=>i.normalizeWindowsPath(e))))}function w(e,n){let r=S(e).replace(t.ROOT_FOLDER_REGEX,`$1`).split(`/`),i=S(n).replace(t.ROOT_FOLDER_REGEX,`$1`).split(`/`);if(i[0][1]===`:`&&r[0][1]===`:`&&r[0]!==i[0])return i.join(`/`);let a=[...r];for(let e of a){if(i[0]!==e)break;r.shift(),i.shift()}return[...r.map(()=>`..`),...i].join(`/`)}function T(e,t,n=!1){return w(n===!0?e:e.replace(/\/$/,``),n===!0?t:t.replace(/\/$/,``))}function E(t){return T(t,e.cwd())}function D(e){let t=/^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g,`/`)||``,r=i.normalizeWindowsPath(e),a=r.replace(/\/$/,``).split(`/`).slice(0,-1);a.length===1&&/^[A-Z]:$/i.test(a[0])&&(a[0]+=`/`);let o=u(r),s=a.join(`/`)||(n.isAbsolutePath(e)?`/`:`.`),c=h(e);return{root:t,dir:s,base:o,ext:c,name:o.slice(0,o.length-c.length)}}function O(e,t){let n=D(e);return r.joinPaths(n.dir,t.includes(`.`)?t:t+n.ext)}exports.basename=d,exports.dirname=l,exports.extname=p,exports.findFileDotExtension=m,exports.findFileDotExtensionSafe=g,exports.findFileExtension=f,exports.findFileExtensionSafe=h,exports.findFileName=s,exports.findFilePath=c,exports.findFolderName=u,exports.hasFileExtension=b,exports.hasFileName=_,exports.hasFilePath=v,exports.hasFolderName=y,exports.parsePath=D,exports.relative=w,exports.relativePath=T,exports.relativeToCurrentDir=E,exports.renameFile=O,exports.resolve=S,exports.resolvePath=x,exports.resolvePaths=C;
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;
@@ -1,2 +1,312 @@
1
- import{cwd as e}from"./cwd.mjs";import{FILE_EXTENSION_REGEX as t,ROOT_FOLDER_REGEX as n}from"./regex.mjs";import{isAbsolute as r,isAbsolutePath as i}from"./is-type.mjs";import{joinPaths as a}from"./join-paths.mjs";import{normalizeString as o,normalizeWindowsPath as s}from"./correct-path.mjs";import{isSetString as c}from"./type-checks/src/is-set-string.mjs";import{EMPTY_STRING as l}from"./types/src/base.mjs";function u(e,t={}){let{requireExtension:n=!1,withExtension:r=!0}=t,i=s(e)?.split(e?.includes(`\\`)?`\\`:`/`)?.pop()??``;return n===!0&&!i.includes(`.`)?l:r===!1&&i.includes(`.`)?i.replace(`.${h(i)??``}`,``)||l:i}function d(e,t={}){let n=s(e),r=n.replace(u(n,{requireExtension:!1,...t}),``);return r===`/`?r:r.replace(/\/$/,``)}const f=d;function p(e,t){let n=d(e,t).split(`/`),r=``;for(let e=n.length-1;e>=0;e--){let t=n[e];if(t){r=t;break}}return r??l}const m=p;function h(e){if(e.endsWith(`.`)||e.endsWith(`/`))return;let n=t.exec(s(e));return n&&n.length>0&&c(n[0])?n[0].replace(`.`,``):void 0}const g=h;function _(e){let t=h(e);return t?`.${t}`:void 0}function v(e){return h(e)??l}function y(e){let t=h(e);return t?`.${t}`:``}function b(e){return!!u(e)}function x(e){return!!d(e)}function S(e){return!!p(e)}function C(e){return!!h(e)}function w(t,n=e()){let r=s(t).split(`/`),c=``,l=!1;for(let e=r.length-1;e>=-1&&!l;e--){let t=e>=0?r[e]:n;!t||t.length===0||(c=a(t,c),l=i(t))}return c=o(c,!l),l&&!i(c)?`/${c}`:c.length>0?c:`.`}function T(...t){t=t.map(e=>s(e));let n=``,i=!1;for(let a=t.length-1;a>=-1&&!i;a--){let o=a>=0?t[a]:e();!o||o.length===0||(n=`${o}/${n}`,i=r(o))}return n=o(n,!i),i&&!r(n)?`/${n}`:n.length>0?n:`.`}function E(...e){return w(a(...e.map(e=>s(e))))}function D(e,t){let r=T(e).replace(n,`$1`).split(`/`),i=T(t).replace(n,`$1`).split(`/`);if(i[0][1]===`:`&&r[0][1]===`:`&&r[0]!==i[0])return i.join(`/`);let a=[...r];for(let e of a){if(i[0]!==e)break;r.shift(),i.shift()}return[...r.map(()=>`..`),...i].join(`/`)}function O(e,t,n=!1){return D(n===!0?e:e.replace(/\/$/,``),n===!0?t:t.replace(/\/$/,``))}function k(t){return O(t,e())}function A(e){let t=/^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g,`/`)||``,n=s(e),r=n.replace(/\/$/,``).split(`/`).slice(0,-1);r.length===1&&/^[A-Z]:$/i.test(r[0])&&(r[0]+=`/`);let a=p(n),o=r.join(`/`)||(i(e)?`/`:`.`),c=v(e);return{root:t,dir:o,base:a,ext:c,name:a.slice(0,a.length-c.length)}}function j(e,t){let n=A(e);return a(n.dir,t.includes(`.`)?t:t+n.ext)}export{m as basename,f as dirname,g as extname,_ as findFileDotExtension,y as findFileDotExtensionSafe,h as findFileExtension,v as findFileExtensionSafe,u as findFileName,d as findFilePath,p as findFolderName,C as hasFileExtension,b as hasFileName,x as hasFilePath,S as hasFolderName,A as parsePath,D as relative,O as relativePath,k as relativeToCurrentDir,j as renameFile,T as resolve,w as resolvePath,E as resolvePaths};
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