mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-30 13:43:00 -07:00
fix(workspaces): clean up distractors and fix ESM runtime errors
This commit is contained in:
@@ -1,20 +1,22 @@
|
||||
/**
|
||||
* Universal Workspace Cleanup (Local)
|
||||
*
|
||||
* Surgical or full cleanup of sessions and worktrees on the GCE worker.
|
||||
* Refactored to use WorkerProvider for container compatibility.
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { fileURLToPath } from 'url';
|
||||
import readline from 'readline';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import readline from 'node:readline';
|
||||
import { ProviderFactory } from './providers/ProviderFactory.ts';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const REPO_ROOT = path.resolve(__dirname, '../../../..');
|
||||
|
||||
async function confirm(question: string): Promise<boolean> {
|
||||
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
return new Promise((resolve) => {
|
||||
rl.question(`${question} (y/n): `, (answer) => {
|
||||
rl.close();
|
||||
@@ -23,13 +25,16 @@ async function confirm(question: string): Promise<boolean> {
|
||||
});
|
||||
}
|
||||
|
||||
export async function runCleanup(args: string[], env: NodeJS.ProcessEnv = process.env) {
|
||||
export async function runCleanup(
|
||||
args: string[],
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
) {
|
||||
const prNumber = args[0];
|
||||
const action = args[1];
|
||||
|
||||
const settingsPath = path.join(REPO_ROOT, '.gemini/workspaces/settings.json');
|
||||
if (!fs.existsSync(settingsPath)) {
|
||||
console.error('❌ Settings not found. Run "npm run workspace:setup" first.');
|
||||
console.error('❌ Settings not found. Run "workspace setup" first.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -42,54 +47,81 @@ export async function runCleanup(args: string[], env: NodeJS.ProcessEnv = proces
|
||||
|
||||
const { projectId, zone } = config;
|
||||
const targetVM = `gcli-workspace-${env.USER || 'mattkorwel'}`;
|
||||
const provider = ProviderFactory.getProvider({ projectId, zone, instanceName: targetVM });
|
||||
const provider = ProviderFactory.getProvider({
|
||||
projectId,
|
||||
zone,
|
||||
instanceName: targetVM,
|
||||
});
|
||||
|
||||
if (prNumber && action) {
|
||||
const sessionName = `workspace-${prNumber}-${action}`;
|
||||
const worktreePath = `/home/node/.workspaces/worktrees/${sessionName}`;
|
||||
|
||||
console.log(`🧹 Surgically removing session and worktree for ${prNumber}-${action}...`);
|
||||
|
||||
|
||||
console.log(
|
||||
`🧹 Surgically removing session and worktree for ${prNumber}-${action}...`,
|
||||
);
|
||||
|
||||
// Kill specific tmux session inside container
|
||||
await provider.exec(`tmux kill-session -t ${sessionName} 2>/dev/null`, { wrapContainer: 'maintainer-worker' });
|
||||
|
||||
await provider.exec(`tmux kill-session -t ${sessionName} 2>/dev/null`, {
|
||||
wrapContainer: 'maintainer-worker',
|
||||
});
|
||||
|
||||
// Remove specific worktree inside container
|
||||
await provider.exec(`cd /home/node/.workspaces/main && git worktree remove -f ${worktreePath} 2>/dev/null && git worktree prune`, { wrapContainer: 'maintainer-worker' });
|
||||
|
||||
await provider.exec(
|
||||
`cd /home/node/.workspaces/main && git worktree remove -f ${worktreePath} 2>/dev/null && git worktree prune`,
|
||||
{ wrapContainer: 'maintainer-worker' },
|
||||
);
|
||||
|
||||
console.log(`✅ Cleaned up ${prNumber}-${action}.`);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- Bulk Cleanup ---
|
||||
console.log(`⚠️ DANGER: You are about to perform a BULK cleanup on ${targetVM}.`);
|
||||
const confirmed = await confirm(' Are you sure you want to kill ALL sessions and worktrees?');
|
||||
console.log(
|
||||
`⚠️ DANGER: You are about to perform a BULK cleanup on ${targetVM}.`,
|
||||
);
|
||||
const confirmed = await confirm(
|
||||
' Are you sure you want to kill ALL sessions and worktrees?',
|
||||
);
|
||||
if (!confirmed) {
|
||||
console.log('❌ Cleanup cancelled.');
|
||||
return 0;
|
||||
console.log('❌ Cleanup cancelled.');
|
||||
return 0;
|
||||
}
|
||||
|
||||
console.log(`🧹 Starting BULK cleanup...`);
|
||||
|
||||
// 1. Standard Cleanup
|
||||
console.log(' - Killing ALL remote tmux sessions...');
|
||||
await provider.exec(`tmux kill-server`, { wrapContainer: 'maintainer-worker' });
|
||||
await provider.exec(`tmux kill-server`, {
|
||||
wrapContainer: 'maintainer-worker',
|
||||
});
|
||||
|
||||
console.log(' - Cleaning up Docker resources...');
|
||||
await provider.exec(`sudo docker rm -f maintainer-worker || true`);
|
||||
await provider.exec(`sudo docker system prune -af --volumes`);
|
||||
|
||||
console.log(' - Cleaning up ALL Git Worktrees...');
|
||||
await provider.exec(`cd /home/node/.workspaces/main && git worktree prune && rm -rf /home/node/.workspaces/worktrees/*`, { wrapContainer: 'maintainer-worker' });
|
||||
await provider.exec(
|
||||
`cd /home/node/.workspaces/main && git worktree prune && rm -rf /home/node/.workspaces/worktrees/*`,
|
||||
{ wrapContainer: 'maintainer-worker' },
|
||||
);
|
||||
|
||||
console.log('✅ Remote environment cleared.');
|
||||
|
||||
// 2. Full Wipe Option
|
||||
const shouldWipe = await confirm('\nWould you like to COMPLETELY wipe the remote workspace (main clone)?');
|
||||
|
||||
const shouldWipe = await confirm(
|
||||
'\nWould you like to COMPLETELY wipe the remote workspace (main clone)?',
|
||||
);
|
||||
|
||||
if (shouldWipe) {
|
||||
console.log(`🔥 Wiping /home/node/.workspaces/main...`);
|
||||
await provider.exec(`rm -rf /home/node/.workspaces/main && mkdir -p /home/node/.workspaces/main`, { wrapContainer: 'maintainer-worker' });
|
||||
console.log('✅ Remote hub wiped. You will need to run npm run workspace:setup again.');
|
||||
await provider.exec(
|
||||
`rm -rf /home/node/.workspaces/main && mkdir -p /home/node/.workspaces/main`,
|
||||
{ wrapContainer: 'maintainer-worker' },
|
||||
);
|
||||
console.log(
|
||||
'✅ Remote hub wiped. You will need to run workspace setup again.',
|
||||
);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user