feat: 重构模板管理,采用树状结构存储文件和目录,优化模板创建和编辑流程

This commit is contained in:
zhanggy committed 2025-10-28 18:34:31 +08:00
1 parent f8890ff1e9
commit 5ca31a5348
4 files changed
+351 -246

No files matched your search

+33 -18
View File
@@ -177,25 +177,40 @@ export default class IPCs {
const baseDirPath = join(targetPath, baseDirName)
ensureDirSync(baseDirPath)
// Create directories from template
for (const dir of template.directories) {
const dirPath = join(baseDirPath, dir.path)
ensureDirSync(dirPath)
}
// Create files from template
for (const file of template.files) {
const filePath = join(baseDirPath, file.path)
const fileDir = dirname(filePath)
ensureDirSync(fileDir)
// Helper function to traverse the tree and collect all nodes
const collectAllNodes = (nodes, allNodes = []) => {
for (const node of nodes) {
allNodes.push(node);
if (node.children) {
collectAllNodes(node.children, allNodes);
}
}
return allNodes;
};
// Collect all nodes from the tree structure
if (template.treeStructure && template.treeStructure.length > 0) {
const allNodes = collectAllNodes(template.treeStructure);
if (file.sourceFilePath) {
// Copy from source file
const sourceContent = readFileSync(file.sourceFilePath)
writeFileSync(filePath, sourceContent)
} else if (file.content) {
// Write content directly
writeFileSync(filePath, file.content, 'utf-8')
// Process all nodes, making sure directories are created before files
for (const node of allNodes) {
const nodePath = join(baseDirPath, node.path);
if (node.type === 'directory') {
ensureDirSync(nodePath);
} else if (node.type === 'file') {
const fileDir = dirname(nodePath);
ensureDirSync(fileDir);
if (node.sourceFilePath) {
// Copy from source file
const sourceContent = readFileSync(node.sourceFilePath);
writeFileSync(nodePath, sourceContent);
} else if (node.content) {
// Write content directly
writeFileSync(nodePath, node.content, 'utf-8');
}
}
}
}
}