import { getConfigPath as getAuthConfigPath, runDeviceLogin, saveAuthToken, signOut, } from '@drawcall/auth' import { Command, Option } from 'commander' import { logout } from './commands/logout.js' import { clearConfig, saveConfig } from './config.js' import { loginResult } from './output.js' import { createClient } from './v1/client.js' const DEFAULT_BASE_URL = 'https://market.drawcall.ai' export function createLoginCommand(): Command { return new Command('login') .description('Sign in') .addOption( new Option('--api ', 'API URL').default( process.env.MARKET_API_URL, 'from MARKET_API_URL / config / default', ), ) .action(async (options: { api?: string }) => { const baseUrl = options.api ?? DEFAULT_BASE_URL const token = await runDeviceLogin() const client = createClient({ baseUrl, authToken: token }) const profile = await client.user.getProfile() if (!profile) throw new Error('The Market API did not accept the auth token.') if (options.api) await saveConfig({ baseUrl: options.api }) else await clearConfig() await saveAuthToken(token) console.log(loginResult(profile.email, getAuthConfigPath())) }) } export function createLogoutCommand(): Command { return new Command('logout').description('Sign out').action(async () => { await logout(await signOut()) }) }