-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-projects.js
More file actions
271 lines (223 loc) · 10.4 KB
/
build-projects.js
File metadata and controls
271 lines (223 loc) · 10.4 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
#!/usr/bin/env node
/**
* Build script to fetch GitHub projects and inject them into index.html
* Run this script during build/deploy to update project data.
*
* Usage: node build-projects.js
*
* Can be run via GitHub Actions with a daily cron schedule.
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const GITHUB_ORG = 'tre-systems';
const GITHUB_API_URL = `https://api.github.com/orgs/${GITHUB_ORG}/repos?sort=pushed&per_page=100`;
const INDEX_FILE = path.join(__dirname, 'index.html');
// Projects to exclude from display
const EXCLUDED_REPOS = ['tre-website'];
// Image patterns to exclude (badges, stats, avatars, etc.)
const EXCLUDED_IMAGE_HANDLES = [
'img.shields.io',
'badge.svg',
'github-readme-stats',
'github-readme-streak-stats',
'github-profile-summary-cards',
'ko-fi.com',
'buy-me-a-coffee',
'paypal.me',
'avatars.githubusercontent.com',
'contrib.rocks',
'license',
'twitter.svg',
'linkedin.svg',
'via.placeholder.com',
'placehold.co'
];
// Maximum number of projects to display
const MAX_PROJECTS = 100;
async function fetchReadme(repoName, defaultBranch, headers) {
const readmeUrl = `https://raw.githubusercontent.com/${GITHUB_ORG}/${repoName}/${defaultBranch}/README.md`;
try {
const response = await fetch(readmeUrl, { headers });
if (response.ok) {
return await response.text();
}
} catch (error) {
console.warn(`Could not fetch README for ${repoName}:`, error.message);
}
return null;
}
function extractFirstImage(readmeContent, repoName, defaultBranch) {
if (!readmeContent) return null;
// Matches both Markdown and HTML image tags
const markdownImageRegex = /!\[.*?\]\((.*?)\)/g;
const htmlImageRegex = /<img.*?src=["'](.*?)["'].*?>/g;
let match;
const imageUrls = [];
// Extract Markdown images
while ((match = markdownImageRegex.exec(readmeContent)) !== null) {
imageUrls.push(match[1]);
}
// Extract HTML images
while ((match = htmlImageRegex.exec(readmeContent)) !== null) {
imageUrls.push(match[1]);
}
// Filter out badges, logos, contributor avatars, and other unwanted images
const filteredImages = imageUrls.filter(url => {
const lowerUrl = url.toLowerCase();
return !EXCLUDED_IMAGE_HANDLES.some(domain => lowerUrl.includes(domain));
});
if (filteredImages.length === 0) return null;
let firstImage = filteredImages[0];
// Resolve relative paths
if (!firstImage.startsWith('http')) {
// Remove leading ./ or / if present
firstImage = firstImage.replace(/^(\.\/|\/)/, '');
firstImage = `https://raw.githubusercontent.com/${GITHUB_ORG}/${repoName}/${defaultBranch}/${firstImage}`;
}
return firstImage;
}
async function fetchProjects() {
console.log('Fetching projects from GitHub API...');
const headers = {
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'tre-website-static'
};
// Use GITHUB_TOKEN if available (for GitHub Actions)
if (process.env.GITHUB_TOKEN) {
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`;
}
const response = await fetch(GITHUB_API_URL, { headers });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}
const repos = await response.json();
// Filter and transform repos
const projects = [];
// Use a subset of projects for efficiency or the full list
const filteredRepos = repos
.filter(repo => !repo.private && !EXCLUDED_REPOS.includes(repo.name))
.sort((a, b) => new Date(b.pushed_at) - new Date(a.pushed_at))
.slice(0, MAX_PROJECTS);
for (const [index, repo] of filteredRepos.entries()) {
console.log(`Processing ${repo.name}...`);
const readmeContent = await fetchReadme(repo.name, repo.default_branch, headers);
const imageUrl = extractFirstImage(readmeContent, repo.name, repo.default_branch);
projects.push({
name: repo.name,
description: repo.description || '',
htmlUrl: repo.html_url,
homepageUrl: repo.homepage || null,
updatedAt: repo.updated_at,
imageUrl: imageUrl,
topics: repo.topics || []
});
}
// Only include projects that have a screenshot, description, and tags
const completeProjects = projects.filter(p =>
p.imageUrl && p.description && p.topics.length > 0
);
console.log(`Found ${projects.length} repos, ${completeProjects.length} with image, description, and tags`);
return completeProjects;
}
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', { month: 'short', year: 'numeric' });
}
function generateProjectCard(project, index) {
const linksClass = project.homepageUrl ? '' : ' project-links-full';
const btnClass = project.homepageUrl ? '' : ' project-btn-full';
const websiteLink = project.homepageUrl ? `
<a href="${project.homepageUrl}" target="_blank" rel="noopener noreferrer" class="project-btn" data-testid="project-website">
<div class="btn-fill"></div>
<span class="btn-text">Website</span>
</a>` : '';
const imageHtml = project.imageUrl ? `
<div class="project-image-container">
<img src="${project.imageUrl}" alt="${project.name} screenshot" class="project-image" loading="lazy" onerror="this.closest('.project-image-container').classList.add('placeholder');this.remove()">
</div>` : `
<div class="project-image-container placeholder">
<div class="placeholder-overlay"></div>
<svg class="placeholder-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
<circle cx="8.5" cy="8.5" r="1.5"></circle>
<polyline points="21 15 16 10 5 21"></polyline>
</svg>
</div>`;
const randomDelay = (((index * 7 + 3) % 10) * -1).toFixed(2); // Deterministic per-card animation offset
const projectLink = project.homepageUrl || project.htmlUrl;
return `
<!-- Project Card: ${project.name} -->
<div class="project-card" data-testid="project-card">
<div class="project-card-bg"></div>
<div class="project-image-wrapper" style="--anim-delay: ${randomDelay}s">
<a href="${projectLink}" target="_blank" rel="noopener noreferrer" class="project-image-link">
${imageHtml}
</a>
</div>
<div class="project-card-content">
<div class="project-header">
<h3 class="project-title" data-testid="project-title">${project.name}</h3>
<p class="project-description" data-testid="project-description">${escapeHtml(project.description)}</p>
</div>
<div class="project-topics">
${project.topics.slice(0, 6).map(topic => `<span class="topic-tag">${escapeHtml(topic)}</span>`).join('')}
</div>
<div class="project-footer">
<div class="project-links${linksClass}">${websiteLink}
<a href="${project.htmlUrl}" target="_blank" rel="noopener noreferrer" class="project-btn${btnClass}" data-testid="project-github">
<div class="btn-fill"></div>
<span class="btn-text">GitHub</span>
</a>
</div>
</div>
</div>
</div>`;
}
function escapeHtml(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function generateProjectsHtml(projects) {
return projects.map((project, index) => generateProjectCard(project, index)).join('\n');
}
async function updateIndexHtml(projectsHtml) {
console.log('Updating index.html...');
const html = fs.readFileSync(INDEX_FILE, 'utf8');
// Find and replace the project grid content
// Look for the project-grid div and replace its contents
const gridStartRegex = /<div class="project-grid"[^>]*data-testid="project-grid"[^>]*>/;
const gridEndMarker = '</div>\n </div>\n </section>\n\n <!-- About Section -->';
const startMatch = html.match(gridStartRegex);
if (!startMatch) {
throw new Error('Could not find project-grid in index.html');
}
const startIndex = startMatch.index + startMatch[0].length;
const endIndex = html.indexOf(gridEndMarker);
if (endIndex === -1) {
throw new Error('Could not find end of project-grid in index.html');
}
// Replace the content between start and end
const newHtml = html.slice(0, startIndex) + '\n' + projectsHtml + '\n ' + html.slice(endIndex);
fs.writeFileSync(INDEX_FILE, newHtml);
console.log('index.html updated successfully!');
}
async function main() {
try {
const projects = await fetchProjects();
const projectsHtml = generateProjectsHtml(projects);
await updateIndexHtml(projectsHtml);
console.log('Build complete!');
} catch (error) {
console.error('Build failed:', error.message);
process.exit(1);
}
}
main();