@stryke/path 0.4.9 → 0.4.10
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/dist/file-path-fns.cjs +47 -54
- package/dist/file-path-fns.d.ts +6 -0
- package/dist/file-path-fns.mjs +1 -1
- package/package.json +1 -1
package/dist/file-path-fns.cjs
CHANGED
|
@@ -16,28 +16,29 @@ exports.renameFile = renameFile;
|
|
|
16
16
|
exports.resolvePath = resolvePath;
|
|
17
17
|
exports.resolvePaths = resolvePaths;
|
|
18
18
|
var _base = require("@stryke/types/base");
|
|
19
|
+
var _nodePath = require("node:path");
|
|
19
20
|
var _correctPath = require("./correct-path.cjs");
|
|
20
21
|
var _getWorkspaceRoot = require("./get-workspace-root.cjs");
|
|
21
22
|
var _isFile = require("./is-file.cjs");
|
|
22
23
|
var _joinPaths = require("./join-paths.cjs");
|
|
23
|
-
function findFileName(
|
|
24
|
-
requireExtension:
|
|
24
|
+
function findFileName(t, {
|
|
25
|
+
requireExtension: e,
|
|
25
26
|
withExtension: r
|
|
26
27
|
} = {}) {
|
|
27
|
-
const
|
|
28
|
-
return
|
|
28
|
+
const n = (0, _correctPath.normalizeWindowsPath)(t)?.split(t?.includes("\\") ? "\\" : "/")?.pop() ?? "";
|
|
29
|
+
return e === !0 && !n.includes(".") ? _base.EMPTY_STRING : r === !1 && n.includes(".") ? n.split(".").slice(-1).join(".") || _base.EMPTY_STRING : n;
|
|
29
30
|
}
|
|
30
|
-
function findFilePath(
|
|
31
|
-
const
|
|
32
|
-
return
|
|
31
|
+
function findFilePath(t) {
|
|
32
|
+
const e = (0, _correctPath.normalizeWindowsPath)(t);
|
|
33
|
+
return e.replace(findFileName(e, {
|
|
33
34
|
requireExtension: !0
|
|
34
35
|
}), "");
|
|
35
36
|
}
|
|
36
|
-
function findFolderName(
|
|
37
|
-
const
|
|
37
|
+
function findFolderName(t) {
|
|
38
|
+
const e = findFilePath(t).split("/");
|
|
38
39
|
let r = "";
|
|
39
|
-
for (let
|
|
40
|
-
const i = n
|
|
40
|
+
for (let n = e.length - 1; n >= 0; n--) {
|
|
41
|
+
const i = e[n];
|
|
41
42
|
if (i) {
|
|
42
43
|
r = i;
|
|
43
44
|
break;
|
|
@@ -45,61 +46,53 @@ function findFolderName(e) {
|
|
|
45
46
|
}
|
|
46
47
|
return r ?? _base.EMPTY_STRING;
|
|
47
48
|
}
|
|
48
|
-
function findFileExtension(
|
|
49
|
-
if (
|
|
50
|
-
const
|
|
51
|
-
return
|
|
49
|
+
function findFileExtension(t) {
|
|
50
|
+
if (t === "..") return "";
|
|
51
|
+
const e = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(t));
|
|
52
|
+
return e && e[1] || _base.EMPTY_STRING;
|
|
52
53
|
}
|
|
53
|
-
function hasFileName(
|
|
54
|
-
return !!findFileName(
|
|
54
|
+
function hasFileName(t) {
|
|
55
|
+
return !!findFileName(t);
|
|
55
56
|
}
|
|
56
|
-
function hasFilePath(
|
|
57
|
-
return !!findFilePath(
|
|
57
|
+
function hasFilePath(t) {
|
|
58
|
+
return !!findFilePath(t);
|
|
58
59
|
}
|
|
59
|
-
function resolvePath(
|
|
60
|
-
const r = (0, _correctPath.normalizeWindowsPath)(
|
|
61
|
-
let
|
|
60
|
+
function resolvePath(t, e = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
|
|
61
|
+
const r = (0, _correctPath.normalizeWindowsPath)(t).split("/");
|
|
62
|
+
let n = "",
|
|
62
63
|
i = !1;
|
|
63
|
-
for (let
|
|
64
|
-
const
|
|
65
|
-
!
|
|
64
|
+
for (let s = r.length - 1; s >= -1 && !i; s--) {
|
|
65
|
+
const o = s >= 0 ? r[s] : e;
|
|
66
|
+
!o || o.length === 0 || (n = (0, _joinPaths.joinPaths)(o, n), i = (0, _isFile.isAbsolutePath)(o));
|
|
66
67
|
}
|
|
67
|
-
return
|
|
68
|
+
return n = (0, _correctPath.normalizeString)(n, !i), i && !(0, _isFile.isAbsolutePath)(n) ? `/${n}` : n.length > 0 ? n : ".";
|
|
68
69
|
}
|
|
69
|
-
function resolvePaths(...
|
|
70
|
-
return resolvePath((0, _joinPaths.joinPaths)(...
|
|
70
|
+
function resolvePaths(...t) {
|
|
71
|
+
return resolvePath((0, _joinPaths.joinPaths)(...t.map(e => (0, _correctPath.normalizeWindowsPath)(e))));
|
|
71
72
|
}
|
|
72
|
-
function relativePath(
|
|
73
|
-
|
|
74
|
-
t = resolvePath(n.replace(/\/$/, "")).replace(/^\/([A-Z]:)?$/i, "$1").split("/");
|
|
75
|
-
if (t[0][1] === ":" && r[0][1] === ":" && r[0] !== t[0]) return t.join("/");
|
|
76
|
-
const i = [...r];
|
|
77
|
-
for (const o of i) {
|
|
78
|
-
if (t[0] !== o) break;
|
|
79
|
-
r.shift(), t.shift();
|
|
80
|
-
}
|
|
81
|
-
return [...r.map(() => ".."), ...t].join("/");
|
|
73
|
+
function relativePath(t, e) {
|
|
74
|
+
return (0, _nodePath.relative)(t.replace(/\/$/, ""), e.replace(/\/$/, ""));
|
|
82
75
|
}
|
|
83
|
-
function relativeToWorkspaceRoot(
|
|
84
|
-
return relativePath(
|
|
76
|
+
function relativeToWorkspaceRoot(t) {
|
|
77
|
+
return relativePath(t, (0, _getWorkspaceRoot.getWorkspaceRoot)());
|
|
85
78
|
}
|
|
86
|
-
function parsePath(
|
|
87
|
-
const
|
|
88
|
-
r = (0, _correctPath.normalizeWindowsPath)(
|
|
89
|
-
|
|
90
|
-
|
|
79
|
+
function parsePath(t) {
|
|
80
|
+
const e = /^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g, "/") || "",
|
|
81
|
+
r = (0, _correctPath.normalizeWindowsPath)(t),
|
|
82
|
+
n = r.replace(/\/$/, "").split("/").slice(0, -1);
|
|
83
|
+
n.length === 1 && /^[A-Z]:$/i.test(n[0]) && (n[0] += "/");
|
|
91
84
|
const i = findFolderName(r),
|
|
92
|
-
|
|
93
|
-
|
|
85
|
+
s = n.join("/") || ((0, _isFile.isAbsolutePath)(t) ? "/" : "."),
|
|
86
|
+
o = findFileExtension(t);
|
|
94
87
|
return {
|
|
95
|
-
root:
|
|
96
|
-
dir:
|
|
88
|
+
root: e,
|
|
89
|
+
dir: s,
|
|
97
90
|
base: i,
|
|
98
|
-
ext:
|
|
99
|
-
name: i.slice(0, i.length -
|
|
91
|
+
ext: o,
|
|
92
|
+
name: i.slice(0, i.length - o.length)
|
|
100
93
|
};
|
|
101
94
|
}
|
|
102
|
-
function renameFile(
|
|
103
|
-
const r = parsePath(
|
|
104
|
-
return (0, _joinPaths.joinPaths)(r.dir,
|
|
95
|
+
function renameFile(t, e) {
|
|
96
|
+
const r = parsePath(t);
|
|
97
|
+
return (0, _joinPaths.joinPaths)(r.dir, e.includes(".") ? e : e + r.ext);
|
|
105
98
|
}
|
package/dist/file-path-fns.d.ts
CHANGED
|
@@ -91,6 +91,12 @@ export declare function resolvePath(path: string, cwd?: string): string;
|
|
|
91
91
|
* @returns The resolved path
|
|
92
92
|
*/
|
|
93
93
|
export declare function resolvePaths(...paths: string[]): string;
|
|
94
|
+
/**
|
|
95
|
+
* Get the relative path from one file to another.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* This function wraps the `path.relative` function in Node's path module.
|
|
99
|
+
*/
|
|
94
100
|
export declare function relativePath(from: string, to: string): string;
|
|
95
101
|
/**
|
|
96
102
|
* Find the file path relative to the workspace root path.
|
package/dist/file-path-fns.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{EMPTY_STRING as a}from"@stryke/types/base";import{normalizeString as
|
|
1
|
+
import{EMPTY_STRING as a}from"@stryke/types/base";import{relative as f}from"node:path";import{normalizeString as g,normalizeWindowsPath as l}from"./correct-path";import{getWorkspaceRoot as p}from"./get-workspace-root";import{isAbsolutePath as c}from"./is-file";import{joinPaths as u}from"./join-paths";export function findFileName(t,{requireExtension:e,withExtension:r}={}){const n=l(t)?.split(t?.includes("\\")?"\\":"/")?.pop()??"";return e===!0&&!n.includes(".")?a:r===!1&&n.includes(".")?n.split(".").slice(-1).join(".")||a:n}export function findFilePath(t){const e=l(t);return e.replace(findFileName(e,{requireExtension:!0}),"")}export function findFolderName(t){const e=findFilePath(t).split("/");let r="";for(let n=e.length-1;n>=0;n--){const i=e[n];if(i){r=i;break}}return r??a}export function findFileExtension(t){if(t==="..")return"";const e=/.(\.[^./]+|\.)$/.exec(l(t));return e&&e[1]||a}export function hasFileName(t){return!!findFileName(t)}export function hasFilePath(t){return!!findFilePath(t)}export function resolvePath(t,e=p()){const r=l(t).split("/");let n="",i=!1;for(let s=r.length-1;s>=-1&&!i;s--){const o=s>=0?r[s]:e;!o||o.length===0||(n=u(o,n),i=c(o))}return n=g(n,!i),i&&!c(n)?`/${n}`:n.length>0?n:"."}export function resolvePaths(...t){return resolvePath(u(...t.map(e=>l(e))))}export function relativePath(t,e){return f(t.replace(/\/$/,""),e.replace(/\/$/,""))}export function relativeToWorkspaceRoot(t){return relativePath(t,p())}export function parsePath(t){const e=/^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g,"/")||"",r=l(t),n=r.replace(/\/$/,"").split("/").slice(0,-1);n.length===1&&/^[A-Z]:$/i.test(n[0])&&(n[0]+="/");const i=findFolderName(r),s=n.join("/")||(c(t)?"/":"."),o=findFileExtension(t);return{root:e,dir:s,base:i,ext:o,name:i.slice(0,i.length-o.length)}}export function renameFile(t,e){const r=parsePath(t);return u(r.dir,e.includes(".")?e:e+r.ext)}
|