|
| 1 | +const fs = require("node:fs"); |
| 2 | +const path = require("node:path"); |
| 3 | + |
| 4 | +/** |
| 5 | + * 指定ディレクトリ以下の全JSファイルからクラス定義とJSDocを抽出してMarkdown生成 |
| 6 | + * @param {string} baseDir - 探索開始ディレクトリ(例: ./src) |
| 7 | + * @param {string} outputPath - 出力先Markdownファイル(例: ./docs/classes.md) |
| 8 | + */ |
| 9 | +function generateMarkdownFromClasses(baseDir, outputPath) { |
| 10 | + const allJsFiles = []; |
| 11 | + walkDir(baseDir, allJsFiles); |
| 12 | + |
| 13 | + let markdown = `# クラス一覧\n\n`; |
| 14 | + |
| 15 | + for (const filePath of allJsFiles) { |
| 16 | + const relPath = path.relative(baseDir, filePath); |
| 17 | + const content = fs.readFileSync(filePath, "utf-8"); |
| 18 | + const classBlocks = extractClassDocs(content); |
| 19 | + if (classBlocks.length === 0) continue; |
| 20 | + |
| 21 | + markdown += `## ${relPath}\n\n`; |
| 22 | + for (const block of classBlocks) { |
| 23 | + const typeParams = block.templates.length > 0 ? `<${block.templates.join(", ")}>` : ""; |
| 24 | + const paramList = block.params.map((p) => `${p.name}: ${p.type}`).join(", "); |
| 25 | + markdown += `### ${block.name}\n\n`; |
| 26 | + markdown += `\`\`\`ts\nclass ${block.name}${typeParams}(${paramList}): ${block.name}${typeParams}\n\`\`\`\n\n`; |
| 27 | + markdown += `${block.description || "説明なし"}\n\n`; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fs.writeFileSync(outputPath, markdown, "utf-8"); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * ディレクトリを再帰的に探索してJSファイルを収集 |
| 36 | + * @param {string} dir |
| 37 | + * @param {string[]} fileList |
| 38 | + */ |
| 39 | +function walkDir(dir, fileList) { |
| 40 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 41 | + for (const entry of entries) { |
| 42 | + const fullPath = path.join(dir, entry.name); |
| 43 | + if (entry.isDirectory()) { |
| 44 | + walkDir(fullPath, fileList); |
| 45 | + } else if (entry.isFile() && entry.name.endsWith(".js")) { |
| 46 | + fileList.push(fullPath); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * クラス定義と直前のJSDocコメントを抽出(複数行のみ)を抽出 |
| 53 | + * @param {string} code - ファイルの内容 |
| 54 | + * @returns {{ name: string, description: string, templates: string[], params: string[] }[]} |
| 55 | + */ |
| 56 | +function extractClassDocs(code) { |
| 57 | + const classRegex = /\/\*\*((?:\s*\*(?:(?!\/\*\*)[\s\S])*?)\n\s*)\*\/\s*class\s+(\w+)/g; |
| 58 | + const results = []; |
| 59 | + |
| 60 | + let match; |
| 61 | + while ((match = classRegex.exec(code)) !== null) { |
| 62 | + const [fullMatch, commentBody, className] = match; |
| 63 | + const classStartIdx = match.index; |
| 64 | + |
| 65 | + // 1行JSDoc(/** ... */)は除外 |
| 66 | + const isSingleLine = fullMatch.split("\n").length === 1; |
| 67 | + if (isSingleLine) continue; |
| 68 | + |
| 69 | + const desc = parseJsDocDescription(commentBody); |
| 70 | + const templates = parseJsDocTemplates(commentBody); |
| 71 | + |
| 72 | + const constructorInfo = extractConstructorParams(code.slice(classStartIdx)); |
| 73 | + |
| 74 | + results.push({ name: className, description: desc, templates, params: constructorInfo.params }); |
| 75 | + } |
| 76 | + |
| 77 | + return results; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * JSDocコメントから説明部分を抽出(@description 優先) |
| 82 | + * @param {string} comment - JSDoc コメント |
| 83 | + * @returns {string} |
| 84 | + */ |
| 85 | +function parseJsDocDescription(comment) { |
| 86 | + const lines = comment.split("\n").map((line) => line.replace(/^\s*\*\s?/, "").trim()); |
| 87 | + const descriptionLine = lines.find((line) => line.startsWith("@description")); |
| 88 | + if (descriptionLine) return descriptionLine.replace("@description", "").trim(); |
| 89 | + |
| 90 | + // @descriptionが無ければ最初の行を説明とみなす |
| 91 | + for (const line of lines) { |
| 92 | + if (line && !line.startsWith("@")) return line; |
| 93 | + } |
| 94 | + |
| 95 | + return ""; |
| 96 | +} |
| 97 | + |
| 98 | +function parseJsDocTemplates(comment) { |
| 99 | + const lines = comment.split("\n"); |
| 100 | + const templateRegex = /@template\s+(\w+)/; |
| 101 | + const templates = []; |
| 102 | + for (const line of lines) { |
| 103 | + const match = line.match(templateRegex); |
| 104 | + if (match) templates.push(match[1]); |
| 105 | + } |
| 106 | + return templates; |
| 107 | +} |
| 108 | + |
| 109 | +function extractConstructorParams(classBody) { |
| 110 | + const constructorRegex = /\/\*\*((?:\s*\*(?!\/)[\s\S]*?)\*\/)\s*constructor\s*\(([^\)]*)\)/; |
| 111 | + const match = classBody.match(constructorRegex); |
| 112 | + |
| 113 | + if (!match) return { params: [] }; |
| 114 | + |
| 115 | + const jsdoc = match[1]; |
| 116 | + return { |
| 117 | + params: parseJsDocParams(jsdoc), |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +function parseJsDocParams(comment) { |
| 122 | + const lines = comment.split("\n"); |
| 123 | + const paramRegex = /@param\s+{([^}]+)}\s+(\w+)/; |
| 124 | + const params = []; |
| 125 | + for (const line of lines) { |
| 126 | + const match = line.match(paramRegex); |
| 127 | + if (match) { |
| 128 | + const [, type, name] = match; |
| 129 | + params.push({ name, type }); |
| 130 | + } |
| 131 | + } |
| 132 | + return params; |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = generateMarkdownFromClasses; |
0 commit comments