@stryke/path 0.4.14 → 0.5.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.
@@ -21,12 +21,13 @@ var _correctPath = require("./correct-path.cjs");
21
21
  var _getWorkspaceRoot = require("./get-workspace-root.cjs");
22
22
  var _isFile = require("./is-file.cjs");
23
23
  var _joinPaths = require("./join-paths.cjs");
24
- function findFileName(t, {
25
- requireExtension: e,
26
- withExtension: r
27
- } = {}) {
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;
24
+ function findFileName(t, e = {}) {
25
+ const {
26
+ requireExtension: i = !1,
27
+ withExtension: n = !0
28
+ } = e,
29
+ r = (0, _correctPath.normalizeWindowsPath)(t)?.split(t?.includes("\\") ? "\\" : "/")?.pop() ?? "";
30
+ return i === !0 && !r.includes(".") ? _base.EMPTY_STRING : n === !1 && r.includes(".") ? r.split(".").slice(-1).join(".") || _base.EMPTY_STRING : r;
30
31
  }
31
32
  function findFilePath(t) {
32
33
  const e = (0, _correctPath.normalizeWindowsPath)(t);
@@ -36,15 +37,15 @@ function findFilePath(t) {
36
37
  }
37
38
  function findFolderName(t) {
38
39
  const e = findFilePath(t).split("/");
39
- let r = "";
40
+ let i = "";
40
41
  for (let n = e.length - 1; n >= 0; n--) {
41
- const i = e[n];
42
- if (i) {
43
- r = i;
42
+ const r = e[n];
43
+ if (r) {
44
+ i = r;
44
45
  break;
45
46
  }
46
47
  }
47
- return r ?? _base.EMPTY_STRING;
48
+ return i ?? _base.EMPTY_STRING;
48
49
  }
49
50
  function findFileExtension(t) {
50
51
  if (t === "..") return "";
@@ -58,14 +59,14 @@ function hasFilePath(t) {
58
59
  return !!findFilePath(t);
59
60
  }
60
61
  function resolvePath(t, e = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
61
- const r = (0, _correctPath.normalizeWindowsPath)(t).split("/");
62
+ const i = (0, _correctPath.normalizeWindowsPath)(t).split("/");
62
63
  let n = "",
63
- i = !1;
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));
64
+ r = !1;
65
+ for (let s = i.length - 1; s >= -1 && !r; s--) {
66
+ const o = s >= 0 ? i[s] : e;
67
+ !o || o.length === 0 || (n = (0, _joinPaths.joinPaths)(o, n), r = (0, _isFile.isAbsolutePath)(o));
67
68
  }
68
- return n = (0, _correctPath.normalizeString)(n, !i), i && !(0, _isFile.isAbsolutePath)(n) ? `/${n}` : n.length > 0 ? n : ".";
69
+ return n = (0, _correctPath.normalizeString)(n, !r), r && !(0, _isFile.isAbsolutePath)(n) ? `/${n}` : n.length > 0 ? n : ".";
69
70
  }
70
71
  function resolvePaths(...t) {
71
72
  return resolvePath((0, _joinPaths.joinPaths)(...t.map(e => (0, _correctPath.normalizeWindowsPath)(e))));
@@ -78,21 +79,21 @@ function relativeToWorkspaceRoot(t) {
78
79
  }
79
80
  function parsePath(t) {
80
81
  const e = /^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g, "/") || "",
81
- r = (0, _correctPath.normalizeWindowsPath)(t),
82
- n = r.replace(/\/$/, "").split("/").slice(0, -1);
82
+ i = (0, _correctPath.normalizeWindowsPath)(t),
83
+ n = i.replace(/\/$/, "").split("/").slice(0, -1);
83
84
  n.length === 1 && /^[A-Z]:$/i.test(n[0]) && (n[0] += "/");
84
- const i = findFolderName(r),
85
+ const r = findFolderName(i),
85
86
  s = n.join("/") || ((0, _isFile.isAbsolutePath)(t) ? "/" : "."),
86
87
  o = findFileExtension(t);
87
88
  return {
88
89
  root: e,
89
90
  dir: s,
90
- base: i,
91
+ base: r,
91
92
  ext: o,
92
- name: i.slice(0, i.length - o.length)
93
+ name: r.slice(0, r.length - o.length)
93
94
  };
94
95
  }
95
96
  function renameFile(t, e) {
96
- const r = parsePath(t);
97
- return (0, _joinPaths.joinPaths)(r.dir, e.includes(".") ? e : e + r.ext);
97
+ const i = parsePath(t);
98
+ return (0, _joinPaths.joinPaths)(i.dir, e.includes(".") ? e : e + i.ext);
98
99
  }
@@ -22,10 +22,10 @@ export interface FindFileNameOptions {
22
22
  * ```
23
23
  *
24
24
  * @param filePath - The file path to process
25
- * @param options - The options to use when processing the file name
25
+ * @param options - Options to control the file name extraction
26
26
  * @returns The file name
27
27
  */
28
- export declare function findFileName(filePath: string, { requireExtension, withExtension }?: FindFileNameOptions): string;
28
+ export declare function findFileName(filePath: string, options?: FindFileNameOptions): string;
29
29
  /**
30
30
  * Find the full file path's directories from a file path.
31
31
  *
@@ -1 +1 @@
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)}
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,e={}){const{requireExtension:i=!1,withExtension:n=!0}=e,r=l(t)?.split(t?.includes("\\")?"\\":"/")?.pop()??"";return i===!0&&!r.includes(".")?a:n===!1&&r.includes(".")?r.split(".").slice(-1).join(".")||a:r}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 i="";for(let n=e.length-1;n>=0;n--){const r=e[n];if(r){i=r;break}}return i??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 i=l(t).split("/");let n="",r=!1;for(let s=i.length-1;s>=-1&&!r;s--){const o=s>=0?i[s]:e;!o||o.length===0||(n=u(o,n),r=c(o))}return n=g(n,!r),r&&!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,"/")||"",i=l(t),n=i.replace(/\/$/,"").split("/").slice(0,-1);n.length===1&&/^[A-Z]:$/i.test(n[0])&&(n[0]+="/");const r=findFolderName(i),s=n.join("/")||(c(t)?"/":"."),o=findFileExtension(t);return{root:e,dir:s,base:r,ext:o,name:r.slice(0,r.length-o.length)}}export function renameFile(t,e){const i=parsePath(t);return u(i.dir,e.includes(".")?e:e+i.ext)}
@@ -7,20 +7,24 @@ exports.resolveParentPath = exports.getParentPath = void 0;
7
7
  var _filePathFns = require("./file-path-fns.cjs");
8
8
  var _isFile = require("./is-file.cjs");
9
9
  var _joinPaths = require("./join-paths.cjs");
10
- const resolveParentPath = r => (0, _filePathFns.resolvePaths)(r, ".."),
11
- getParentPath = (r, a, o) => {
12
- const p = o?.ignoreCase ?? !0,
13
- l = o?.skipCwd ?? !1,
14
- e = o?.targetType ?? "both";
15
- let t = a;
16
- l && (t = resolveParentPath(a));
17
- let n = Array.isArray(r) ? r : [r];
18
- for (p && (n = n.map(i => i.toLowerCase()));;) {
19
- const i = n.find(g => (0, _isFile.isFile)((0, _joinPaths.joinPaths)(t, g)) && (e === "file" || e === "both") || (0, _isFile.isDirectory)((0, _joinPaths.joinPaths)(t, g)) && (e === "directory" || e === "both"));
20
- if (i) return (0, _joinPaths.joinPaths)(t, i);
21
- const f = resolveParentPath(t);
22
- if (f === t) return;
23
- t = f;
10
+ const resolveParentPath = (r, i = 1) => {
11
+ let t = r;
12
+ for (let o = 0; o < i; o++) t = (0, _filePathFns.resolvePaths)(t, "..");
13
+ return t;
14
+ },
15
+ getParentPath = (r, i, t) => {
16
+ const o = t?.ignoreCase ?? !0,
17
+ l = t?.skipCwd ?? !1,
18
+ n = t?.targetType ?? "both";
19
+ let e = i;
20
+ l && (e = resolveParentPath(i));
21
+ let a = Array.isArray(r) ? r : [r];
22
+ for (o && (a = a.map(s => s.toLowerCase()));;) {
23
+ const s = a.find(g => (0, _isFile.isFile)((0, _joinPaths.joinPaths)(e, g)) && (n === "file" || n === "both") || (0, _isFile.isDirectory)((0, _joinPaths.joinPaths)(e, g)) && (n === "directory" || n === "both"));
24
+ if (s) return (0, _joinPaths.joinPaths)(e, s);
25
+ const p = resolveParentPath(e);
26
+ if (p === e) return;
27
+ e = p;
24
28
  }
25
29
  };
26
30
  exports.getParentPath = getParentPath;
@@ -2,9 +2,10 @@
2
2
  * Resolve the parent path of the provided path.
3
3
  *
4
4
  * @param path - The path to resolve.
5
+ * @param count - The number of parent directories to traverse.
5
6
  * @returns The parent path of the provided path.
6
7
  */
7
- export declare const resolveParentPath: (path: string) => string;
8
+ export declare const resolveParentPath: (path: string, count?: number) => string;
8
9
  /**
9
10
  * Options for the `getParentPath` function.
10
11
  */
@@ -1 +1 @@
1
- import{resolvePaths as h}from"./file-path-fns";import{isDirectory as P,isFile as c}from"./is-file";import{joinPaths as s}from"./join-paths";export const resolveParentPath=r=>h(r,".."),getParentPath=(r,a,o)=>{const p=o?.ignoreCase??!0,l=o?.skipCwd??!1,e=o?.targetType??"both";let t=a;l&&(t=resolveParentPath(a));let n=Array.isArray(r)?r:[r];for(p&&(n=n.map(i=>i.toLowerCase()));;){const i=n.find(g=>c(s(t,g))&&(e==="file"||e==="both")||P(s(t,g))&&(e==="directory"||e==="both"));if(i)return s(t,i);const f=resolveParentPath(t);if(f===t)return;t=f}};
1
+ import{resolvePaths as h}from"./file-path-fns";import{isDirectory as P,isFile as c}from"./is-file";import{joinPaths as f}from"./join-paths";export const resolveParentPath=(r,i=1)=>{let t=r;for(let o=0;o<i;o++)t=h(t,"..");return t},getParentPath=(r,i,t)=>{const o=t?.ignoreCase??!0,l=t?.skipCwd??!1,n=t?.targetType??"both";let e=i;l&&(e=resolveParentPath(i));let a=Array.isArray(r)?r:[r];for(o&&(a=a.map(s=>s.toLowerCase()));;){const s=a.find(g=>c(f(e,g))&&(n==="file"||n==="both")||P(f(e,g))&&(n==="directory"||n==="both"));if(s)return f(e,s);const p=resolveParentPath(e);if(p===e)return;e=p}};
package/dist/resolve.cjs CHANGED
@@ -12,6 +12,7 @@ exports.resolveSafeSync = resolveSafeSync;
12
12
  exports.resolveSync = resolveSync;
13
13
  var _mlly = require("mlly");
14
14
  var _correctPath = require("./correct-path.cjs");
15
+ var _filePathFns = require("./file-path-fns.cjs");
15
16
  var _getWorkspaceRoot = require("./get-workspace-root.cjs");
16
17
  var _joinPaths = require("./join-paths.cjs");
17
18
  async function resolve(r, e = {}) {
@@ -50,9 +51,9 @@ async function importModule(r) {
50
51
  }
51
52
  async function resolvePackage(r, e = {}) {
52
53
  let t = await resolveSafe((0, _joinPaths.joinPaths)(r, "package.json"), e);
53
- return t || (t = await resolveSafe((0, _joinPaths.joinPaths)(r, "index.js"), e), t || (t = await resolveSafe(r, e))), t;
54
+ return t || (t = await resolveSafe((0, _joinPaths.joinPaths)(r, "index.js"), e), t || (t = await resolveSafe(r, e))), t ? (0, _filePathFns.findFilePath)(t) : void 0;
54
55
  }
55
56
  async function resolvePackageSync(r, e = {}) {
56
57
  let t = resolveSafeSync((0, _joinPaths.joinPaths)(r, "package.json"), e);
57
- return t || (t = resolveSafeSync((0, _joinPaths.joinPaths)(r, "index.js"), e), t || (t = resolveSafeSync(r, e))), t;
58
+ return t || (t = resolveSafeSync((0, _joinPaths.joinPaths)(r, "index.js"), e), t || (t = resolveSafeSync(r, e))), t ? (0, _filePathFns.findFilePath)(t) : void 0;
58
59
  }
package/dist/resolve.d.ts CHANGED
@@ -12,7 +12,7 @@ export interface PackageResolvingOptions {
12
12
  /**
13
13
  * Resolve the path to a specified module
14
14
  *
15
- * @param name - The name of the module
15
+ * @param path - The path to the module
16
16
  * @param options - The options to use when resolving the module
17
17
  * @returns A promise for the path to the module
18
18
  */
@@ -20,7 +20,7 @@ export declare function resolve(path: string, options?: PackageResolvingOptions)
20
20
  /**
21
21
  * Resolve the path to a specified module
22
22
  *
23
- * @param name - The name of the module
23
+ * @param path - The path to the module
24
24
  * @param options - The options to use when resolving the module
25
25
  * @returns The path to the module or undefined
26
26
  */
@@ -49,16 +49,23 @@ export declare function resolveSafeSync(name: string, options?: PackageResolving
49
49
  */
50
50
  export declare function importModule<T = any>(path: string): Promise<T>;
51
51
  /**
52
- * Import a module from a specified path with error handling
52
+ * Resolve the path to a specified package asynchronously
53
53
  *
54
- * @param path - The path to the module
55
- * @returns The module or undefined
54
+ * @remarks
55
+ * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.
56
+ *
57
+ * @param name - The name of the module
58
+ * @returns A promise for the module or undefined
56
59
  */
57
60
  export declare function resolvePackage(name: string, options?: PackageResolvingOptions): Promise<string | undefined>;
58
61
  /**
59
- * Import a module from a specified path with error handling
62
+ * Resolve the path to a specified package synchronously
60
63
  *
61
- * @param path - The path to the module
64
+ * @remarks
65
+ * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself.
66
+ *
67
+ * @param name - The name of the module
68
+ * @param options - The options to use when resolving the module
62
69
  * @returns The module or undefined
63
70
  */
64
71
  export declare function resolvePackageSync(name: string, options?: PackageResolvingOptions): Promise<string | undefined>;
package/dist/resolve.mjs CHANGED
@@ -1 +1 @@
1
- import{interopDefault as i,resolvePath as c,resolvePathSync as p}from"mlly";import{correctPath as o}from"./correct-path";import{getWorkspaceRoot as a}from"./get-workspace-root";import{joinPaths as s}from"./join-paths";export async function resolve(r,e={}){const t=e.paths??[];t.length===0&&t.push(process.cwd());const n=a();return t.includes(n)||t.push(n),o(await c(r,{url:t}))}export function resolveSync(r,e={}){const t=e.paths??[];t.length===0&&t.push(process.cwd());const n=a();return t.includes(n)||t.push(n),o(p(r,{url:e.paths}))}export async function resolveSafe(r,e={}){try{return await resolve(r,e)}catch{return}}export function resolveSafeSync(r,e={}){try{return resolveSync(r,e)}catch{return}}export async function importModule(r){const e=await import(r);return e&&i(e)}export async function resolvePackage(r,e={}){let t=await resolveSafe(s(r,"package.json"),e);return t||(t=await resolveSafe(s(r,"index.js"),e),t||(t=await resolveSafe(r,e))),t}export async function resolvePackageSync(r,e={}){let t=resolveSafeSync(s(r,"package.json"),e);return t||(t=resolveSafeSync(s(r,"index.js"),e),t||(t=resolveSafeSync(r,e))),t}
1
+ import{interopDefault as c,resolvePath as p,resolvePathSync as f}from"mlly";import{correctPath as s}from"./correct-path";import{findFilePath as i}from"./file-path-fns";import{getWorkspaceRoot as a}from"./get-workspace-root";import{joinPaths as o}from"./join-paths";export async function resolve(r,e={}){const t=e.paths??[];t.length===0&&t.push(process.cwd());const n=a();return t.includes(n)||t.push(n),s(await p(r,{url:t}))}export function resolveSync(r,e={}){const t=e.paths??[];t.length===0&&t.push(process.cwd());const n=a();return t.includes(n)||t.push(n),s(f(r,{url:e.paths}))}export async function resolveSafe(r,e={}){try{return await resolve(r,e)}catch{return}}export function resolveSafeSync(r,e={}){try{return resolveSync(r,e)}catch{return}}export async function importModule(r){const e=await import(r);return e&&c(e)}export async function resolvePackage(r,e={}){let t=await resolveSafe(o(r,"package.json"),e);return t||(t=await resolveSafe(o(r,"index.js"),e),t||(t=await resolveSafe(r,e))),t?i(t):void 0}export async function resolvePackageSync(r,e={}){let t=resolveSafeSync(o(r,"package.json"),e);return t||(t=resolveSafeSync(o(r,"index.js"),e),t||(t=resolveSafeSync(r,e))),t?i(t):void 0}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/path",
3
- "version": "0.4.14",
3
+ "version": "0.5.1",
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": {
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "private": false,
12
12
  "dependencies": { "@storm-software/config-tools": "latest", "mlly": "1.7.4" },
13
- "devDependencies": { "@stryke/types": "^0.8.3", "@types/node": "^22.13.1" },
13
+ "devDependencies": { "@stryke/types": "^0.8.3", "@types/node": "^22.14.0" },
14
14
  "publishConfig": { "access": "public" },
15
15
  "sideEffects": false,
16
16
  "files": ["dist/**/*"],