module-migration-tool
Version:
分析项目文件依赖并迁移到新项目的工具
234 lines (214 loc) • 6.04 kB
JavaScript
/**
* HTML依赖报告生成器
*/
const fs = require("fs")
const path = require("path")
/**
* 生成HTML依赖报告
* @param {Map<string, string[]>} dependencyMap - 依赖映射表
* @param {string} outputPath - 输出路径
* @param {Object} options - 附加选项
*/
function generateReport(dependencyMap, outputPath, options = {}) {
const { title = "项目依赖关系报告", projectRoot = "" } = options
console.log("生成依赖关系报表...")
// 生成HTML内容
let reportContent = `
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>
body {
font-family: Arial, "Microsoft YaHei", sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
color: #333;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
header {
margin-bottom: 20px;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
h1 {
color: #2c3e50;
margin-top: 0;
}
.stats {
background-color: #f8f9fa;
padding: 15px;
border-radius: 4px;
margin-bottom: 20px;
border-left: 4px solid #007bff;
}
.file-node {
margin: 10px 0;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
background-color: #fff;
}
.file-node:hover {
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.file-path {
font-weight: bold;
color: #0066cc;
cursor: pointer;
padding: 5px;
background-color: #f0f7ff;
border-radius: 3px;
}
.dependencies {
margin-left: 20px;
margin-top: 10px;
display: none;
}
.dependencies.active {
display: block;
}
.dependency {
margin: 5px 0;
color: #555;
padding: 3px 6px;
border-radius: 3px;
background-color: #f8f9fa;
}
.search-container {
margin-bottom: 20px;
}
.search-input {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.timestamp {
font-size: 12px;
color: #777;
}
.filter-container {
margin-bottom: 15px;
}
.filter-label {
margin-right: 15px;
}
.resolved {
color: #28a745;
}
.unresolved {
color: #dc3545;
}
.toggle-all {
margin-bottom: 15px;
background-color: #0066cc;
color: white;
border: none;
padding: 8px 15px;
border-radius: 4px;
cursor: pointer;
}
.toggle-all:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>${title}</h1>
<p class="timestamp">生成时间: ${new Date().toLocaleString()}</p>
</header>
<div class="stats">
<p><strong>文件总数:</strong> ${dependencyMap.size}</p>
<p><strong>依赖总数:</strong> ${Array.from(dependencyMap.values()).reduce(
(acc, deps) => acc + deps.length,
0
)}</p>
</div>
<div class="filter-container">
<button class="toggle-all" id="toggleAll">展开/折叠全部</button>
</div>
<div class="search-container">
<input type="text" class="search-input" id="searchInput" placeholder="搜索文件路径...">
</div>
<div id="dependency-tree">
`
// 遍历依赖映射表
for (const [file, deps] of dependencyMap.entries()) {
const relativePath = projectRoot ? path.relative(projectRoot, file) : file
reportContent += `
<div class="file-node">
<div class="file-path" onclick="toggleDependencies(this)">${relativePath} (${deps.length})</div>
<div class="dependencies">
`
if (deps.length === 0) {
reportContent += ` <div class="dependency">无依赖</div>\n`
} else {
for (const dep of deps) {
// 这里可以添加逻辑来标记依赖是否已解析
const isResolved = dep.resolved ? "resolved" : "unresolved"
reportContent += ` <div class="dependency ${isResolved}">${
dep.source || dep
} ${dep.resolved ? "-> " + dep.resolved : ""}</div>\n`
}
}
reportContent += ` </div>
</div>
`
}
reportContent += `
</div>
</div>
<script>
function toggleDependencies(element) {
const dependencies = element.nextElementSibling;
dependencies.classList.toggle('active');
}
document.getElementById('searchInput').addEventListener('input', function() {
const searchTerm = this.value.toLowerCase();
const fileNodes = document.querySelectorAll('.file-node');
fileNodes.forEach(node => {
const filePath = node.querySelector('.file-path').textContent.toLowerCase();
if (filePath.includes(searchTerm)) {
node.style.display = 'block';
} else {
node.style.display = 'none';
}
});
});
document.getElementById('toggleAll').addEventListener('click', function() {
const allDependencies = document.querySelectorAll('.dependencies');
const anyActive = Array.from(allDependencies).some(el => el.classList.contains('active'));
allDependencies.forEach(el => {
if (anyActive) {
el.classList.remove('active');
} else {
el.classList.add('active');
}
});
});
</script>
</body>
</html>
`
// 写入文件
fs.writeFileSync(outputPath, reportContent)
console.log(`依赖报表已生成: ${outputPath}`)
}
module.exports = {
generateReport,
}