Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/create/src/file-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { basename, extname, resolve } from 'node:path'
import parseGitignore from 'parse-gitignore'
import ignore from 'ignore'

import { hasDrive, stripDrive } from './utils'
import type { Environment } from './types'

const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico']
Expand Down Expand Up @@ -43,7 +44,17 @@ export function toCleanPath(absolutePath: string, baseDir: string): string {
// Normalize both paths to use forward slashes for consistent comparison
const normalizedPath = absolutePath.replace(/\\/g, '/')
const normalizedBase = baseDir.replace(/\\/g, '/')
let cleanPath = normalizedPath.replace(normalizedBase, '')
let cleanPath = normalizedPath
if (normalizedPath.startsWith(normalizedBase)) {
cleanPath = normalizedPath.slice(normalizedBase.length)
} else if (hasDrive(normalizedPath) !== hasDrive(normalizedBase)) {
// Handle paths that are missing the Windows drive letter (e.g. memfs on Windows)
const pathNoDrive = stripDrive(normalizedPath)
const baseNoDrive = stripDrive(normalizedBase)
if (pathNoDrive.startsWith(baseNoDrive)) {
cleanPath = pathNoDrive.slice(baseNoDrive.length)
}
}
// Handle leading path separator
if (cleanPath.startsWith('/')) {
cleanPath = cleanPath.slice(1)
Expand Down
8 changes: 8 additions & 0 deletions packages/create/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ export function handleSpecialURL(url: string) {
}
return url
}

export function hasDrive(path: string) {
return /^[A-Za-z]:/.test(path)
}

export function stripDrive(path: string) {
return path.replace(/^[A-Za-z]:/, '')
}
6 changes: 6 additions & 0 deletions packages/create/tests/file-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ describe('toCleanPath', () => {
toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:/Projects/my-app'),
).toBe('src/file.ts')
})

it('should handle missing drive letter in path against Windows base', () => {
expect(
toCleanPath('/Users/me/my-app/src/file.ts', 'C:\\Users\\me\\my-app'),
).toBe('src/file.ts')
})
})

describe('relativePath', () => {
Expand Down