symref
Version:
Static code checker for AI code agents (Windsurf, Cline, etc.)
66 lines • 3.05 kB
JavaScript
import * as fs from 'node:fs';
import * as path from 'node:path';
import { OutputFormatter } from './OutputFormatter.js';
import { LogLevel } from '../types/LogLevel.js';
export class MermaidGenerator {
constructor() {
this.formatter = new OutputFormatter();
}
generate(result, outputPath) {
try {
if (!result.graphMermaidFormat) {
this.formatter.log('警告: Mermaidグラフデータを生成できませんでした。', LogLevel.WARN);
return;
}
// 出力ディレクトリを.symbolsに変更
const outputDir = '.symbols';
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const timestamp = `${year}${month}${day}_${hours}${minutes}`;
const safeBaseName = outputPath.replace(/[^a-zA-Z0-9]/g, '_');
const fileName = `${safeBaseName}_${timestamp}.md`;
const resolvedPath = path.resolve(process.cwd(), outputDir, fileName);
// 出力ディレクトリを確保
const dir = path.dirname(resolvedPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Mermaidコンテンツを生成
const mermaidContent = this.generateMermaidContent(result);
// ファイルに書き込み
fs.writeFileSync(resolvedPath, mermaidContent, 'utf8');
this.formatter.log(`Mermaidグラフファイルを生成しました: ${resolvedPath}`, LogLevel.INFO);
this.formatter.log('可視化するには: GitHubで表示するか、https://mermaid.live で開いてください', LogLevel.INFO);
}
catch (error) {
this.formatter.log(`Mermaidファイルの生成中にエラーが発生しました: ${error.message}`, LogLevel.ERROR);
throw error;
}
}
generateMermaidContent(result) {
const nodes = new Set();
const edges = new Set();
result.paths.forEach((path) => {
path.nodes.forEach((node, index) => {
const nodeId = this.sanitizeId(node.symbol);
nodes.add(`${nodeId}["${node.symbol}"]`);
if (index < path.nodes.length - 1) {
const nextNode = path.nodes[index + 1];
const nextNodeId = this.sanitizeId(nextNode.symbol);
edges.add(`${nodeId} --> ${nextNodeId}`);
}
});
});
return `graph TD
${Array.from(nodes).join('\n ')}
${Array.from(edges).join('\n ')}`;
}
sanitizeId(symbol) {
return symbol.replace(/[^a-zA-Z0-9]/g, '_');
}
}
//# sourceMappingURL=MermaidGenerator.js.map