diff --git a/packages/create/src/file-helpers.ts b/packages/create/src/file-helpers.ts index 4ea00165..8ce715c3 100644 --- a/packages/create/src/file-helpers.ts +++ b/packages/create/src/file-helpers.ts @@ -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'] @@ -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) diff --git a/packages/create/src/utils.ts b/packages/create/src/utils.ts index 5f450e78..4ecc2a40 100644 --- a/packages/create/src/utils.ts +++ b/packages/create/src/utils.ts @@ -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]:/, '') +} diff --git a/packages/create/tests/file-helper.test.ts b/packages/create/tests/file-helper.test.ts index 52a69483..53285b4c 100644 --- a/packages/create/tests/file-helper.test.ts +++ b/packages/create/tests/file-helper.test.ts @@ -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', () => {