import { log } from "@clack/prompts" import { defineCommand } from "../lib/command" import { globalOptions, globalOptionsSchema } from "../lib/global-options" import { authClient } from "../lib/auth-client" import { accent } from "../lib/io" import { clearSavedTokenSync } from "../lib/session" import { clearAllExecutionData, getActiveExecutionDataPath, } from "../lib/execution-data" import { clearSpinner, startSpinner } from "../lib/spinner" import { output, sessionIntro } from "../utils" export const logoutCommand = defineCommand({ name: "logout", description: "Logout from Automate.ax", options: globalOptions, schema: globalOptionsSchema, run: logout, }) /** Signs out and clears the saved CLI session token. */ export async function logout() { let remoteError: unknown const session = await authClient.getSession().catch((error: unknown) => { remoteError = error return null }) output.normal(() => sessionIntro(session)) startSpinner("Signing out") // Capture whether cleanup had work before the origin directory is removed. const hadLocalExecutionData = (await getActiveExecutionDataPath()) !== null if (session) { try { await authClient.signOut() } catch (error) { remoteError = error } } try { await clearAllExecutionData() } finally { clearSavedTokenSync() clearSpinner() } if (remoteError) throw remoteError output .normal(() => { if (session) { log.success(`Signed out. Sign back in with ${accent("automate login")}`) } else { log.warn( `Local session cleared. Run ${accent("automate login")} to sign in.`, ) } }) .json({ localExecutionDataCleared: hadLocalExecutionData, loggedIn: false, success: true, user: null, }) }