refactor(commands): centralize shared command formatting helpers

This commit is contained in:
Peter Steinberger
2026-02-22 21:18:10 +00:00
parent 06bdd53658
commit 4bf67ab698
15 changed files with 406 additions and 115 deletions

View File

@@ -10,6 +10,14 @@ export type RemovalResult = {
skipped?: boolean;
};
export type CleanupResolvedPaths = {
stateDir: string;
configPath: string;
oauthDir: string;
configInsideState: boolean;
oauthInsideState: boolean;
};
export function collectWorkspaceDirs(cfg: OpenClawConfig | undefined): string[] {
const dirs = new Set<string>();
const defaults = cfg?.agents?.defaults;
@@ -96,6 +104,42 @@ export async function removePath(
}
}
export async function removeStateAndLinkedPaths(
cleanup: CleanupResolvedPaths,
runtime: RuntimeEnv,
opts?: { dryRun?: boolean },
): Promise<void> {
await removePath(cleanup.stateDir, runtime, {
dryRun: opts?.dryRun,
label: cleanup.stateDir,
});
if (!cleanup.configInsideState) {
await removePath(cleanup.configPath, runtime, {
dryRun: opts?.dryRun,
label: cleanup.configPath,
});
}
if (!cleanup.oauthInsideState) {
await removePath(cleanup.oauthDir, runtime, {
dryRun: opts?.dryRun,
label: cleanup.oauthDir,
});
}
}
export async function removeWorkspaceDirs(
workspaceDirs: readonly string[],
runtime: RuntimeEnv,
opts?: { dryRun?: boolean },
): Promise<void> {
for (const workspace of workspaceDirs) {
await removePath(workspace, runtime, {
dryRun: opts?.dryRun,
label: workspace,
});
}
}
export async function listAgentSessionDirs(stateDir: string): Promise<string[]> {
const root = path.join(stateDir, "agents");
try {