MCPcopy Create free account
hub / github.com/StepfenShawn/ShitCodify / filesToTree

Function filesToTree

src/utils/promptGenerator.ts:127–182  ·  view source on GitHub ↗
(files: File[])

Source from the content-addressed store, hash-verified

125
126// 辅助函数:将文件列表转换为树形结构
127export function filesToTree(files: File[]): FileNode[] {
128 const root: FileNode[] = [];
129 const pathMap: Record<string, FileNode> = {};
130
131 // 首先创建所有目录节点
132 for (const file of files) {
133 const pathParts = file.webkitRelativePath.split('/');
134 let currentPath = '';
135
136 for (let i = 0; i < pathParts.length - 1; i++) {
137 const part = pathParts[i];
138 if (!part) continue;
139
140 const parentPath = currentPath;
141 currentPath = currentPath ? `${currentPath}/${part}` : part;
142
143 if (!pathMap[currentPath]) {
144 const dirNode: FileNode = {
145 name: part,
146 path: currentPath,
147 isDirectory: true,
148 children: []
149 };
150
151 pathMap[currentPath] = dirNode;
152
153 if (parentPath) {
154 pathMap[parentPath].children?.push(dirNode);
155 } else {
156 root.push(dirNode);
157 }
158 }
159 }
160 }
161
162 // 然后添加文件节点
163 for (const file of files) {
164 const pathParts = file.webkitRelativePath.split('/');
165 const fileName = pathParts[pathParts.length - 1];
166 const parentPath = pathParts.slice(0, -1).join('/');
167
168 const fileNode: FileNode = {
169 name: fileName,
170 path: file.webkitRelativePath,
171 isDirectory: false,
172 };
173
174 if (parentPath && pathMap[parentPath]) {
175 pathMap[parentPath].children?.push(fileNode);
176 } else {
177 root.push(fileNode);
178 }
179 }
180
181 return root;
182}
183
184// 辅助函数:将单个文件转换为树节点

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected