(projectPath: string)
| 39 | * @returns Result indicating what was created or if the project was already initialized |
| 40 | */ |
| 41 | export async function initializeProject(projectPath: string): Promise<ProjectInitResult> { |
| 42 | const api = getElectronAPI(); |
| 43 | const createdFiles: string[] = []; |
| 44 | const existingFiles: string[] = []; |
| 45 | |
| 46 | try { |
| 47 | // Validate that the project directory exists and is a directory |
| 48 | const projectExists = await api.exists(projectPath); |
| 49 | if (!projectExists) { |
| 50 | return { |
| 51 | success: false, |
| 52 | isNewProject: false, |
| 53 | error: `Project directory does not exist: ${projectPath}. Create it first before initializing.`, |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | // Verify it's actually a directory (not a file) |
| 58 | const projectStat = await api.stat(projectPath); |
| 59 | if (!projectStat.success) { |
| 60 | return { |
| 61 | success: false, |
| 62 | isNewProject: false, |
| 63 | error: projectStat.error || `Failed to stat project directory: ${projectPath}`, |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | if (projectStat.stats && !projectStat.stats.isDirectory) { |
| 68 | return { |
| 69 | success: false, |
| 70 | isNewProject: false, |
| 71 | error: `Project path is not a directory: ${projectPath}`, |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | // Initialize git repository if it doesn't exist |
| 76 | const gitDirExists = await api.exists(`${projectPath}/.git`); |
| 77 | if (!gitDirExists) { |
| 78 | logger.info('Initializing git repository...'); |
| 79 | try { |
| 80 | // Initialize git and create an initial empty commit via server route |
| 81 | const result = await api.worktree?.initGit(projectPath); |
| 82 | if (result?.success && result.result?.initialized) { |
| 83 | createdFiles.push('.git'); |
| 84 | logger.info('Git repository initialized with initial commit'); |
| 85 | } else if (result?.success && !result.result?.initialized) { |
| 86 | // Git already existed (shouldn't happen since we checked, but handle it) |
| 87 | existingFiles.push('.git'); |
| 88 | logger.info('Git repository already exists'); |
| 89 | } else { |
| 90 | logger.warn('Failed to initialize git repository:', result?.error); |
| 91 | } |
| 92 | } catch (gitError) { |
| 93 | logger.warn('Failed to initialize git repository:', gitError); |
| 94 | // Don't fail the whole initialization if git init fails |
| 95 | } |
| 96 | } else { |
| 97 | existingFiles.push('.git'); |
| 98 | } |
no test coverage detected