MCPcopy Create free account
hub / github.com/TanStack/cli / relativePath

Function relativePath

packages/create/src/edge-file-helpers.ts:33–71  ·  view source on GitHub ↗
(
  from: string,
  to: string,
  stripExtension: boolean = false,
)

Source from the content-addressed store, hash-verified

31}
32
33export function relativePath(
34 from: string,
35 to: string,
36 stripExtension: boolean = false,
37) {
38 const normalized = from.replace(/\\/g, '/')
39 const cleanedFrom = normalized.startsWith('./')
40 ? normalized.slice(2)
41 : normalized
42 const cleanedTo = to.startsWith('./') ? to.slice(2) : to
43
44 const fromSegments = cleanedFrom.split('/')
45 const toSegments = cleanedTo.split('/')
46
47 fromSegments.pop()
48 toSegments.pop()
49
50 let commonIndex = 0
51 while (
52 commonIndex < fromSegments.length &&
53 commonIndex < toSegments.length &&
54 fromSegments[commonIndex] === toSegments[commonIndex]
55 ) {
56 commonIndex++
57 }
58
59 const upLevels = fromSegments.length - commonIndex
60 const downLevels = toSegments.slice(commonIndex)
61 const target = stripExtension ? to.replace(extnamePath(to), '') : to
62
63 if (upLevels === 0 && downLevels.length === 0) {
64 return `./${basenamePath(target)}`
65 } else if (upLevels === 0 && downLevels.length > 0) {
66 return `./${downLevels.join('/')}/${basenamePath(target)}`
67 } else {
68 const relative = [...Array(upLevels).fill('..'), ...downLevels].join('/')
69 return `${relative}/${basenamePath(target)}`
70 }
71}
72
73export function isDemoFilePath(path?: string): boolean {
74 if (!path) return false

Callers

nothing calls this directly

Calls 3

extnamePathFunction · 0.85
basenamePathFunction · 0.85
replaceMethod · 0.65

Tested by

no test coverage detected