forked from WoHinDu/AI-Project-Brief
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
154 lines (124 loc) · 5.5 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
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const ignore = require('ignore');
const minimatch = require('minimatch');
async function getCombinedContent(rootPath) {
// Get settings
const config = vscode.workspace.getConfiguration('aiProjectBrief');
const filePatterns = config.get('filePatterns', []);
const respectGitignore = config.get('respectGitignore', true);
const customPrefixText = config.get('customPrefixText', '');
const customSuffixText = config.get('customSuffixText', '');
const gitignorePath = path.join(rootPath, '.gitignore');
let ig = ignore();
if (respectGitignore && fs.existsSync(gitignorePath)) {
const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8');
ig = ignore().add(gitignoreContent);
}
let combinedContent = customPrefixText ? customPrefixText + '\n\n' : '';
async function processDirectory(dirPath) {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dirPath, entry.name);
const relativePath = path.relative(rootPath, fullPath);
if (respectGitignore && ig.ignores(relativePath)) {
continue;
}
if (entry.isDirectory()) {
await processDirectory(fullPath);
} else if (entry.isFile()) {
let action = 'include'; // Default action
// Check if the file matches any of the patterns
for (const pattern of filePatterns) {
if (minimatch(relativePath, pattern.pattern)) {
action = pattern.action;
break;
}
}
switch (action) {
case 'include':
const content = await fs.promises.readFile(fullPath, 'utf8');
combinedContent += `\n\n=== FILE: ${relativePath} ===\n\n${content}`;
break;
case 'pathOnly':
combinedContent += `\n\n=== FILE: ${relativePath} ===\n`;
break;
case 'ignore':
// Do nothing
break;
}
}
}
}
await processDirectory(rootPath);
if (customSuffixText) {
combinedContent += '\n\n' + customSuffixText;
}
return combinedContent.trim();
}
function activate(context) {
let combineFilesCommand = vscode.commands.registerCommand('aiProjectBrief.combineFiles', async function () {
const workspaceFolder = vscode.workspace.workspaceFolders[0];
if (!workspaceFolder) {
vscode.window.showErrorMessage('No workspace folder open');
return;
}
try {
const combinedContent = await getCombinedContent(workspaceFolder.uri.fsPath);
// Create a new untitled document
const document = await vscode.workspace.openTextDocument({
content: combinedContent,
language: 'text'
});
// Show the document in the editor
await vscode.window.showTextDocument(document);
vscode.window.showInformationMessage('Files combined successfully in a new unsaved file!');
} catch (error) {
vscode.window.showErrorMessage(`Error combining files: ${error.message}`);
}
});
let combineFilesToClipboardCommand = vscode.commands.registerCommand('aiProjectBrief.combineFilesToClipboard', async function () {
const workspaceFolder = vscode.workspace.workspaceFolders[0];
if (!workspaceFolder) {
vscode.window.showErrorMessage('No workspace folder open');
return;
}
try {
const combinedContent = await getCombinedContent(workspaceFolder.uri.fsPath);
// Copy to clipboard
await vscode.env.clipboard.writeText(combinedContent);
vscode.window.showInformationMessage('Combined files copied to clipboard!');
} catch (error) {
vscode.window.showErrorMessage(`Error combining files: ${error.message}`);
}
});
let editCustomTextsCommand = vscode.commands.registerCommand('aiProjectBrief.editCustomTexts', async function () {
const config = vscode.workspace.getConfiguration('aiProjectBrief');
const customPrefixText = config.get('customPrefixText', '');
const customSuffixText = config.get('customSuffixText', '');
const prefixText = await vscode.window.showInputBox({
prompt: 'Enter custom prefix text',
value: customPrefixText,
multiline: true
});
const suffixText = await vscode.window.showInputBox({
prompt: 'Enter custom suffix text',
value: customSuffixText,
multiline: true
});
if (prefixText !== undefined) {
await config.update('customPrefixText', prefixText, vscode.ConfigurationTarget.Workspace);
}
if (suffixText !== undefined) {
await config.update('customSuffixText', suffixText, vscode.ConfigurationTarget.Workspace);
}
vscode.window.showInformationMessage('Custom texts updated successfully!');
});
context.subscriptions.push(combineFilesCommand, combineFilesToClipboardCommand, editCustomTextsCommand);
}
function deactivate() { }
module.exports = {
activate,
deactivate
};