@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.
@@ -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(e, {
24
- requireExtension: n,
24
+ function findFileName(t, {
25
+ requireExtension: e,
25
26
  withExtension: r
26
27
  } = {}) {
27
- const t = (0, _correctPath.normalizeWindowsPath)(e)?.split(e?.includes("\\") ? "\\" : "/")?.pop() ?? "";
28
- return n === !0 && !t.includes(".") ? _base.EMPTY_STRING : r === !1 && t.includes(".") ? t.split(".").slice(-1).join(".") || _base.EMPTY_STRING : t;
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(e) {
31
- const n = (0, _correctPath.normalizeWindowsPath)(e);
32
- return n.replace(findFileName(n, {
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(e) {
37
- const n = findFilePath(e).split("/");
37
+ function findFolderName(t) {
38
+ const e = findFilePath(t).split("/");
38
39
  let r = "";
39
- for (let t = n.length - 1; t >= 0; t--) {
40
- const i = n[t];
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(e) {
49
- if (e === "..") return "";
50
- const n = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(e));
51
- return n && n[1] || _base.EMPTY_STRING;
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(e) {
54
- return !!findFileName(e);
54
+ function hasFileName(t) {
55
+ return !!findFileName(t);
55
56
  }
56
- function hasFilePath(e) {
57
- return !!findFilePath(e);
57
+ function hasFilePath(t) {
58
+ return !!findFilePath(t);
58
59
  }
59
- function resolvePath(e, n = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
60
- const r = (0, _correctPath.normalizeWindowsPath)(e).split("/");
61
- let t = "",
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 o = r.length - 1; o >= -1 && !i; o--) {
64
- const s = o >= 0 ? r[o] : n;
65
- !s || s.length === 0 || (t = (0, _joinPaths.joinPaths)(s, t), i = (0, _isFile.isAbsolutePath)(s));
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 t = (0, _correctPath.normalizeString)(t, !i), i && !(0, _isFile.isAbsolutePath)(t) ? `/${t}` : t.length > 0 ? t : ".";
68
+ return n = (0, _correctPath.normalizeString)(n, !i), i && !(0, _isFile.isAbsolutePath)(n) ? `/${n}` : n.length > 0 ? n : ".";
68
69
  }
69
- function resolvePaths(...e) {
70
- return resolvePath((0, _joinPaths.joinPaths)(...e.map(n => (0, _correctPath.normalizeWindowsPath)(n))));
70
+ function resolvePaths(...t) {
71
+ return resolvePath((0, _joinPaths.joinPaths)(...t.map(e => (0, _correctPath.normalizeWindowsPath)(e))));
71
72
  }
72
- function relativePath(e, n) {
73
- const r = resolvePath(e.replace(/\/$/, "")).replace(/^\/([A-Z]:)?$/i, "$1").split("/"),
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(e) {
84
- return relativePath(e, (0, _getWorkspaceRoot.getWorkspaceRoot)());
76
+ function relativeToWorkspaceRoot(t) {
77
+ return relativePath(t, (0, _getWorkspaceRoot.getWorkspaceRoot)());
85
78
  }
86
- function parsePath(e) {
87
- const n = /^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g, "/") || "",
88
- r = (0, _correctPath.normalizeWindowsPath)(e),
89
- t = r.replace(/\/$/, "").split("/").slice(0, -1);
90
- t.length === 1 && /^[A-Z]:$/i.test(t[0]) && (t[0] += "/");
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
- o = t.join("/") || ((0, _isFile.isAbsolutePath)(e) ? "/" : "."),
93
- s = findFileExtension(e);
85
+ s = n.join("/") || ((0, _isFile.isAbsolutePath)(t) ? "/" : "."),
86
+ o = findFileExtension(t);
94
87
  return {
95
- root: n,
96
- dir: o,
88
+ root: e,
89
+ dir: s,
97
90
  base: i,
98
- ext: s,
99
- name: i.slice(0, i.length - s.length)
91
+ ext: o,
92
+ name: i.slice(0, i.length - o.length)
100
93
  };
101
94
  }
102
- function renameFile(e, n) {
103
- const r = parsePath(e);
104
- return (0, _joinPaths.joinPaths)(r.dir, n.includes(".") ? n : n + r.ext);
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
  }
@@ -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.
@@ -1 +1 @@
1
- import{EMPTY_STRING as a}from"@stryke/types/base";import{normalizeString as f,normalizeWindowsPath as l}from"./correct-path";import{getWorkspaceRoot as u}from"./get-workspace-root";import{isAbsolutePath as c}from"./is-file";import{joinPaths as p}from"./join-paths";export function findFileName(e,{requireExtension:n,withExtension:r}={}){const t=l(e)?.split(e?.includes("\\")?"\\":"/")?.pop()??"";return n===!0&&!t.includes(".")?a:r===!1&&t.includes(".")?t.split(".").slice(-1).join(".")||a:t}export function findFilePath(e){const n=l(e);return n.replace(findFileName(n,{requireExtension:!0}),"")}export function findFolderName(e){const n=findFilePath(e).split("/");let r="";for(let t=n.length-1;t>=0;t--){const i=n[t];if(i){r=i;break}}return r??a}export function findFileExtension(e){if(e==="..")return"";const n=/.(\.[^./]+|\.)$/.exec(l(e));return n&&n[1]||a}export function hasFileName(e){return!!findFileName(e)}export function hasFilePath(e){return!!findFilePath(e)}export function resolvePath(e,n=u()){const r=l(e).split("/");let t="",i=!1;for(let o=r.length-1;o>=-1&&!i;o--){const s=o>=0?r[o]:n;!s||s.length===0||(t=p(s,t),i=c(s))}return t=f(t,!i),i&&!c(t)?`/${t}`:t.length>0?t:"."}export function resolvePaths(...e){return resolvePath(p(...e.map(n=>l(n))))}export function relativePath(e,n){const r=resolvePath(e.replace(/\/$/,"")).replace(/^\/([A-Z]:)?$/i,"$1").split("/"),t=resolvePath(n.replace(/\/$/,"")).replace(/^\/([A-Z]:)?$/i,"$1").split("/");if(t[0][1]===":"&&r[0][1]===":"&&r[0]!==t[0])return t.join("/");const i=[...r];for(const o of i){if(t[0]!==o)break;r.shift(),t.shift()}return[...r.map(()=>".."),...t].join("/")}export function relativeToWorkspaceRoot(e){return relativePath(e,u())}export function parsePath(e){const n=/^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g,"/")||"",r=l(e),t=r.replace(/\/$/,"").split("/").slice(0,-1);t.length===1&&/^[A-Z]:$/i.test(t[0])&&(t[0]+="/");const i=findFolderName(r),o=t.join("/")||(c(e)?"/":"."),s=findFileExtension(e);return{root:n,dir:o,base:i,ext:s,name:i.slice(0,i.length-s.length)}}export function renameFile(e,n){const r=parsePath(e);return p(r.dir,n.includes(".")?n:n+r.ext)}
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)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/path",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
4
4
  "type": "module",
5
5
  "description": "A package containing various utilities that expand the functionality of NodeJs's built-in `path` module",
6
6
  "repository": {