-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
287 lines (233 loc) · 8.69 KB
/
build.js
File metadata and controls
287 lines (233 loc) · 8.69 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
const fs = require('fs').promises;
const path = require('path');
const puppeteer = require('puppeteer');
const { execSync } = require('child_process');
const GITHUB_USERNAME = 'ruthmade';
// Repos to exclude from the site
const EXCLUDED_REPOS = [
'ruthmade.github.io',
'obsidian-releases', // Fork
];
async function fetchRepos() {
console.log('Fetching repositories…');
const response = await fetch(`https://api.github.com/users/${GITHUB_USERNAME}/repos`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'ruthmade-site-builder'
}
});
if (!response.ok) {
throw new Error('GitHub API error: ' + response.status);
}
const repos = await response.json();
// Filter for public repos, exclude forks and specific repos
const filtered = repos.filter(function (repo) {
return !repo.private &&
!repo.fork &&
!EXCLUDED_REPOS.includes(repo.name);
});
// Sort by creation date, newest first
filtered.sort(function (a, b) {
return new Date(b.created_at) - new Date(a.created_at);
});
return filtered;
}
async function takeScreenshot(url, outputPath) {
console.log('Taking screenshot of ' + url + '…');
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1200, height: 800 });
await page.goto(url, {
waitUntil: 'networkidle0',
timeout: 30000
});
await new Promise(resolve => setTimeout(resolve, 1000));
await page.screenshot({
path: outputPath,
type: 'jpeg',
quality: 85
});
console.log('Screenshot saved: ' + outputPath);
return true;
} catch (error) {
console.error('Failed to screenshot ' + url + ':', error.message);
return false;
} finally {
await browser.close();
}
}
function generatePreviewSVG(repo) {
const title = repo.name;
const description = repo.description || 'A tool by ruthmade';
// Truncate description if too long
const maxDescLength = 60;
const truncatedDesc = description.length > maxDescLength
? description.substring(0, maxDescLength) + '…'
: description;
// Extract topics/tags for display
const topics = repo.topics || [];
const topicsText = topics.slice(0, 4).join(' • ');
return `<svg width="1200" height="800" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#1a1a2e;stop-opacity:1" />
<stop offset="100%" style="stop-color:#16213e;stop-opacity:1" />
</linearGradient>
</defs>
<rect width="100%" height="100%" fill="url(#grad)"/>
<!-- Title -->
<text x="600" y="340"
font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif"
font-size="96"
font-weight="700"
fill="#ffffff"
text-anchor="middle">
${title}
</text>
<!-- Description -->
<text x="600" y="420"
font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif"
font-size="32"
font-weight="400"
fill="#8892a0"
text-anchor="middle">
${truncatedDesc}
</text>
<!-- Topics -->
${topicsText ? `<text x="600" y="480"
font-family="ui-monospace, SFMono-Regular, monospace"
font-size="22"
fill="#6366f1"
text-anchor="middle">
${topicsText}
</text>` : ''}
<!-- GitHub icon hint -->
<text x="600" y="700"
font-family="system-ui, sans-serif"
font-size="20"
fill="#4a5568"
text-anchor="middle">
github.com/ruthmade/${title}
</text>
</svg>`;
}
async function generatePreviewImage(repo, outputPath) {
console.log('Generating preview card for ' + repo.name + '…');
const svgPath = outputPath.replace('.jpg', '.svg');
const svg = generatePreviewSVG(repo);
await fs.writeFile(svgPath, svg);
// Try to convert SVG to JPG using rsvg-convert
try {
execSync(`rsvg-convert "${svgPath}" -o "${outputPath}" -w 1200 -h 800 --format jpeg`, {
stdio: 'pipe'
});
console.log('Preview card saved: ' + outputPath);
// Clean up SVG
await fs.unlink(svgPath);
return true;
} catch (error) {
console.error('rsvg-convert failed, trying puppeteer fallback…');
// Fallback: use puppeteer to render SVG
try {
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
await page.setViewport({ width: 1200, height: 800 });
await page.setContent(`<!DOCTYPE html>
<html><body style="margin:0;padding:0;">
${svg}
</body></html>`);
await page.screenshot({
path: outputPath,
type: 'jpeg',
quality: 90
});
await browser.close();
await fs.unlink(svgPath);
console.log('Preview card saved (via puppeteer): ' + outputPath);
return true;
} catch (puppeteerError) {
console.error('Puppeteer fallback also failed:', puppeteerError.message);
return false;
}
}
}
// Words that should stay ALL CAPS
const PRESERVE_CAPS = ['csv', 'cli', 'api', 'url', 'html', 'css', 'sql', 'ai', 'ui', 'ux'];
function generateProjectHTML(repo, imageFilename, hasWebsite) {
const titleWords = repo.name.split('-');
const title = titleWords.map(function (word) {
if (PRESERVE_CAPS.includes(word.toLowerCase())) {
return word.toUpperCase();
}
return word.charAt(0).toUpperCase() + word.slice(1);
}).join(' ');
const description = repo.description || 'A useful tool.';
// Link to homepage if it exists, otherwise to GitHub repo
const url = hasWebsite ? repo.homepage : repo.html_url;
// Add a badge for CLI/non-web tools
const badge = hasWebsite ? '' : '<span class="cli-badge">CLI</span>';
return `
<a href="${url}" class="project-card" target="_blank" rel="noopener">
<img src="${imageFilename}" alt="${title}" class="project-screenshot">
<div class="project-info">
<div class="project-title">${title}${badge}</div>
<div class="project-description">${description}</div>
</div>
</a>`;
}
async function build() {
console.log('Starting build…');
const distDir = path.join(__dirname, 'dist');
await fs.mkdir(distDir, { recursive: true });
const repos = await fetchRepos();
console.log('Found ' + repos.length + ' repository(ies)');
const projectsHTML = [];
for (let i = 0; i < repos.length; i++) {
const repo = repos[i];
const imageFilename = repo.name + '.jpg';
const imagePath = path.join(distDir, imageFilename);
// Take screenshot if repo has any valid homepage URL
const hasWebsite = repo.homepage && (repo.homepage.startsWith('http://') || repo.homepage.startsWith('https://'));
try {
let success = false;
if (hasWebsite) {
// Take screenshot of the website
success = await takeScreenshot(repo.homepage, imagePath);
} else {
// Generate a preview card
success = await generatePreviewImage(repo, imagePath);
}
if (success) {
projectsHTML.push(generateProjectHTML(repo, imageFilename, hasWebsite));
}
} catch (error) {
console.error('Failed to process ' + repo.name + ':', error);
}
}
let html = await fs.readFile(path.join(__dirname, 'index.html'), 'utf-8');
html = html.replace(
'<!-- Projects will be inserted here by build script -->',
projectsHTML.join('\n')
);
try {
await fs.copyFile(
path.join(__dirname, 'profile.jpg'),
path.join(distDir, 'profile.jpg')
);
} catch (error) {
console.warn('Profile picture not found, skipping...');
}
await fs.writeFile(path.join(distDir, 'index.html'), html);
console.log('Build complete! Generated ' + projectsHTML.length + ' project cards.');
}
build().catch(function (error) {
console.error('Build failed:', error);
process.exit(1);
});