-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputFormatter.js
65 lines (61 loc) · 1.5 KB
/
outputFormatter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function formatXml(files) {
if (files.length === 1) {
const file = files[0];
return [
"<file>",
` <path>${file.path}</path>`,
" <content><![CDATA[",
file.content,
" ]]></content>",
"</file>",
].join("\n");
}
let output = "<files>\n";
for (const file of files) {
output += [
" <file>",
` <path>${file.path}</path>`,
" <content><![CDATA[",
file.content,
" ]]></content>",
" </file>\n",
].join("\n");
}
output += "</files>\n";
output += "<file_list>\n";
output += files.map((file) => file.path).join("\n");
output += "\n</file_list>";
return output;
}
function formatJson(files) {
if (files.length === 1) {
return JSON.stringify(files[0]);
}
return JSON.stringify({
files: files.map((file) => ({ path: file.path, content: file.content })),
file_list: files.map((file) => file.path),
});
}
function formatCodeblocks(files) {
let output = "";
for (const file of files) {
output += `File: ${file.path}\n\`\`\`\n${file.content}\n\`\`\`\n\n`;
}
if (files.length > 1) {
output += "File list:\n";
output += files.map((file) => file.path).join("\n");
}
return output;
}
export function formatOutput(files, format) {
switch (format) {
case "xml":
return formatXml(files);
case "json":
return formatJson(files);
case "codeblocks":
return formatCodeblocks(files);
default:
throw new Error(`Unsupported format: ${format}`);
}
}