fix: make getting started clone destination explicit

This commit is contained in:
lucaronin
2026-04-15 14:55:56 +02:00
parent e142b4c8f9
commit 7eaf57d040
12 changed files with 370 additions and 213 deletions

View File

@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest'
import {
GETTING_STARTED_VAULT_NAME,
buildGettingStartedVaultPath,
formatGettingStartedCloneError,
labelFromPath,
} from './gettingStartedVault'
describe('gettingStartedVault', () => {
it('builds a child vault path from a parent folder', () => {
expect(buildGettingStartedVaultPath('/Users/luca/Documents')).toBe('/Users/luca/Documents/Getting Started')
})
it('trims trailing separators when building the child vault path', () => {
expect(buildGettingStartedVaultPath('/Users/luca/Documents/')).toBe('/Users/luca/Documents/Getting Started')
})
it('preserves windows separators when building the child vault path', () => {
expect(buildGettingStartedVaultPath('C:\\Users\\luca\\Documents\\')).toBe('C:\\Users\\luca\\Documents\\Getting Started')
})
it('derives a label from the final path segment', () => {
expect(labelFromPath('/Users/luca/Documents/Getting Started')).toBe(GETTING_STARTED_VAULT_NAME)
})
it('passes through destination errors verbatim', () => {
expect(formatGettingStartedCloneError("Destination '/tmp/Getting Started' already exists and is not empty"))
.toBe("Destination '/tmp/Getting Started' already exists and is not empty")
})
it('converts other clone failures into a friendly download message', () => {
expect(formatGettingStartedCloneError('git clone failed: fatal: unable to access'))
.toBe('Could not download Getting Started vault. Check your connection and try again.')
})
})

View File

@@ -0,0 +1,38 @@
export const GETTING_STARTED_VAULT_NAME = 'Getting Started'
const CLONE_PATH_ERRORS = [
'already exists and is not empty',
'already exists and is not a directory',
'Failed to create parent directory',
'Target path is required',
]
export function buildGettingStartedVaultPath(parentPath: string): string {
const trimmed = parentPath.trim().replace(/[\\/]+$/g, '')
if (!trimmed) {
return GETTING_STARTED_VAULT_NAME
}
const separator = trimmed.includes('\\') && !trimmed.includes('/') ? '\\' : '/'
return `${trimmed}${separator}${GETTING_STARTED_VAULT_NAME}`
}
export function labelFromPath(path: string): string {
const trimmed = path.trim().replace(/[\\/]+$/g, '')
return trimmed.split(/[\\/]/).pop() || 'Vault'
}
export function formatGettingStartedCloneError(err: unknown): string {
const message =
typeof err === 'string'
? err
: err instanceof Error
? err.message
: `${err}`
if (CLONE_PATH_ERRORS.some(fragment => message.includes(fragment))) {
return message
}
return 'Could not download Getting Started vault. Check your connection and try again.'
}