import { Command } from 'commander'; import { LintUseCase } from '../../../usecases/LintUseCase.js'; import { TextDocument } from '../../../domain/text/TextDocument.js'; import { MarkdownParser } from '../../../core/parser.js'; import { FileReader } from '../../../infrastructure/FileReader.js'; import { ConsoleFormatter, JsonFormatter } from '../../formatters.js'; import fs from 'fs'; import path from 'path'; /** * text サブコマンドを作成する */ export function createTextCommand(): Command { const command = new Command('text'); command .description('テキスト教材をチェック') .argument('', 'チェック対象のMarkdownファイル') .option('--format ', '出力形式 (console|json)', 'console') .action(async (file: string, options: { format: string }) => { const absolutePath = path.resolve(process.cwd(), file); if (!fs.existsSync(absolutePath)) { console.error(`エラー: ファイルが見つかりません: ${file}`); process.exit(1); } // 依存関係のセットアップ const parser = new MarkdownParser(); const fileReader = new FileReader(); const documentType = new TextDocument(); const useCase = new LintUseCase(parser, fileReader, documentType); try { const result = await useCase.executeFromFile(absolutePath); // フォーマッター選択 const formatter = options.format === 'json' ? new JsonFormatter() : new ConsoleFormatter(); const output = formatter.format(result); console.log(output); // エラーがあれば終了コード1 const hasErrors = result.results.some(r => r.severity === 'error'); if (hasErrors) { process.exit(1); } } catch (error) { console.error('エラーが発生しました:', error); process.exit(1); } }); return command; }