Skip to content

Commit

Permalink
Merge pull request #11 from tonik/fix/github-repository-exists
Browse files Browse the repository at this point in the history
Fix/GitHub repository exists
  • Loading branch information
KarolinaKopacz authored Oct 24, 2024
2 parents 90ce222 + 298f65c commit 3194ede
Show file tree
Hide file tree
Showing 3 changed files with 1,524 additions and 3,816 deletions.
6 changes: 3 additions & 3 deletions packages/core/utils/github/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export async function initializeRepository(options: ProjectOptions) {
}

// Check if the repository exists and create it
const repoCreated = await createGitHubRepository(projectName, visibility, username);
if (!repoCreated) {
const repoName = await createGitHubRepository(projectName, visibility, username);
if (!repoName) {
console.error('๐Ÿ–‡๏ธ Failed to create GitHub repository. Check your permissions or if the repository already exists.');
process.exit(1);
}

await setupGitRepository(projectName, username);
await setupGitRepository(repoName, username);
}
67 changes: 59 additions & 8 deletions packages/core/utils/github/repositoryManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
import { execSync } from 'child_process';
import { exec, execSync } from 'child_process';
import inquirer from 'inquirer';
import { promisify } from 'util';

const execAsync = promisify(exec);

async function generateUniqueRepoName(baseName: string): Promise<string> {
// Remove any existing numbering pattern from the end
const cleanBaseName = baseName.replace(/-\d+$/, '');

// Try the base name first
try {
await execAsync(`gh repo view ${cleanBaseName}`);
console.error(`๐Ÿ–‡๏ธ Repository "${cleanBaseName}" already exists.`);
// If we get here, the repo exists, so we need a new name
} catch (error) {
// If repo doesn't exist, we can use the clean base name
if (error) {
return cleanBaseName;
}
}

// Find the next available number
let counter = 2; // Start with 2 since it's more natural than 1
while (true) {
const candidateName = `${cleanBaseName}-v${counter}`;
try {
await execAsync(`gh repo view ${candidateName}`);
console.error(`๐Ÿ–‡๏ธ Repository "${candidateName}" already exists.`);
counter++;
} catch (error) {
if (error) {
return candidateName;
}
}
}
}

export function isGitHubAuthenticated(): boolean {
try {
Expand Down Expand Up @@ -51,32 +87,47 @@ export async function createGitHubRepository(
projectName: string,
repositoryVisibility: 'public' | 'private',
username: string,
): Promise<boolean> {
): Promise<string | undefined> {
console.log(`๐Ÿ–‡๏ธ Checking if repository already exists...`);

// Check if the repository exists
const repoCheckCommand = `echo "$(gh repo view ${username}/${projectName} --json name)"`;
const existingRepo = execSync(repoCheckCommand, { stdio: 'pipe' }).toString().trim();
let repoName = projectName;

if (existingRepo) {
console.error(`๐Ÿ–‡๏ธ Repository "${projectName}" already exists.`);
return false; // Return false to indicate the repo was not created
const newRepoName = await generateUniqueRepoName(projectName);
const { confirmedName } = await inquirer.prompt([
{
type: 'input',
name: 'confirmedName',
message: 'Please confirm or modify the repository name:',
default: newRepoName,
validate: (input: string) => {
if (!/^[a-zA-Z0-9._-]+$/.test(input)) {
return 'Repository name can only contain letters, numbers, dots, hyphens, and underscores';
}
return true;
},
},
]);
repoName = confirmedName;
}

console.log(`๐Ÿ–‡๏ธ Creating GitHub repository: \x1b[36m${projectName}\x1b[0m`);
console.log(`๐Ÿ–‡๏ธ Creating GitHub repository: \x1b[36m${repoName}\x1b[0m`);

const visibility = repositoryVisibility === 'public' ? '--public' : '--private';
const command = `gh repo create ${projectName} ${visibility}`;
const command = `gh repo create ${repoName} ${visibility}`;

const result = execSync(command);

if (result) {
console.log(`๐Ÿ–‡๏ธ Repository successfully created at \x1b[36m${result}\x1b[0m`);
return true; // Return true to indicate success
return repoName; // Return true to indicate success
}

console.error('๐Ÿ–‡๏ธ Failed to create GitHub repository.');
return false; // Return false on failure
return; // Return false on failure
}

// New function to set up the local Git repository
Expand Down
Loading

0 comments on commit 3194ede

Please sign in to comment.