MCPcopy Create free account
hub / github.com/code-pushup/cli / osAgnosticPath

Function osAgnosticPath

testing/test-utils/src/lib/utils/os-agnostic-paths.ts:43–95  ·  view source on GitHub ↗
(filePath?: string)

Source from the content-addressed store, hash-verified

41export function osAgnosticPath(filePath: string): string;
42export function osAgnosticPath(): undefined;
43export function osAgnosticPath(filePath?: string): string | undefined {
44 if (filePath == null) {
45 return filePath;
46 }
47 // prepare the path for comparison
48 // normalize path separators od cwd: "Users\\repo" => "Users/repo"
49 const osAgnosticCwd = process
50 .cwd()
51 .split(AGNOSTIC_PATH_SEP_REGEX)
52 .join(OS_AGNOSTIC_PATH_SEP);
53 // normalize path separators => "..\\folder\\repo.ts" => => "../folder/repo.ts"
54 const osAgnosticFilePath = filePath
55 .split(AGNOSTIC_PATH_SEP_REGEX)
56 .join(OS_AGNOSTIC_PATH_SEP);
57 // remove the current working directory for easier comparison
58 const osAgnosticPathWithoutCwd = osAgnosticFilePath
59 .replace(osAgnosticCwd, '')
60 // consider already agnostic paths
61 .replace(OS_AGNOSTIC_CWD, '');
62
63 // path is outside cwd (Users/repo/../my-folder/my-file.ts)
64 if (
65 osAgnosticPathWithoutCwd.startsWith(
66 `${OS_AGNOSTIC_PATH_SEP}..${OS_AGNOSTIC_PATH_SEP}`,
67 )
68 ) {
69 return osAgnosticPathWithoutCwd.slice(1); // remove the leading '/'
70 }
71
72 // path is at cwd (Users/repo/my-folder/my-file.ts)
73 if (
74 osAgnosticFilePath.startsWith(osAgnosticCwd) ||
75 osAgnosticFilePath.startsWith(OS_AGNOSTIC_CWD)
76 ) {
77 // Add a substitute for the current working directory
78 return `${OS_AGNOSTIC_CWD}${osAgnosticPathWithoutCwd}`;
79 }
80
81 // Notice: I kept the following conditions for documentation purposes
82
83 // path is absolute (/my-folder/my-file.ts)
84 if (osAgnosticPathWithoutCwd.startsWith(OS_AGNOSTIC_PATH_SEP)) {
85 return osAgnosticPathWithoutCwd;
86 }
87
88 // path is relative (./my-folder/my-file.ts)
89 if (osAgnosticPathWithoutCwd.startsWith(`.${OS_AGNOSTIC_PATH_SEP}`)) {
90 return osAgnosticPathWithoutCwd;
91 }
92
93 // path is segment (my-folder/my-file.ts or my-folder/sub-folder)
94 return osAgnosticPathWithoutCwd;
95}

Callers 12

utils.int.test.tsFile · 0.90
utils.unit.test.tsFile · 0.90
sanitizeReportPathsFunction · 0.90
assertPathMatchFunction · 0.90
assertPathStartWithFunction · 0.90
assertPathContainFunction · 0.90
assertPathEndWithFunction · 0.90
osAgnosticAuditFunction · 0.90

Calls

no outgoing calls

Tested by 1

sanitizeReportPathsFunction · 0.72