-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
189 lines (155 loc) · 6.06 KB
/
background.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 API_BASE_URL = 'https://api.github.com';
let githubToken = '';
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'setToken') {
githubToken = request.token;
sendResponse({success: true});
} else if (request.action === 'fetchFiles') {
githubToken = request.token; // Set the token from the message
fetchRepoFiles(request.repo, request.mergyignoreRules)
.then(files => sendResponse({files: files}))
.catch(error => sendResponse({error: error.message}));
return true;
} else if (request.action === 'downloadFiles') {
combineAndDownloadFiles(request.repo, request.files)
.then(content => sendResponse({content: content}))
.catch(error => sendResponse({error: error.message}));
return true;
}
});
async function fetchRepoFiles(repo, mergyignoreRules) {
if (!githubToken) {
throw new Error('GitHub token not set. Please set your Personal Access Token.');
}
const files = await listRepoContents(repo, '', mergyignoreRules);
return files.sort((a, b) => b.size - a.size);
}
async function listRepoContents(repo, path = '', mergyignoreRules) {
const url = `${API_BASE_URL}/repos/${repo.owner}/${repo.name}/contents/${path}`;
const response = await fetch(url, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `token ${githubToken}`
}
});
if (!response.ok) {
throw new Error(`GitHub API request failed: ${response.statusText}`);
}
const contents = await response.json();
let files = [];
for (const item of contents) {
const fullPath = path ? `${path}/${item.name}` : item.name;
if (shouldIncludeFile(fullPath, item.name, mergyignoreRules)) {
if (item.type === 'file') {
files.push({
name: item.name,
path: fullPath,
size: item.size,
downloadUrl: item.download_url
});
} else if (item.type === 'dir') {
const subFiles = await listRepoContents(repo, fullPath, mergyignoreRules);
files = files.concat(subFiles);
}
}
}
return files;
}
function shouldIncludeFile(fullPath, fileName, rules) {
const extension = '.' + fileName.split('.').pop();
// Ensure all rule arrays exist, if not, create empty arrays
rules.includePath = rules.includePath || [];
rules.includeExtension = rules.includeExtension || [];
rules.excludePath = rules.excludePath || [];
rules.excludeExtension = rules.excludeExtension || [];
// Check include rules
if (rules.includePath.length > 0 || rules.includeExtension.length > 0) {
const pathIncluded = rules.includePath.length === 0 || rules.includePath.some(path => fullPath.includes(path));
const extensionIncluded = rules.includeExtension.length === 0 || rules.includeExtension.includes(extension);
if (!pathIncluded || !extensionIncluded) {
return false;
}
}
// Check exclude rules
if (rules.excludePath.some(path => fullPath.includes(path))) {
return false;
}
if (rules.excludeExtension.includes(extension)) {
return false;
}
return true;
}
function parseMergyIgnoreRules(rulesString) {
return rulesString.split('\n')
.map(rule => rule.trim())
.filter(rule => rule && !rule.startsWith('#'))
.map(rule => new RegExp(rule.replace(/\*/g, '.*').replace(/\?/g, '.'), 'i'));
}
function shouldIgnore(path, ignorePatterns) {
return ignorePatterns.some(pattern => pattern.test(path));
}
async function combineAndDownloadFiles(repo, files) {
// Implement file combination and download
// This is a placeholder and needs to be implemented
console.log('Combining and downloading files:', files);
return 'download_url_placeholder';
}
async function combineAndDownloadFiles(repo, filePaths) {
let combinedContent = '';
for (const filePath of filePaths) {
const fileContent = await fetchFileContent(repo, filePath);
combinedContent += `// File: ${filePath}\n`;
combinedContent += fileContent;
combinedContent += '\n\n';
}
const optimizedContent = optimizeWhitespace(combinedContent);
// Instead of creating a Blob and URL here, we'll return the content
return optimizedContent;
}
async function fetchFileContent(repo, filePath) {
const url = `${API_BASE_URL}/repos/${repo.owner}/${repo.name}/contents/${filePath}`;
const response = await fetch(url, {
headers: {
'Accept': 'application/vnd.github.v3.raw',
'Authorization': `token ${githubToken}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch file content: ${response.statusText}`);
}
return await response.text();
}
function optimizeWhitespace(content) {
const lines = content.split('\n');
const optimizedLines = [];
let previousLineEmpty = false;
for (let line of lines) {
// Trim trailing whitespace
line = line.trimEnd();
// Remove leading whitespace, but keep a single space for indentation if present
const indent = line.match(/^\s+/);
if (indent) {
line = ' ' + line.trimStart();
}
if (line === '') {
if (!previousLineEmpty) {
optimizedLines.push('');
previousLineEmpty = true;
}
} else {
optimizedLines.push(line);
previousLineEmpty = false;
}
}
return optimizedLines.join('\n');
}
async function combineAndDownloadFiles(repo, filePaths) {
let combinedContent = '';
for (const filePath of filePaths) {
const fileContent = await fetchFileContent(repo, filePath);
combinedContent += `// File: ${filePath}\n`;
combinedContent += fileContent;
combinedContent += '\n\n';
}
return optimizeWhitespace(combinedContent);
}