|
| 1 | +function getListData() { |
| 2 | + const jsonText = document.getElementById("list-data")?.textContent || "{}"; |
| 3 | + /** @type {{controllers?: Array<{ |
| 4 | + * packageName: string, |
| 5 | + * typeName: string, |
| 6 | + * methodSignature: string, |
| 7 | + * returnType: string, |
| 8 | + * typeLabel: string, |
| 9 | + * usingFieldTypes: string[], |
| 10 | + * cyclomaticComplexity: number, |
| 11 | + * path: string |
| 12 | + * }>} | Array<{ |
| 13 | + * packageName: string, |
| 14 | + * typeName: string, |
| 15 | + * methodSignature: string, |
| 16 | + * returnType: string, |
| 17 | + * typeLabel: string, |
| 18 | + * usingFieldTypes: string[], |
| 19 | + * cyclomaticComplexity: number, |
| 20 | + * path: string |
| 21 | + * }>} */ |
| 22 | + const listData = JSON.parse(jsonText); |
| 23 | + if (Array.isArray(listData)) { |
| 24 | + return listData; |
| 25 | + } |
| 26 | + return listData.controllers ?? []; |
| 27 | +} |
| 28 | + |
| 29 | +function escapeCsvValue(value) { |
| 30 | + const text = String(value ?? "") |
| 31 | + .replace(/\r\n/g, "\n") |
| 32 | + .replace(/\r/g, "\n"); |
| 33 | + return `"${text.replace(/"/g, "\"\"")}"`; |
| 34 | +} |
| 35 | + |
| 36 | +function formatFieldTypes(fieldTypes) { |
| 37 | + if (!fieldTypes) return ""; |
| 38 | + if (Array.isArray(fieldTypes)) { |
| 39 | + return fieldTypes.join("\n"); |
| 40 | + } |
| 41 | + return String(fieldTypes); |
| 42 | +} |
| 43 | + |
| 44 | +function buildControllerCsv(items) { |
| 45 | + const header = [ |
| 46 | + "パッケージ名", |
| 47 | + "クラス名", |
| 48 | + "メソッドシグネチャ", |
| 49 | + "メソッド戻り値の型", |
| 50 | + "クラス別名", |
| 51 | + "使用しているフィールドの型", |
| 52 | + "循環的複雑度", |
| 53 | + "パス", |
| 54 | + ]; |
| 55 | + const rows = items.map(item => [ |
| 56 | + item.packageName ?? "", |
| 57 | + item.typeName ?? "", |
| 58 | + item.methodSignature ?? "", |
| 59 | + item.returnType ?? "", |
| 60 | + item.typeLabel ?? "", |
| 61 | + formatFieldTypes(item.usingFieldTypes), |
| 62 | + item.cyclomaticComplexity ?? "", |
| 63 | + item.path ?? "", |
| 64 | + ]); |
| 65 | + const lines = [header, ...rows].map(row => row.map(escapeCsvValue).join(",")); |
| 66 | + return lines.join("\r\n"); |
| 67 | +} |
| 68 | + |
| 69 | +function downloadCsv(text, filename) { |
| 70 | + const blob = new Blob([text], {type: "text/csv;charset=utf-8;"}); |
| 71 | + const url = URL.createObjectURL(blob); |
| 72 | + const anchor = document.createElement("a"); |
| 73 | + anchor.href = url; |
| 74 | + anchor.download = filename; |
| 75 | + document.body.appendChild(anchor); |
| 76 | + anchor.click(); |
| 77 | + anchor.remove(); |
| 78 | + URL.revokeObjectURL(url); |
| 79 | +} |
| 80 | + |
| 81 | +function renderControllerTable(items) { |
| 82 | + const tableBody = document.querySelector("#controller-list tbody"); |
| 83 | + if (!tableBody) return; |
| 84 | + tableBody.innerHTML = ""; |
| 85 | + |
| 86 | + const fragment = document.createDocumentFragment(); |
| 87 | + items.forEach(item => { |
| 88 | + const row = document.createElement("tr"); |
| 89 | + const values = [ |
| 90 | + item.packageName, |
| 91 | + item.typeName, |
| 92 | + item.methodSignature, |
| 93 | + item.returnType, |
| 94 | + item.typeLabel, |
| 95 | + formatFieldTypes(item.usingFieldTypes), |
| 96 | + item.cyclomaticComplexity, |
| 97 | + item.path, |
| 98 | + ]; |
| 99 | + values.forEach((value, index) => { |
| 100 | + const cell = document.createElement("td"); |
| 101 | + if (index === 6) { |
| 102 | + cell.className = "number"; |
| 103 | + } |
| 104 | + cell.textContent = value ?? ""; |
| 105 | + row.appendChild(cell); |
| 106 | + }); |
| 107 | + fragment.appendChild(row); |
| 108 | + }); |
| 109 | + |
| 110 | + tableBody.appendChild(fragment); |
| 111 | +} |
| 112 | + |
| 113 | +if (typeof document !== "undefined") { |
| 114 | + document.addEventListener("DOMContentLoaded", function () { |
| 115 | + if (!document.body.classList.contains("list-output")) return; |
| 116 | + const items = getListData(); |
| 117 | + renderControllerTable(items); |
| 118 | + |
| 119 | + const exportButton = document.getElementById("export-csv"); |
| 120 | + if (exportButton) { |
| 121 | + exportButton.addEventListener("click", () => { |
| 122 | + const csvText = buildControllerCsv(items); |
| 123 | + downloadCsv(csvText, "list-output.csv"); |
| 124 | + }); |
| 125 | + } |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +// Nodeのテスト用エクスポート。ブラウザでは無視される。 |
| 130 | +if (typeof module !== "undefined" && module.exports) { |
| 131 | + module.exports = { |
| 132 | + getListData, |
| 133 | + escapeCsvValue, |
| 134 | + formatFieldTypes, |
| 135 | + buildControllerCsv, |
| 136 | + renderControllerTable, |
| 137 | + }; |
| 138 | +} |
0 commit comments