Skip to content

Commit

Permalink
refactor: names and modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
maneike committed Oct 11, 2024
1 parent 0dbe13e commit 35bbe29
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
44 changes: 0 additions & 44 deletions packages/core/utils/github/createRepository.ts

This file was deleted.

File renamed without changes.
10 changes: 5 additions & 5 deletions packages/core/utils/github/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkGitHubAuth, createGitHubRepository, getGitHubUsername, gitHubAuth } from './createRepository';
import { installGitHubCLI, isGitHubCLIInstalled } from './installGitHubCLI';
import { isGitHubAuthenticated, createGitHubRepository, fetchGitHubUsername, authenticateGitHub } from './repositoryManager';
import { installGitHubCLI, isGitHubCLIInstalled } from './ghInstaller';

interface ProjectOptions {
projectName: string;
Expand All @@ -24,8 +24,8 @@ export async function createProject(options: ProjectOptions) {

console.log('Checking GitHub authentication...');

if (!checkGitHubAuth()) {
if (!gitHubAuth()) {
if (!isGitHubAuthenticated()) {
if (!authenticateGitHub()) {
console.log('You are not authenticated with GitHub CLI.');
console.log('Please run the following command in your terminal to authenticate:');
console.log('gh auth login');
Expand All @@ -34,7 +34,7 @@ export async function createProject(options: ProjectOptions) {
}
}

const username = getGitHubUsername();
const username = fetchGitHubUsername();
if (username) {
console.log(`Authenticated as GitHub user: ${username}`);
} else {
Expand Down
37 changes: 37 additions & 0 deletions packages/core/utils/github/repositoryManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { execSync } from 'child_process';

function executeCommand(command: string, silent = false): string | null {
try {
return execSync(command, { stdio: silent ? 'ignore' : 'pipe' })
.toString()
.trim();
} catch {
return null;
}
}

export function isGitHubAuthenticated(): boolean {
return !!executeCommand('gh auth status', true);
}

export function authenticateGitHub(): boolean {
return !!executeCommand('gh auth login', true);
}

export function fetchGitHubUsername(): string | null {
return executeCommand('gh api user --jq .login');
}

export function createGitHubRepository(projectName: string, repositoryVisibility: 'public' | 'private'): boolean {
console.log(`Creating GitHub repository: ${projectName}`);
const visibility = repositoryVisibility === 'public' ? '--public' : '--private';
const result = executeCommand(`gh repo create ${projectName} ${visibility} --confirm`);

if (result) {
console.log(result);
return true;
}

console.error('Failed to create GitHub repository.');
return false;
}

0 comments on commit 35bbe29

Please sign in to comment.