diff --git a/packages/core/utils/github/install.ts b/packages/core/utils/github/install.ts index fb56081..9a4d287 100644 --- a/packages/core/utils/github/install.ts +++ b/packages/core/utils/github/install.ts @@ -27,7 +27,7 @@ function checkGitHubCLI() { // Helper function to ensure GitHub authentication function ensureGitHubAuthentication() { - console.log('🖇️ Checking GitHub authentication...'); + console.log('🖇️ Checking GitHub authentication status...'); // Check if the user is already authenticated if (isGitHubAuthenticated()) { @@ -35,9 +35,9 @@ function ensureGitHubAuthentication() { return; // Exit early if authenticated } - if (!authenticateGitHub()) { - console.error('🖇️ Authentication failed. Run "gh auth login" in your terminal and try again.'); - process.exit(1); + if (!isGitHubAuthenticated()) { + console.error(`🖇️ It looks like you're not logged in...`); + authenticateGitHub(); } } diff --git a/packages/core/utils/github/repositoryManager.ts b/packages/core/utils/github/repositoryManager.ts index 5de555e..267cff5 100644 --- a/packages/core/utils/github/repositoryManager.ts +++ b/packages/core/utils/github/repositoryManager.ts @@ -1,33 +1,13 @@ -import { execSync } from 'child_process'; - -async function executeCommand(command: string, silent = false): Promise { - try { - const result = execSync(command, { stdio: 'pipe' }); // Use 'pipe' to capture output - const output = result.toString().trim(); // Convert buffer to string - return output || null; // Ensure we return null if output is empty - } catch (error) { - console.error(`Error executing command: ${command}`); - if (error instanceof Error) { - console.error(`Error message: ${error.message}`); - if (error) { - console.error(`Command stdout: ${error.toString()}`); // Log stdout - } - } - return null; - } -} +import { type StdioOptions, execSync } from 'child_process'; export function isGitHubAuthenticated(): boolean { - console.log('🖇️ Checking GitHub authentication status...'); - try { // Use execSync to run the command and capture output - const result = execSync('gh auth status', { stdio: 'pipe' }).toString(); + const result = execSync('gh auth status', { stdio: 'pipe' }).toString().trim(); // Check if the output includes "Logged in" - this is to be changed in the future but couldn't find a better way return result.includes('Logged in'); } catch (error) { - console.error(`🖇️ Error checking authentication status: ${error}`); return false; } } @@ -35,19 +15,17 @@ export function isGitHubAuthenticated(): boolean { export async function authenticateGitHub(): Promise { console.log('🖇️ Attempting to authenticate with GitHub...'); - const result = await executeCommand('gh auth login'); + execSync('gh auth login', { stdio: 'inherit' }); - if (result) { - // Immediately check authentication status after login attempt - const isAuthenticated = isGitHubAuthenticated(); + // Immediately check authentication status after login attempt + const isAuthenticated = isGitHubAuthenticated(); - if (isAuthenticated) { - console.log('🖇️ Authentication was successful.'); - return true; - } else { - console.error('🖇️ Authentication failed after login attempt.'); - return false; - } + if (isAuthenticated) { + console.log('🖇️ Authentication was successful.'); + return true; + } else { + console.error('🖇️ Authentication failed after login attempt.'); + return false; } console.error('🖇️ No output from gh auth login command.'); @@ -57,7 +35,7 @@ export async function authenticateGitHub(): Promise { export async function fetchGitHubUsername(): Promise { try { // Run the command without --jq first to inspect raw output - const username = await executeCommand('echo "$(gh api user --jq .login)"'); + const username = execSync('echo "$(gh api user --jq .login)"', { stdio: 'pipe' }).toString().trim(); if (username) { console.log(`🖇️ Hello \x1b[36m${username}\x1b[0m!`); @@ -81,7 +59,7 @@ export async function createGitHubRepository( // Check if the repository exists const repoCheckCommand = `echo "$(gh repo view ${username}/${projectName} --json name)"`; - const existingRepo = await executeCommand(repoCheckCommand, true); // Silent mode to suppress output + const existingRepo = execSync(repoCheckCommand, { stdio: 'pipe' }).toString().trim(); if (existingRepo) { console.error(`🖇️ Repository "${projectName}" already exists.`); @@ -91,12 +69,12 @@ export async function createGitHubRepository( console.log(`🖇️ Creating GitHub repository: \x1b[36m${projectName}\x1b[0m`); const visibility = repositoryVisibility === 'public' ? '--public' : '--private'; - const command = `gh repo create ${projectName} ${visibility} --confirm`; + const command = `gh repo create ${projectName} ${visibility}`; - const result = await executeCommand(command); + const result = execSync(command); if (result) { - console.log(`🖇️ Repository created successfully at \x1b[36m${result}\x1b[0m`); + console.log(`🖇️ Repository successfully created at \x1b[36m${result}\x1b[0m`); return true; // Return true to indicate success } @@ -119,7 +97,7 @@ export async function setupGitRepository(projectName: string, username: string) ]; for (const cmd of commands) { - const result = executeCommand(cmd); + const result = execSync(cmd, { stdio: 'pipe' }); if (!result) { console.error(`🖇️ Failed to execute command: ${cmd}`); process.exit(1);