@stryke/path 0.8.2 → 0.10.0

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.
@@ -4,11 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.findFileExtension = findFileExtension;
7
+ exports.findFileExtensionSafe = findFileExtensionSafe;
7
8
  exports.findFileName = findFileName;
8
9
  exports.findFilePath = findFilePath;
9
10
  exports.findFolderName = findFolderName;
11
+ exports.hasFileExtension = hasFileExtension;
10
12
  exports.hasFileName = hasFileName;
11
13
  exports.hasFilePath = hasFilePath;
14
+ exports.hasFolderName = hasFolderName;
12
15
  exports.parsePath = parsePath;
13
16
  exports.relativePath = relativePath;
14
17
  exports.relativeToWorkspaceRoot = relativeToWorkspaceRoot;
@@ -21,26 +24,26 @@ var _correctPath = require("./correct-path.cjs");
21
24
  var _getWorkspaceRoot = require("./get-workspace-root.cjs");
22
25
  var _isFile = require("./is-file.cjs");
23
26
  var _joinPaths = require("./join-paths.cjs");
24
- function findFileName(t, e = {}) {
27
+ function findFileName(e, t = {}) {
25
28
  const {
26
29
  requireExtension: n = !1,
27
30
  withExtension: r = !0
28
- } = e,
29
- i = (0, _correctPath.normalizeWindowsPath)(t)?.split(t?.includes("\\") ? "\\" : "/")?.pop() ?? "";
31
+ } = t,
32
+ i = (0, _correctPath.normalizeWindowsPath)(e)?.split(e?.includes("\\") ? "\\" : "/")?.pop() ?? "";
30
33
  return n === !0 && !i.includes(".") ? _base.EMPTY_STRING : r === !1 && i.includes(".") ? i.substring(0, i.lastIndexOf(".")) || _base.EMPTY_STRING : i;
31
34
  }
32
- function findFilePath(t) {
33
- const e = (0, _correctPath.normalizeWindowsPath)(t),
34
- n = e.replace(findFileName(e, {
35
+ function findFilePath(e) {
36
+ const t = (0, _correctPath.normalizeWindowsPath)(e),
37
+ n = t.replace(findFileName(t, {
35
38
  requireExtension: !0
36
39
  }), "");
37
40
  return n === "/" ? n : n.replace(/\/$/, "");
38
41
  }
39
- function findFolderName(t) {
40
- const e = findFilePath(t).split("/");
42
+ function findFolderName(e) {
43
+ const t = findFilePath(e).split("/");
41
44
  let n = "";
42
- for (let r = e.length - 1; r >= 0; r--) {
43
- const i = e[r];
45
+ for (let r = t.length - 1; r >= 0; r--) {
46
+ const i = t[r];
44
47
  if (i) {
45
48
  n = i;
46
49
  break;
@@ -48,53 +51,62 @@ function findFolderName(t) {
48
51
  }
49
52
  return n ?? _base.EMPTY_STRING;
50
53
  }
51
- function findFileExtension(t) {
52
- if (t === "..") return "";
53
- const e = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(t));
54
- return e && e[1] || _base.EMPTY_STRING;
54
+ function findFileExtension(e) {
55
+ if (e.endsWith(".") || e.endsWith("/")) return;
56
+ const t = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(e));
57
+ return t && t[1] || void 0;
55
58
  }
56
- function hasFileName(t) {
57
- return !!findFileName(t);
59
+ function findFileExtensionSafe(e) {
60
+ return findFileExtension(e) ?? _base.EMPTY_STRING;
58
61
  }
59
- function hasFilePath(t) {
60
- return !!findFilePath(t);
62
+ function hasFileName(e) {
63
+ return !!findFileName(e);
61
64
  }
62
- function resolvePath(t, e = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
63
- const n = (0, _correctPath.normalizeWindowsPath)(t).split("/");
65
+ function hasFilePath(e) {
66
+ return !!findFilePath(e);
67
+ }
68
+ function hasFolderName(e) {
69
+ return !!findFolderName(e);
70
+ }
71
+ function hasFileExtension(e) {
72
+ return !!findFileExtension(e);
73
+ }
74
+ function resolvePath(e, t = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
75
+ const n = (0, _correctPath.normalizeWindowsPath)(e).split("/");
64
76
  let r = "",
65
77
  i = !1;
66
78
  for (let s = n.length - 1; s >= -1 && !i; s--) {
67
- const o = s >= 0 ? n[s] : e;
79
+ const o = s >= 0 ? n[s] : t;
68
80
  !o || o.length === 0 || (r = (0, _joinPaths.joinPaths)(o, r), i = (0, _isFile.isAbsolutePath)(o));
69
81
  }
70
82
  return r = (0, _correctPath.normalizeString)(r, !i), i && !(0, _isFile.isAbsolutePath)(r) ? `/${r}` : r.length > 0 ? r : ".";
71
83
  }
72
- function resolvePaths(...t) {
73
- return resolvePath((0, _joinPaths.joinPaths)(...t.map(e => (0, _correctPath.normalizeWindowsPath)(e))));
84
+ function resolvePaths(...e) {
85
+ return resolvePath((0, _joinPaths.joinPaths)(...e.map(t => (0, _correctPath.normalizeWindowsPath)(t))));
74
86
  }
75
- function relativePath(t, e, n = !1) {
76
- return (0, _nodePath.relative)(n !== !0 ? t.replace(/\/$/, "") : t, n !== !0 ? e.replace(/\/$/, "") : e);
87
+ function relativePath(e, t, n = !1) {
88
+ return (0, _nodePath.relative)(n !== !0 ? e.replace(/\/$/, "") : e, n !== !0 ? t.replace(/\/$/, "") : t);
77
89
  }
78
- function relativeToWorkspaceRoot(t) {
79
- return relativePath(t, (0, _getWorkspaceRoot.getWorkspaceRoot)());
90
+ function relativeToWorkspaceRoot(e) {
91
+ return relativePath(e, (0, _getWorkspaceRoot.getWorkspaceRoot)());
80
92
  }
81
- function parsePath(t) {
82
- const e = /^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g, "/") || "",
83
- n = (0, _correctPath.normalizeWindowsPath)(t),
93
+ function parsePath(e) {
94
+ const t = /^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g, "/") || "",
95
+ n = (0, _correctPath.normalizeWindowsPath)(e),
84
96
  r = n.replace(/\/$/, "").split("/").slice(0, -1);
85
97
  r.length === 1 && /^[A-Z]:$/i.test(r[0]) && (r[0] += "/");
86
98
  const i = findFolderName(n),
87
- s = r.join("/") || ((0, _isFile.isAbsolutePath)(t) ? "/" : "."),
88
- o = findFileExtension(t);
99
+ s = r.join("/") || ((0, _isFile.isAbsolutePath)(e) ? "/" : "."),
100
+ o = findFileExtensionSafe(e);
89
101
  return {
90
- root: e,
102
+ root: t,
91
103
  dir: s,
92
104
  base: i,
93
105
  ext: o,
94
106
  name: i.slice(0, i.length - o.length)
95
107
  };
96
108
  }
97
- function renameFile(t, e) {
98
- const n = parsePath(t);
99
- return (0, _joinPaths.joinPaths)(n.dir, e.includes(".") ? e : e + n.ext);
109
+ function renameFile(e, t) {
110
+ const n = parsePath(e);
111
+ return (0, _joinPaths.joinPaths)(n.dir, t.includes(".") ? t : t + n.ext);
100
112
  }
@@ -59,9 +59,16 @@ export declare function findFolderName(filePath: string): string;
59
59
  * Find the file extension from a file path.
60
60
  *
61
61
  * @param filePath - The file path to process
62
- * @returns The file extension
62
+ * @returns The file extension or undefined if no extension is found
63
63
  */
64
- export declare function findFileExtension(filePath: string): string;
64
+ export declare function findFileExtension(filePath: string): string | undefined;
65
+ /**
66
+ * Find the file extension from a file path or an empty string.
67
+ *
68
+ * @param filePath - The file path to process
69
+ * @returns The file extension or an empty string if no extension is found
70
+ */
71
+ export declare function findFileExtensionSafe(filePath: string): string;
65
72
  /**
66
73
  * Check if a file path has a file name.
67
74
  *
@@ -76,6 +83,20 @@ export declare function hasFileName(filePath: string): boolean;
76
83
  * @returns An indicator specifying if the file path has a file path
77
84
  */
78
85
  export declare function hasFilePath(filePath: string): boolean;
86
+ /**
87
+ * Check if a file path has a folder name.
88
+ *
89
+ * @param filePath - The file path to process
90
+ * @returns An indicator specifying if the file path has a folder name
91
+ */
92
+ export declare function hasFolderName(filePath: string): boolean;
93
+ /**
94
+ * Check if a file path has a file extension.
95
+ *
96
+ * @param filePath - The file path to process
97
+ * @returns An indicator specifying if the file path has a file extension
98
+ */
99
+ export declare function hasFileExtension(filePath: string): boolean;
79
100
  /**
80
101
  * Resolve the file path to an absolute path.
81
102
  *
@@ -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,e={}){const{requireExtension:n=!1,withExtension:r=!0}=e,i=l(t)?.split(t?.includes("\\")?"\\":"/")?.pop()??"";return n===!0&&!i.includes(".")?a:r===!1&&i.includes(".")?i.substring(0,i.lastIndexOf("."))||a:i}export function findFilePath(t){const e=l(t),n=e.replace(findFileName(e,{requireExtension:!0}),"");return n==="/"?n:n.replace(/\/$/,"")}export function findFolderName(t){const e=findFilePath(t).split("/");let n="";for(let r=e.length-1;r>=0;r--){const i=e[r];if(i){n=i;break}}return n??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 n=l(t).split("/");let r="",i=!1;for(let s=n.length-1;s>=-1&&!i;s--){const o=s>=0?n[s]:e;!o||o.length===0||(r=u(o,r),i=c(o))}return r=g(r,!i),i&&!c(r)?`/${r}`:r.length>0?r:"."}export function resolvePaths(...t){return resolvePath(u(...t.map(e=>l(e))))}export function relativePath(t,e,n=!1){return f(n!==!0?t.replace(/\/$/,""):t,n!==!0?e.replace(/\/$/,""):e)}export function relativeToWorkspaceRoot(t){return relativePath(t,p())}export function parsePath(t){const e=/^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g,"/")||"",n=l(t),r=n.replace(/\/$/,"").split("/").slice(0,-1);r.length===1&&/^[A-Z]:$/i.test(r[0])&&(r[0]+="/");const i=findFolderName(n),s=r.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 n=parsePath(t);return u(n.dir,e.includes(".")?e:e+n.ext)}
1
+ import{EMPTY_STRING as a}from"@stryke/types/base";import{relative as p}from"node:path";import{normalizeString as g,normalizeWindowsPath as l}from"./correct-path";import{getWorkspaceRoot as f}from"./get-workspace-root";import{isAbsolutePath as u}from"./is-file";import{joinPaths as c}from"./join-paths";export function findFileName(e,t={}){const{requireExtension:n=!1,withExtension:r=!0}=t,i=l(e)?.split(e?.includes("\\")?"\\":"/")?.pop()??"";return n===!0&&!i.includes(".")?a:r===!1&&i.includes(".")?i.substring(0,i.lastIndexOf("."))||a:i}export function findFilePath(e){const t=l(e),n=t.replace(findFileName(t,{requireExtension:!0}),"");return n==="/"?n:n.replace(/\/$/,"")}export function findFolderName(e){const t=findFilePath(e).split("/");let n="";for(let r=t.length-1;r>=0;r--){const i=t[r];if(i){n=i;break}}return n??a}export function findFileExtension(e){if(e.endsWith(".")||e.endsWith("/"))return;const t=/.(\.[^./]+|\.)$/.exec(l(e));return t&&t[1]||void 0}export function findFileExtensionSafe(e){return findFileExtension(e)??a}export function hasFileName(e){return!!findFileName(e)}export function hasFilePath(e){return!!findFilePath(e)}export function hasFolderName(e){return!!findFolderName(e)}export function hasFileExtension(e){return!!findFileExtension(e)}export function resolvePath(e,t=f()){const n=l(e).split("/");let r="",i=!1;for(let s=n.length-1;s>=-1&&!i;s--){const o=s>=0?n[s]:t;!o||o.length===0||(r=c(o,r),i=u(o))}return r=g(r,!i),i&&!u(r)?`/${r}`:r.length>0?r:"."}export function resolvePaths(...e){return resolvePath(c(...e.map(t=>l(t))))}export function relativePath(e,t,n=!1){return p(n!==!0?e.replace(/\/$/,""):e,n!==!0?t.replace(/\/$/,""):t)}export function relativeToWorkspaceRoot(e){return relativePath(e,f())}export function parsePath(e){const t=/^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g,"/")||"",n=l(e),r=n.replace(/\/$/,"").split("/").slice(0,-1);r.length===1&&/^[A-Z]:$/i.test(r[0])&&(r[0]+="/");const i=findFolderName(n),s=r.join("/")||(u(e)?"/":"."),o=findFileExtensionSafe(e);return{root:t,dir:s,base:i,ext:o,name:i.slice(0,i.length-o.length)}}export function renameFile(e,t){const n=parsePath(e);return c(n.dir,t.includes(".")?t:t+n.ext)}
@@ -3,24 +3,36 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getWorkspaceRoot = exports.getProjectRoot = void 0;
6
+ exports.WORKSPACE_ROOT_CONTENT = exports.PROJECT_ROOT_CONTENT = void 0;
7
+ exports.getProjectRoot = getProjectRoot;
8
+ exports.getWorkspaceRoot = getWorkspaceRoot;
9
+ exports.isProjectRoot = isProjectRoot;
10
+ exports.isWorkspaceRoot = isWorkspaceRoot;
7
11
  var _configTools = require("@storm-software/config-tools");
8
12
  var _getParentPath = require("./get-parent-path.cjs");
9
13
  var _isRootDir = require("./is-root-dir.cjs");
10
- const getWorkspaceRoot = (t = process.cwd()) => {
11
- if (process.env.STORM_WORKSPACE_ROOT || process.env.NX_WORKSPACE_ROOT_PATH) return process.env.STORM_WORKSPACE_ROOT || process.env.NX_WORKSPACE_ROOT_PATH;
12
- const r = (0, _configTools.findWorkspaceRootSafe)(t);
13
- if (r) return r;
14
- let o = (0, _getParentPath.getParentPath)(["package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock", "nx.json", "knip.json", "pnpm-workspace.yaml", "LICENSE", ".all-contributorsrc", ".whitesource", "syncpack.config.js", "syncpack.json", "socket.yaml", "lefthook.yaml", ".npmrc", ".log4brains.yml", ".huskyrc", ".husky", ".lintstagedrc", ".commitlintrc", "lefthook.yml", ".github", ".nx", ".vscode", "patches"], t);
15
- if (o) return o;
16
- for (o = t; o && !(0, _isRootDir.isSystemRoot)(o);) if (o = (0, _getParentPath.getParentPath)("storm.json", o, {
17
- skipCwd: !0
18
- }), o) return o;
19
- return t;
20
- },
21
- getProjectRoot = (t = process.cwd()) => {
22
- const r = (0, _getParentPath.getParentPath)(["project.json", "package.json", ".storm"], t);
23
- return r || t;
24
- };
25
- exports.getProjectRoot = getProjectRoot;
26
- exports.getWorkspaceRoot = getWorkspaceRoot;
14
+ const WORKSPACE_ROOT_CONTENT = exports.WORKSPACE_ROOT_CONTENT = ["package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock", "nx.json", "knip.json", "pnpm-workspace.yaml", "LICENSE", ".all-contributorsrc", ".whitesource", "syncpack.config.js", "syncpack.json", "socket.yaml", "lefthook.yaml", ".npmrc", ".log4brains.yml", ".huskyrc", ".husky", ".lintstagedrc", ".commitlintrc", "lefthook.yml", ".github", ".nx", ".vscode", "patches"],
15
+ PROJECT_ROOT_CONTENT = exports.PROJECT_ROOT_CONTENT = ["project.json", "package.json", ".storm"];
16
+ function getWorkspaceRoot(o = process.cwd()) {
17
+ if (process.env.STORM_WORKSPACE_ROOT || process.env.NX_WORKSPACE_ROOT_PATH) return process.env.STORM_WORKSPACE_ROOT || process.env.NX_WORKSPACE_ROOT_PATH;
18
+ const t = (0, _configTools.findWorkspaceRootSafe)(o);
19
+ if (t) return t;
20
+ let r = (0, _getParentPath.getParentPath)(WORKSPACE_ROOT_CONTENT, o);
21
+ if (r) return r;
22
+ for (r = o; r && !(0, _isRootDir.isSystemRoot)(r);) if (r = (0, _getParentPath.getParentPath)("storm-workspace.json", r, {
23
+ skipCwd: !0
24
+ }), r) return r;
25
+ return o;
26
+ }
27
+ function isWorkspaceRoot(o = process.cwd()) {
28
+ const t = getWorkspaceRoot(o);
29
+ return t ? t === o : !1;
30
+ }
31
+ function getProjectRoot(o = process.cwd()) {
32
+ const t = (0, _getParentPath.getParentPath)(PROJECT_ROOT_CONTENT, o);
33
+ return t || o;
34
+ }
35
+ function isProjectRoot(o = process.cwd()) {
36
+ const t = getProjectRoot(o);
37
+ return t ? t === o : !1;
38
+ }
@@ -1,14 +1,30 @@
1
+ export declare const WORKSPACE_ROOT_CONTENT: string[];
2
+ export declare const PROJECT_ROOT_CONTENT: string[];
1
3
  /**
2
4
  * Get the workspace root path
3
5
  *
4
6
  * @param dir - A directory to start the search from
5
7
  * @returns The workspace root path
6
8
  */
7
- export declare const getWorkspaceRoot: (dir?: string) => string;
9
+ export declare function getWorkspaceRoot(dir?: string): string;
10
+ /**
11
+ * Check if the given directory is the workspace root
12
+ *
13
+ * @param dir - A directory to check
14
+ * @returns True if the directory is the workspace root, false otherwise
15
+ */
16
+ export declare function isWorkspaceRoot(dir?: string): boolean;
8
17
  /**
9
18
  * Get the project root path
10
19
  *
11
20
  * @param dir - A directory to start the search from
12
21
  * @returns The project root path
13
22
  */
14
- export declare const getProjectRoot: (dir?: string) => string;
23
+ export declare function getProjectRoot(dir?: string): string;
24
+ /**
25
+ * Check if the given directory is the project root
26
+ *
27
+ * @param dir - A directory to check
28
+ * @returns True if the directory is the project root, false otherwise
29
+ */
30
+ export declare function isProjectRoot(dir?: string): boolean;
@@ -1 +1 @@
1
- import{findWorkspaceRootSafe as s}from"@storm-software/config-tools";import{getParentPath as e}from"./get-parent-path";import{isSystemRoot as c}from"./is-root-dir";export const getWorkspaceRoot=(t=process.cwd())=>{if(process.env.STORM_WORKSPACE_ROOT||process.env.NX_WORKSPACE_ROOT_PATH)return process.env.STORM_WORKSPACE_ROOT||process.env.NX_WORKSPACE_ROOT_PATH;const r=s(t);if(r)return r;let o=e(["package-lock.json","yarn.lock","pnpm-lock.yaml","bun.lock","nx.json","knip.json","pnpm-workspace.yaml","LICENSE",".all-contributorsrc",".whitesource","syncpack.config.js","syncpack.json","socket.yaml","lefthook.yaml",".npmrc",".log4brains.yml",".huskyrc",".husky",".lintstagedrc",".commitlintrc","lefthook.yml",".github",".nx",".vscode","patches"],t);if(o)return o;for(o=t;o&&!c(o);)if(o=e("storm.json",o,{skipCwd:!0}),o)return o;return t},getProjectRoot=(t=process.cwd())=>{const r=e(["project.json","package.json",".storm"],t);return r||t};
1
+ import{findWorkspaceRootSafe as e}from"@storm-software/config-tools";import{getParentPath as s}from"./get-parent-path";import{isSystemRoot as n}from"./is-root-dir";export const WORKSPACE_ROOT_CONTENT=["package-lock.json","yarn.lock","pnpm-lock.yaml","bun.lock","nx.json","knip.json","pnpm-workspace.yaml","LICENSE",".all-contributorsrc",".whitesource","syncpack.config.js","syncpack.json","socket.yaml","lefthook.yaml",".npmrc",".log4brains.yml",".huskyrc",".husky",".lintstagedrc",".commitlintrc","lefthook.yml",".github",".nx",".vscode","patches"],PROJECT_ROOT_CONTENT=["project.json","package.json",".storm"];export function getWorkspaceRoot(o=process.cwd()){if(process.env.STORM_WORKSPACE_ROOT||process.env.NX_WORKSPACE_ROOT_PATH)return process.env.STORM_WORKSPACE_ROOT||process.env.NX_WORKSPACE_ROOT_PATH;const t=e(o);if(t)return t;let r=s(WORKSPACE_ROOT_CONTENT,o);if(r)return r;for(r=o;r&&!n(r);)if(r=s("storm-workspace.json",r,{skipCwd:!0}),r)return r;return o}export function isWorkspaceRoot(o=process.cwd()){const t=getWorkspaceRoot(o);return t?t===o:!1}export function getProjectRoot(o=process.cwd()){const t=s(PROJECT_ROOT_CONTENT,o);return t||o}export function isProjectRoot(o=process.cwd()){const t=getProjectRoot(o);return t?t===o:!1}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/path",
3
- "version": "0.8.2",
3
+ "version": "0.10.0",
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": {
@@ -259,5 +259,6 @@
259
259
  },
260
260
  "main": "./dist/index.cjs",
261
261
  "module": "./dist/index.mjs",
262
- "types": "./dist/index.d.ts"
262
+ "types": "./dist/index.d.ts",
263
+ "gitHead": "a5ed5552046d1a8b313d88aed4d8bce7f33caec8"
263
264
  }