Skip to content

Commit f4c9e90

Browse files
jong-kyung이종경
andauthored
Fix/window path (#330)
* fix: enhance toCleanPath for better Windows drive handling * fix: add test for handling missing drive letter in Windows path in toCleanPath --------- Co-authored-by: 이종경 <[email protected]>
1 parent ccf0727 commit f4c9e90

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/create/src/file-helpers.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { basename, extname, resolve } from 'node:path'
44
import parseGitignore from 'parse-gitignore'
55
import ignore from 'ignore'
66

7+
import { hasDrive, stripDrive } from './utils'
78
import type { Environment } from './types'
89

910
const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico']
@@ -43,7 +44,17 @@ export function toCleanPath(absolutePath: string, baseDir: string): string {
4344
// Normalize both paths to use forward slashes for consistent comparison
4445
const normalizedPath = absolutePath.replace(/\\/g, '/')
4546
const normalizedBase = baseDir.replace(/\\/g, '/')
46-
let cleanPath = normalizedPath.replace(normalizedBase, '')
47+
let cleanPath = normalizedPath
48+
if (normalizedPath.startsWith(normalizedBase)) {
49+
cleanPath = normalizedPath.slice(normalizedBase.length)
50+
} else if (hasDrive(normalizedPath) !== hasDrive(normalizedBase)) {
51+
// Handle paths that are missing the Windows drive letter (e.g. memfs on Windows)
52+
const pathNoDrive = stripDrive(normalizedPath)
53+
const baseNoDrive = stripDrive(normalizedBase)
54+
if (pathNoDrive.startsWith(baseNoDrive)) {
55+
cleanPath = pathNoDrive.slice(baseNoDrive.length)
56+
}
57+
}
4758
// Handle leading path separator
4859
if (cleanPath.startsWith('/')) {
4960
cleanPath = cleanPath.slice(1)

packages/create/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ export function handleSpecialURL(url: string) {
4040
}
4141
return url
4242
}
43+
44+
export function hasDrive(path: string) {
45+
return /^[A-Za-z]:/.test(path)
46+
}
47+
48+
export function stripDrive(path: string) {
49+
return path.replace(/^[A-Za-z]:/, '')
50+
}

packages/create/tests/file-helper.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ describe('toCleanPath', () => {
4444
toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:/Projects/my-app'),
4545
).toBe('src/file.ts')
4646
})
47+
48+
it('should handle missing drive letter in path against Windows base', () => {
49+
expect(
50+
toCleanPath('/Users/me/my-app/src/file.ts', 'C:\\Users\\me\\my-app'),
51+
).toBe('src/file.ts')
52+
})
4753
})
4854

4955
describe('relativePath', () => {

0 commit comments

Comments
 (0)