feat(workspaces): implement interactive fork selection and management

This commit is contained in:
mkorwel
2026-03-18 11:27:30 -07:00
parent d20923953a
commit c91e00e0ed
+35 -19
View File
@@ -38,6 +38,17 @@ async function confirm(question: string): Promise<boolean> {
}); });
} }
async function createFork(upstream: string): Promise<string> {
console.log(` - Creating fork for ${upstream}...`);
const forkRes = spawnSync('gh', ['repo', 'fork', upstream, '--clone=false'], { stdio: 'inherit' });
if (forkRes.status === 0) {
const userRes = spawnSync('gh', ['api', 'user', '-q', '.login'], { stdio: 'pipe' });
const user = userRes.stdout.toString().trim();
return `${user}/${upstream.split('/')[1]}`;
}
return upstream;
}
export async function runSetup(env: NodeJS.ProcessEnv = process.env) { export async function runSetup(env: NodeJS.ProcessEnv = process.env) {
console.log(` console.log(`
================================================================================ ================================================================================
@@ -77,28 +88,33 @@ and full builds) to a dedicated, high-performance GCP worker.
if (repoInfoRes.status === 0) { if (repoInfoRes.status === 0) {
try { try {
const repoInfo = JSON.parse(repoInfoRes.stdout.toString()); const repoInfo = JSON.parse(repoInfoRes.stdout.toString());
if (repoInfo.isFork && repoInfo.parent) { upstreamRepo = repoInfo.isFork && repoInfo.parent ? repoInfo.parent.nameWithOwner : repoInfo.nameWithOwner;
upstreamRepo = repoInfo.parent.nameWithOwner;
userFork = repoInfo.nameWithOwner;
console.log(` ✅ Detected Existing Fork: ${userFork}`);
} else {
upstreamRepo = repoInfo.nameWithOwner;
console.log(` ✅ Working on Upstream: ${upstreamRepo}`);
const shouldFork = await confirm(`❓ No fork detected. Create a personal fork for sandboxed implementations?`); console.log(` - Upstream identified: ${upstreamRepo}`);
if (shouldFork) { console.log(` - Searching for your forks of ${upstreamRepo}...`);
console.log(` - Creating fork for ${upstreamRepo}...`);
const forkRes = spawnSync('gh', ['repo', 'fork', upstreamRepo, '--clone=false'], { stdio: 'inherit' }); const forksRes = spawnSync('gh', ['repo', 'list', '--fork', '--json', 'nameWithOwner', '--limit', '10'], { stdio: 'pipe' });
if (forkRes.status === 0) { const allForks = JSON.parse(forksRes.stdout.toString());
// Get the new fork name (usually <user>/<repo>) const myForks = allForks.filter((f: any) => f.nameWithOwner.endsWith('/' + upstreamRepo.split('/')[1]));
const userRes = spawnSync('gh', ['api', 'user', '-q', '.login'], { stdio: 'pipe' });
const user = userRes.stdout.toString().trim(); if (myForks.length > 0) {
userFork = `${user}/${upstreamRepo.split('/')[1]}`; console.log('\n🍴 Found existing forks:');
console.log(` ✅ Fork created: ${userFork}`); myForks.forEach((f: any, i: number) => console.log(` [${i + 1}] ${f.nameWithOwner}`));
} console.log(` [c] Create a new fork`);
} else { console.log(` [u] Use upstream directly (not recommended)`);
const choice = await prompt('Select an option', '1');
if (choice.toLowerCase() === 'c') {
userFork = await createFork(upstreamRepo);
} else if (choice.toLowerCase() === 'u') {
userFork = upstreamRepo; userFork = upstreamRepo;
} else {
const idx = parseInt(choice) - 1;
userFork = myForks[idx] ? myForks[idx].nameWithOwner : upstreamRepo;
} }
} else {
const shouldFork = await confirm(`❓ No fork detected. Create a personal fork for sandboxed implementations?`);
userFork = shouldFork ? await createFork(upstreamRepo) : upstreamRepo;
} }
} catch (e) { } catch (e) {
userFork = upstreamRepo; userFork = upstreamRepo;