-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
189 lines (156 loc) Β· 5.66 KB
/
extension.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
function activate(context) {
console.log("Folder Structure Generator is now active!");
let generateCommand = vscode.commands.registerCommand("extension.generateFolderStructure", async function () {
const document = await vscode.workspace.openTextDocument({
content: `# Enter your folder structure here
# You can use either of these formats:
# Format 1 (tree-like):
# src
# βββ app
# β βββ layout.tsx
# β βββ page.tsx
# βββ components
# βββ ExpenseForm.tsx
# Format 2 (indented):
# src/
# app/
# layout.tsx
# page.tsx
# components/
# ExpenseForm.tsx
`,
language: "plaintext",
});
await vscode.window.showTextDocument(document);
vscode.window.showInformationMessage(
'Enter your folder structure, then run the "Process Folder Structure" command'
);
});
let processCommand = vscode.commands.registerCommand("extension.processFolderStructure", async function () {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage("No active editor");
return;
}
try {
let workspaceFolder = vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0].uri : undefined;
const folderUri = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
openLabel: "Select Folder",
defaultUri: workspaceFolder,
});
if (!folderUri || folderUri.length === 0) {
vscode.window.showInformationMessage("Folder selection cancelled");
return;
}
const rootPath = folderUri[0].fsPath;
const input = editor.document.getText();
const { folderCount, fileCount } = createFolderStructure(rootPath, input);
const message = `Folder structure created successfully!\n${folderCount} folders and ${fileCount} files were created.`;
const action = await vscode.window.showInformationMessage(message, "Open Folder", "Generate Report");
if (action === "Open Folder") {
let uri = vscode.Uri.file(rootPath);
await vscode.commands.executeCommand("vscode.openFolder", uri);
} else if (action === "Generate Report") {
await generateReport(rootPath, input);
}
} catch (error) {
vscode.window.showErrorMessage(`Error creating folder structure: ${error.message}`);
console.error("Full error:", error);
}
});
context.subscriptions.push(generateCommand, processCommand);
}
function createFolderStructure(rootPath, input) {
const lines = input.split("\n").filter((line) => !line.trim().startsWith("#") && line.trim() !== "");
const isTreeFormat = lines.some((line) => line.includes("βββ") || line.includes("βββ") || line.includes("β"));
let folderCount = 0;
let fileCount = 0;
try {
if (isTreeFormat) {
({ folderCount, fileCount } = processTreeStructure(rootPath, lines));
} else {
({ folderCount, fileCount } = processIndentedStructure(rootPath, lines));
}
} catch (error) {
console.error("Error in createFolderStructure:", error);
throw error;
}
return { folderCount, fileCount };
}
function processTreeStructure(rootPath, lines) {
let folderCount = 0;
let fileCount = 0;
const stack = [{ path: rootPath, depth: -1 }];
lines.forEach((line, index) => {
try {
const depth = line.search(/[^\sβ]/); // Find the first non-space, non-β character
const name = line.replace(/^[β ]*[ββ]ββ\s*/, "").trim();
// Pop items from stack if we're at a shallower depth
while (stack.length > 1 && stack[stack.length - 1].depth >= depth) {
stack.pop();
}
const parentPath = stack[stack.length - 1].path;
const fullPath = path.join(parentPath, name);
if (name.includes(".")) {
fs.writeFileSync(fullPath, "");
fileCount++;
} else {
fs.mkdirSync(fullPath, { recursive: true });
folderCount++;
stack.push({ path: fullPath, depth });
}
} catch (error) {
console.error(`Error processing line ${index + 1}: ${line}`, error);
throw error;
}
});
return { folderCount, fileCount };
}
function processIndentedStructure(rootPath, lines) {
const stack = [{ path: rootPath, level: -1 }];
let folderCount = 0;
let fileCount = 0;
lines.forEach((line, index) => {
try {
const trimmedLine = line.trimStart();
const level = line.length - trimmedLine.length;
const name = trimmedLine.replace(/\/$/g, "");
while (stack.length > 1 && stack[stack.length - 1].level >= level) {
stack.pop();
}
const parentPath = stack[stack.length - 1].path;
const currentPath = path.join(parentPath, name);
if (name.includes(".")) {
fs.writeFileSync(currentPath, "");
fileCount++;
} else {
fs.mkdirSync(currentPath, { recursive: true });
folderCount++;
stack.push({ path: currentPath, level });
}
} catch (error) {
console.error(`Error processing line ${index + 1}: ${line}`, error);
throw error;
}
});
return { folderCount, fileCount };
}
async function generateReport(rootPath, input) {
const reportContent = `# Folder Structure Report\n\n\`\`\`\n${input}\n\`\`\``;
const reportPath = path.join(rootPath, "folder_structure_report.md");
fs.writeFileSync(reportPath, reportContent);
const uri = vscode.Uri.file(reportPath);
await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(uri);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};