-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
327 lines (290 loc) · 9.19 KB
/
Copy pathextension.js
File metadata and controls
327 lines (290 loc) · 9.19 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const {
buildMatchers,
deriveAllowedExtensions,
buildExtensionSet,
collectFiles,
renderBlock,
suggestedOutputFileName,
atomicWriteFile,
normalizeMaxFileSizeKB,
parseRunFilterInput,
} = require('./lib/combiner');
function activate(context) {
const combineWorkspace = vscode.commands.registerCommand(
'codebaseCombiner.combineWorkspace',
handleCombineWorkspace
);
const combineFolder = vscode.commands.registerCommand(
'codebaseCombiner.combineFolder',
handleCombineFolder
);
context.subscriptions.push(combineWorkspace, combineFolder);
}
function deactivate() {}
async function handleCombineWorkspace() {
const config = getConfig();
const folder = await pickWorkspaceFolder();
if (!folder) {
return;
}
const filters = await promptForFilters(config);
if (!filters) {
return;
}
const outputPath = await promptForOutputPath(
folder.uri.fsPath,
suggestedOutputFileName(config.outputFileName, config.outputFormat)
);
if (!outputPath) {
return;
}
await combineRoot(folder.uri.fsPath, outputPath, { ...config, ...filters });
}
async function handleCombineFolder(uri) {
const config = getConfig();
let targetPath;
try {
if (uri && uri.fsPath) {
const stats = await fs.promises.lstat(uri.fsPath);
if (stats.isSymbolicLink()) {
throw new Error('Symbolic-link folder selections are not supported.');
}
targetPath = stats.isDirectory() ? uri.fsPath : path.dirname(uri.fsPath);
} else {
const folder = await pickWorkspaceFolder();
if (!folder) {
return;
}
targetPath = folder.uri.fsPath;
}
} catch (err) {
vscode.window.showErrorMessage(`Codebase Combiner: ${err.message}`);
return;
}
const filters = await promptForFilters(config);
if (!filters) {
return;
}
const outputPath = await promptForOutputPath(
targetPath,
suggestedOutputFileName(config.outputFileName, config.outputFormat)
);
if (!outputPath) {
return;
}
await combineRoot(targetPath, outputPath, { ...config, ...filters });
}
async function combineRoot(rootPath, outputAbsolute, config) {
let includeMatchers;
let excludeMatchers;
let includeExtensions;
let excludeExtensions;
let maxFileSizeKB;
try {
includeMatchers = buildMatchers(config.includeGlobs);
excludeMatchers = buildMatchers(config.excludeGlobs);
includeExtensions = buildExtensionSet(config.includeExtensions);
excludeExtensions = buildExtensionSet(config.excludeExtensions);
maxFileSizeKB = normalizeMaxFileSizeKB(config.maxFileSizeKB);
} catch (err) {
vscode.window.showErrorMessage(`Codebase Combiner: invalid filters: ${err.message}`);
return;
}
const allowedExtensions = includeExtensions.size
? includeExtensions
: config.useExtensionsFilter
? deriveAllowedExtensions(config.includeGlobs)
: new Set();
try {
await fs.promises.mkdir(path.dirname(outputAbsolute), { recursive: true });
} catch (err) {
vscode.window.showErrorMessage(
`Codebase Combiner: cannot create output folder: ${err.message}`
);
return;
}
try {
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Codebase Combiner: combining files…',
cancellable: true,
},
async (_progress, cancellationToken) => {
const controller = new AbortController();
const cancellation = cancellationToken.onCancellationRequested(() => controller.abort());
try {
const files = await collectFiles(
rootPath,
includeMatchers,
excludeMatchers,
allowedExtensions,
excludeExtensions,
maxFileSizeKB,
outputAbsolute,
{
maxFiles: 10000,
maxBytes: 64 * 1024 * 1024,
maxDepth: 128,
maxVisitedEntries: 50000,
signal: controller.signal,
}
);
if (controller.signal.aborted) throw cancellationError();
const content = files.map((file) => renderBlock(file, config.outputFormat)).join('');
if (controller.signal.aborted) throw cancellationError();
await atomicWriteFile(outputAbsolute, content, { signal: controller.signal });
const document = await vscode.workspace.openTextDocument(outputAbsolute);
await vscode.window.showTextDocument(document, { preview: false });
const skipped = Object.entries(files.skipSummary)
.filter(([, count]) => count > 0)
.map(([reason, count]) => `${reason} ${count}`)
.join(', ');
vscode.window.showInformationMessage(
`Codebase Combiner: combined ${files.length} file(s) into ${path.basename(outputAbsolute)}${skipped ? `; skipped items: ${skipped}` : ''}.`
);
} finally {
cancellation.dispose();
}
}
);
} catch (err) {
if (err.name === 'AbortError') {
vscode.window.showInformationMessage('Codebase Combiner: combination cancelled.');
return;
}
vscode.window.showErrorMessage(`Codebase Combiner failed: ${err.message}`);
}
}
function cancellationError() {
const error = new Error('Combination cancelled.');
error.name = 'AbortError';
return error;
}
function getConfig() {
const config = vscode.workspace.getConfiguration('codebaseCombiner');
return {
outputFormat: config.get('outputFormat', 'txt'),
outputFileName: config.get('outputFileName', 'combined_code.txt'),
includeGlobs: config.get('includeGlobs', ['**/*']),
excludeGlobs: config.get('excludeGlobs', [
'**/node_modules/**',
'**/.git/**',
'**/.vscode/**',
'**/dist/**',
'**/build/**',
]),
includeExtensions: config.get('includeExtensions', []),
excludeExtensions: config.get('excludeExtensions', [
'png',
'jpg',
'jpeg',
'gif',
'mp4',
'zip',
'bin',
'lock',
]),
maxFileSizeKB: config.get('maxFileSizeKB', 512),
useExtensionsFilter: config.get('useExtensionsFilter', true),
};
}
async function pickWorkspaceFolder() {
const folders = vscode.workspace.workspaceFolders;
if (!folders || folders.length === 0) {
vscode.window.showInformationMessage('Codebase Combiner: open a workspace or folder first.');
return null;
}
if (folders.length === 1) {
return folders[0];
}
const pick = await vscode.window.showQuickPick(
folders.map((f) => ({ label: f.name, description: f.uri.fsPath, folder: f })),
{ placeHolder: 'Select workspace folder to combine' }
);
return pick ? pick.folder : null;
}
async function promptForOutputPath(rootPath, defaultName) {
const uri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(rootPath, defaultName)),
filters: {
'Text and Markdown': ['txt', 'md'],
'All Files': ['*'],
},
saveLabel: 'Combine',
title: 'Choose combined output file',
});
return uri?.fsPath;
}
async function promptForFilters(config) {
const choice = await vscode.window.showQuickPick(
[
{
label: 'Use configured filters',
description: 'Use include/exclude globs from settings',
value: 'config',
},
{
label: 'Edit filters for this run',
description: 'Customize include/exclude globs',
value: 'edit',
},
],
{
placeHolder: 'Select filters to use for Codebase Combiner',
}
);
if (!choice) {
return null; // user cancelled
}
if (choice.value === 'config') {
return {
includeGlobs: config.includeGlobs,
excludeGlobs: config.excludeGlobs,
};
}
const includeInput = await vscode.window.showInputBox({
prompt: 'Include glob patterns (comma or newline separated)',
value: (config.includeGlobs || ['**/*']).join(', '),
ignoreFocusOut: true,
});
if (includeInput === undefined) {
return null;
}
const excludeInput = await vscode.window.showInputBox({
prompt: 'Exclude glob patterns (comma or newline separated)',
value: (config.excludeGlobs || []).join(', '),
ignoreFocusOut: true,
});
if (excludeInput === undefined) {
return null;
}
const includeExtInput = await vscode.window.showInputBox({
prompt: 'Only include these extensions (comma or space separated, leave empty for any)',
value: (config.includeExtensions || []).join(', '),
ignoreFocusOut: true,
});
if (includeExtInput === undefined) {
return null;
}
const excludeExtInput = await vscode.window.showInputBox({
prompt: 'Exclude these extensions (comma or space separated)',
value: (config.excludeExtensions || []).join(', '),
ignoreFocusOut: true,
});
if (excludeExtInput === undefined) {
return null;
}
const includeGlobs = parseRunFilterInput(includeInput, 'glob');
const excludeGlobs = parseRunFilterInput(excludeInput, 'glob');
const includeExtensions = parseRunFilterInput(includeExtInput, 'extension');
const excludeExtensions = parseRunFilterInput(excludeExtInput, 'extension');
return { includeGlobs, excludeGlobs, includeExtensions, excludeExtensions };
}
module.exports = {
activate,
deactivate,
};