@oh-my-pi/pi-utils 13.12.7 → 13.12.9

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/dirs.ts +29 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@oh-my-pi/pi-utils",
4
- "version": "13.12.7",
4
+ "version": "13.12.9",
5
5
  "description": "Shared utilities for pi packages",
6
6
  "homepage": "https://github.com/can1357/oh-my-pi",
7
7
  "author": "Can Boluk",
package/src/dirs.ts CHANGED
@@ -47,6 +47,35 @@ function standardizeMacOSPath(p: string): string {
47
47
  return p;
48
48
  }
49
49
 
50
+ export function resolveEquivalentPath(inputPath: string): string {
51
+ const resolvedPath = path.resolve(inputPath);
52
+ try {
53
+ return fs.realpathSync(resolvedPath);
54
+ } catch {
55
+ return resolvedPath;
56
+ }
57
+ }
58
+
59
+ export function normalizePathForComparison(inputPath: string): string {
60
+ const resolvedPath = resolveEquivalentPath(inputPath);
61
+ return process.platform === "win32" ? resolvedPath.toLowerCase() : resolvedPath;
62
+ }
63
+
64
+ export function pathIsWithin(root: string, candidate: string): boolean {
65
+ const normalizedRoot = normalizePathForComparison(root);
66
+ const normalizedCandidate = normalizePathForComparison(candidate);
67
+ const relative = path.relative(normalizedRoot, normalizedCandidate);
68
+ return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
69
+ }
70
+
71
+ export function relativePathWithinRoot(root: string, candidate: string): string | null {
72
+ if (!pathIsWithin(root, candidate)) return null;
73
+ const normalizedRoot = normalizePathForComparison(root);
74
+ const normalizedCandidate = normalizePathForComparison(candidate);
75
+ const relative = path.relative(normalizedRoot, normalizedCandidate);
76
+ return relative || null;
77
+ }
78
+
50
79
  let projectDir = standardizeMacOSPath(process.cwd());
51
80
 
52
81
  /** Get the project directory. */