-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreateStructure.ts
More file actions
171 lines (162 loc) · 7.95 KB
/
createStructure.ts
File metadata and controls
171 lines (162 loc) · 7.95 KB
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
import * as vscode from 'vscode';
import { ERROR_MESSAGES } from '../constants';
import { StructureService } from '../services/structure';
import { FileSystemService } from '../services/fileSystem';
import { OutputFormat, WebviewMessage } from '../types';
import { createStructureInputPanel } from '../ui/webview';
import { DEFAULT_OUTPUT_FORMAT } from '../constants';
export async function createStructure(): Promise<void> {
try {
const defaultUri = vscode.workspace.workspaceFolders?.[0]?.uri;
const pick = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
defaultUri,
openLabel: 'Select target folder',
});
const resolvedUri = pick?.[0];
if (!resolvedUri) {
throw new Error(ERROR_MESSAGES.TARGET_REQUIRED);
}
const initialFormat = vscode.workspace
.getConfiguration('folderStructure')
.get<OutputFormat>('outputFormat', DEFAULT_OUTPUT_FORMAT);
const panel = createStructureInputPanel(initialFormat);
panel.webview.onDidReceiveMessage(async (message: WebviewMessage | any) => {
if (message.command === 'validate') {
const currentFormat = (message.format as OutputFormat) || initialFormat;
if (currentFormat === 'Plain Text Format') {
const { structure, invalidLines } = StructureService.parsePlainTextToStructure(
message.text ?? '',
);
const preview = StructureService.formatAsTree(structure);
const hasContent = Object.keys(structure).length > 0;
panel.webview.postMessage({
command: 'validationResult',
valid: invalidLines.length === 0 && hasContent,
invalidLines,
preview,
errorMessage: hasContent ? undefined : 'Empty input or no valid lines.',
});
} else {
try {
const obj = JSON.parse(message.text ?? '{}');
const valid = StructureService.validateJsonStructure(obj);
const preview = valid ? StructureService.formatAsTree(obj) : '';
panel.webview.postMessage({
command: 'validationResult',
valid,
invalidLines: [],
preview,
errorMessage: valid
? undefined
: 'JSON structure must be nested objects with string file types.',
});
} catch (e) {
panel.webview.postMessage({
command: 'validationResult',
valid: false,
invalidLines: [],
preview: '',
errorMessage: 'Invalid JSON: ' + (e as Error).message,
});
}
}
} else if (message.command === 'submit') {
try {
// Determine existing targets and prompt for replacement
let targets: string[] = [];
const currentFormat = (message.format as OutputFormat) || initialFormat;
if (currentFormat === 'Plain Text Format') {
const { structure, invalidLines } =
StructureService.parsePlainTextToStructure(message.text ?? '');
const hasContent = Object.keys(structure).length > 0;
if (invalidLines.length > 0 || !hasContent) {
const detail = !hasContent
? 'Empty input'
: `Invalid lines: ${invalidLines.join(', ')}`;
const confirm = await vscode.window.showWarningMessage(
`The input appears invalid (${detail}). Continue and create only recognized items?`,
{ modal: true },
'Yes',
'No',
);
if (confirm !== 'Yes') {
return;
}
}
targets = Object.keys(structure).map((k) => k);
} else {
try {
const obj = JSON.parse(message.text ?? '{}');
if (!StructureService.validateJsonStructure(obj)) {
vscode.window.showErrorMessage(
'Invalid JSON structure. Please use nested objects and string file types for files.',
);
return;
}
targets = Object.keys(obj);
} catch (e) {
vscode.window.showErrorMessage('Invalid JSON: ' + (e as Error).message);
return;
}
}
const existing: string[] = [];
for (const name of targets) {
const full = vscode.Uri.joinPath(resolvedUri, name);
if (await FileSystemService.exists(full)) {
existing.push(name);
}
}
if (existing.length > 0) {
const selection = await vscode.window.showWarningMessage(
`The following items already exist: ${existing.join(', ')}. Replace them? (Replaced items go to Trash)`,
{ modal: true },
'Replace',
'Skip',
'Cancel',
);
if (selection === 'Cancel' || !selection) {
return;
}
if (selection === 'Replace') {
for (const name of existing) {
const full = vscode.Uri.joinPath(resolvedUri, name);
await FileSystemService.delete(full, {
recursive: true,
useTrash: true,
});
}
}
// On 'Skip', continue without deleting and we won't overwrite existing files
}
await StructureService.createStructure(
resolvedUri,
message.text,
currentFormat,
);
vscode.window.showInformationMessage('Project created successfully!');
panel.dispose();
} catch (error) {
vscode.window.showErrorMessage(
`Error processing folder structure: ${(error as Error).message}`,
);
}
} else if (message.command === 'copyPreview') {
try {
await vscode.env.clipboard.writeText(message.text ?? '');
vscode.window.showInformationMessage('Preview copied to clipboard');
} catch (e) {
vscode.window.showErrorMessage('Failed to copy preview');
}
}
});
} catch (error) {
vscode.window.showErrorMessage(`Failed to create project: ${(error as Error).message}`);
}
}
/*
* Copyright (c) 2025 Shrey Purohit.
* This code is licensed under the MIT License.
*/