Skip to content

Commit

Permalink
feat: add functions fot gh repository creation
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolinaKopacz committed Oct 10, 2024
1 parent dce291b commit 0dbe13e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/core/utils/github/createRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { execSync } from 'child_process';

export function checkGitHubAuth(): boolean {
try {
execSync('gh auth status', { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}

export function gitHubAuth(): boolean {
try {
execSync('gh auth login', { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}

export function getGitHubUsername(): string | null {
try {
return execSync('gh api user --jq .login').toString().trim();
} catch (error) {
return null;
}
}

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

try {
const result = execSync(`gh repo create ${projectName} ${visibility} --confirm`, { stdio: 'pipe' }).toString();
console.log(result);
return true;
} catch (error) {
console.error('Failed to create GitHub repository.');
if (error instanceof Error) {
console.error(error.message);
}
return false;
}
}
55 changes: 55 additions & 0 deletions packages/core/utils/github/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { checkGitHubAuth, createGitHubRepository, getGitHubUsername, gitHubAuth } from './createRepository';
import { installGitHubCLI, isGitHubCLIInstalled } from './installGitHubCLI';

interface ProjectOptions {
projectName: string;
repositoryVisibility: 'public' | 'private';
}

// GitHub CLI - check if installed, if not install
// Check if use is authenticated, if not auth, get his Username just to prompt it and then create repository

export async function createProject(options: ProjectOptions) {
const { projectName, repositoryVisibility } = options;

console.log('Checking GitHub CLI installation...');

if (!isGitHubCLIInstalled()) {
console.log('GitHub CLI is not installed.');
const installed = installGitHubCLI();
if (!installed) {
process.exit(1);
}
}

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

if (!checkGitHubAuth()) {
if (!gitHubAuth()) {
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');
console.log('After authentication, run this script again.');
process.exit(1);
}
}

const username = getGitHubUsername();
if (username) {
console.log(`Authenticated as GitHub user: ${username}`);
} else {
console.log('Authenticated with GitHub, but unable to retrieve username.');
}

// Create GitHub repository
const repoCreated = createGitHubRepository(projectName, repositoryVisibility);
if (!repoCreated) {
console.log('Failed to create GitHub repository. Exiting...');
process.exit(1);
}

// Continue with project creation
console.log(`Creating repository: ${projectName}`);

console.log('\x1b[32m%s\x1b[0m', `Success: Your ${projectName} repository is ready!`);
}
68 changes: 68 additions & 0 deletions packages/core/utils/github/installGitHubCLI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { execSync } from 'child_process';
import * as os from 'os';

export function isGitHubCLIInstalled(): boolean {
try {
execSync('gh --version', { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}

export function installGitHubCLI(): boolean {
const platform = os.platform();
let installCommand: string;

switch (platform) {
case 'darwin': // macOS
installCommand = 'brew install gh';
break;
case 'linux':
const linuxDistro = getLinuxDistro();
if (linuxDistro === 'ubuntu' || linuxDistro === 'debian') {
installCommand =
'sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0 && sudo apt-add-repository https://cli.github.com/packages && sudo apt update && sudo apt install gh';
} else if (linuxDistro === 'fedora' || linuxDistro === 'centos' || linuxDistro === 'rhel') {
installCommand =
'sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo && sudo dnf install gh';
} else {
console.log('Automatic installation is not supported for your Linux distribution.');
console.log('Please visit https://github.com/cli/cli#installation for installation instructions.');
return false;
}
break;
case 'win32': // Windows
installCommand = 'winget install --id GitHub.cli';
break;
default:
console.log('Automatic installation is not supported for your operating system.');
console.log('Please visit https://github.com/cli/cli#installation for installation instructions.');
return false;
}

console.log('Installing GitHub CLI...');
try {
execSync(installCommand, { stdio: 'inherit' });
console.log('GitHub CLI installed successfully.');
return true;
} catch (error) {
console.error('Failed to install GitHub CLI.');
console.log('Please install it manually from: https://github.com/cli/cli#installation');
return false;
}
}

export function getLinuxDistro(): string {
try {
const osRelease = execSync('cat /etc/os-release').toString();
if (osRelease.includes('Ubuntu')) return 'ubuntu';
if (osRelease.includes('Debian')) return 'debian';
if (osRelease.includes('Fedora')) return 'fedora';
if (osRelease.includes('CentOS')) return 'centos';
if (osRelease.includes('Red Hat')) return 'rhel';
return 'unknown';
} catch (error) {
return 'unknown';
}
}

0 comments on commit 0dbe13e

Please sign in to comment.