Skip to content

Commit 5b420d9

Browse files
committed
feat: enhance resolvePath function to handle nested folders before root in path resolution
1 parent 9bee22a commit 5b420d9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/utils/resolvePath.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export function resolvePath(path: string, rootPath: string, rootAlias: string):
99
// when filename starts with alias + '/', replace alias with root
1010
if (normalizedPath.startsWith(`${normalizedAlias}/`)) return normalizedRoot + normalizedPath.slice(normalizedAlias.length)
1111

12-
// when filename includes root, return filename starting from root
13-
if (normalizedPath.includes(`${normalizedRoot}`)) return normalizedPath.slice(normalizedPath.indexOf(`${normalizedRoot}`))
12+
// when filename includes root, return filename starting from the last occurrence of root
13+
const rootIndex = normalizedPath.lastIndexOf(`${normalizedRoot}`)
14+
if (rootIndex !== -1) return normalizedPath.slice(rootIndex)
1415

1516
// otherwise, return null
1617
return null

tests/utils/resolvePath.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ describe('resolvePath', () => {
2020
expect(result).toBe('src/components/Button.vue')
2121
})
2222

23+
it('handles nested folders before root (e.g. apps/web/src/...)', () => {
24+
const root = '/src'
25+
const alias = '@'
26+
const filename = '/home/andrew/projects/apps/web/src/shared/ui/button/iButton.vue'
27+
28+
const result = resolvePath(filename, root, alias)
29+
expect(result).toBe('src/shared/ui/button/iButton.vue')
30+
})
31+
2332
it('returns the root when filename equals the root', () => {
2433
const root = '/project/src'
2534
const alias = '@'

0 commit comments

Comments
 (0)