import { Command } from 'commander' import { handleError } from '@/shared/utils/error-handler' import { formatOutput } from '@/shared/utils/output' import { LineClient } from '../client' async function listAction(chatId: string, options: { count?: string; pretty?: boolean }): Promise { let client: LineClient | undefined try { client = await new LineClient().login() const count = options.count ? Number.parseInt(options.count, 10) : 20 const messages = await client.getMessages(chatId, { count }) console.log(formatOutput(messages, options.pretty)) } catch (error) { handleError(error as Error) } finally { client?.close() } } async function sendAction(chatId: string, text: string, options: { pretty?: boolean }): Promise { let client: LineClient | undefined try { client = await new LineClient().login() const result = await client.sendMessage(chatId, text) console.log(formatOutput(result, options.pretty)) } catch (error) { handleError(error as Error) } finally { client?.close() } } export const messageCommand = new Command('message') .description('LINE message commands') .addCommand( new Command('list') .description('List messages in a chat room') .argument('', 'Chat room MID') .option('-n, --count ', 'Number of messages to fetch', '20') .option('--pretty', 'Pretty print JSON output') .action(listAction), ) .addCommand( new Command('send') .description('Send a text message to a chat room') .argument('', 'Chat room MID') .argument('', 'Message text') .option('--pretty', 'Pretty print JSON output') .action(sendAction), )