2026-10-11 00:53:10 +07:00
|
|
|
import { chromium } from 'playwright';
|
2026-10-19 18:51:27 +07:00
|
|
|
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
|
|
|
import { dirname, join, resolve } from 'node:path';
|
2026-10-22 07:29:06 +07:00
|
|
|
import { CONTENT, UI } from '../src/i18n/index.js';
|
|
|
|
|
import type { ResolvedExperienceEntry, ResolvedProject, ResolvedSkillGroup } from '../src/i18n/index.js';
|
2026-10-19 18:51:27 +07:00
|
|
|
import { resumeQr } from '../src/data/resume.js';
|
|
|
|
|
import { siteUrl } from '../src/data/site.js';
|
2026-10-12 14:50:17 +07:00
|
|
|
import type { Language } from '../src/types/index.js';
|
|
|
|
|
|
2026-10-11 00:53:10 +07:00
|
|
|
const ROOT = resolve(import.meta.dirname, '..');
|
|
|
|
|
|
2026-10-12 14:50:17 +07:00
|
|
|
// --- CLI args ---
|
|
|
|
|
const args = process.argv.slice(2);
|
2026-10-19 18:51:27 +07:00
|
|
|
const langArg = (args.find(a => a.startsWith('--lang=')) || '').replace('--lang=', '');
|
2026-10-15 15:00:22 +07:00
|
|
|
const previewMode = args.includes('--preview');
|
2026-10-12 14:50:17 +07:00
|
|
|
|
2026-10-19 18:51:27 +07:00
|
|
|
let langs: Language[];
|
|
|
|
|
if (langArg === 'all') {
|
|
|
|
|
if (previewMode) {
|
|
|
|
|
console.error('❌ --lang=all cannot be combined with --preview (preview renders one language)');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
langs = ['en', 'vi'];
|
|
|
|
|
} else {
|
|
|
|
|
langs = [langArg === 'vi' ? 'vi' : 'en'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 00:53:10 +07:00
|
|
|
// --- Validate required files ---
|
|
|
|
|
const requiredFiles = [
|
|
|
|
|
'public/profile-photo.webp',
|
2026-10-15 15:00:22 +07:00
|
|
|
'public/fonts/SourceSans3-Regular.woff2',
|
|
|
|
|
'public/fonts/SourceSans3-Medium.woff2',
|
|
|
|
|
'public/fonts/SourceSans3-Semibold.woff2',
|
|
|
|
|
'public/fonts/SourceSans3-Bold.woff2',
|
2026-10-11 00:53:10 +07:00
|
|
|
'public/resume-template.html',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const file of requiredFiles) {
|
|
|
|
|
if (!existsSync(join(ROOT, file))) {
|
|
|
|
|
console.error(`❌ Missing required file: ${file}`);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Read template ---
|
|
|
|
|
const template = readFileSync(join(ROOT, 'public/resume-template.html'), 'utf-8');
|
|
|
|
|
|
|
|
|
|
// --- Read and base64-encode assets ---
|
|
|
|
|
const photoBase64 = readFileSync(join(ROOT, 'public/profile-photo.webp')).toString('base64');
|
|
|
|
|
|
|
|
|
|
const fonts: Record<string, string> = {};
|
|
|
|
|
for (const [name, weight] of [
|
2026-10-15 15:00:22 +07:00
|
|
|
['REGULAR', 'SourceSans3-Regular.woff2'],
|
|
|
|
|
['MEDIUM', 'SourceSans3-Medium.woff2'],
|
2026-10-19 18:51:27 +07:00
|
|
|
['SEMIBOLD', 'SourceSans3-Semibold.woff2',],
|
2026-10-15 15:00:22 +07:00
|
|
|
['BOLD', 'SourceSans3-Bold.woff2'],
|
2026-10-11 00:53:10 +07:00
|
|
|
] as const) {
|
|
|
|
|
fonts[name] = readFileSync(join(ROOT, 'public/fonts', weight)).toString('base64');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Generate font-face CSS ---
|
|
|
|
|
const fontFaces = Object.entries(fonts).map(([key, b64]) => {
|
|
|
|
|
const weightMap: Record<string, number> = { REGULAR: 400, MEDIUM: 500, SEMIBOLD: 600, BOLD: 700 };
|
|
|
|
|
return `@font-face {
|
2026-10-15 15:00:22 +07:00
|
|
|
font-family: 'Source Sans 3';
|
2026-10-11 00:53:10 +07:00
|
|
|
font-style: normal;
|
|
|
|
|
font-weight: ${weightMap[key]};
|
|
|
|
|
font-display: swap;
|
|
|
|
|
src: url('data:font/woff2;base64,${b64}') format('woff2');
|
|
|
|
|
}`;
|
|
|
|
|
}).join('\n');
|
|
|
|
|
|
2026-10-19 18:51:27 +07:00
|
|
|
// --- Company logos: load every logo referenced by experience entries ---
|
2026-10-22 07:29:06 +07:00
|
|
|
const logoIds = [...new Set(CONTENT.en.experiences.map(exp => exp.logo).filter((id): id is string => id !== undefined))];
|
2026-10-19 18:51:27 +07:00
|
|
|
const logos: Record<string, string> = {};
|
|
|
|
|
for (const id of logoIds) {
|
|
|
|
|
const logoFile = join(ROOT, 'public/resume-logos', `${id}.svg`);
|
|
|
|
|
if (!existsSync(logoFile)) {
|
|
|
|
|
console.error(`❌ Missing logo asset: public/resume-logos/${id}.svg`);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
logos[id] = readFileSync(logoFile).toString('base64');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- QR codes: owner-exported images from the Telegram/Zalo apps ---
|
|
|
|
|
// These are private, owner-supplied assets (not committed). A missing file is
|
|
|
|
|
// reported loudly but does not block the build — the resume still ships without it.
|
|
|
|
|
const qrMime: Record<string, string> = { png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg' };
|
|
|
|
|
const qrAssets: { label: string; dataUri: string }[] = [];
|
|
|
|
|
for (const q of resumeQr) {
|
|
|
|
|
const file = join(ROOT, 'public/resume-qr', q.file);
|
|
|
|
|
if (!existsSync(file)) {
|
|
|
|
|
console.warn(`⚠️ QR asset missing: public/resume-qr/${q.file} — ${q.label} QR skipped`);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const ext = q.file.split('.').pop() ?? '';
|
|
|
|
|
const mime = qrMime[ext] ?? 'image/png';
|
|
|
|
|
qrAssets.push({ label: q.label, dataUri: `data:${mime};base64,${readFileSync(file).toString('base64')}` });
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-12 14:50:17 +07:00
|
|
|
// --- Version stamp date ---
|
|
|
|
|
const updatedDate = new Date().toISOString().slice(0, 10);
|
|
|
|
|
|
2026-10-22 07:29:06 +07:00
|
|
|
// (Removed: the inline `labels` map and `resolveField` — labels now live in
|
|
|
|
|
// the UI dictionaries and text arrives pre-resolved through CONTENT.)
|
2026-10-12 14:50:17 +07:00
|
|
|
|
2026-10-19 18:51:27 +07:00
|
|
|
|
|
|
|
|
// --- Escape data strings before injection ---
|
|
|
|
|
function escapeHtml(value: string): string {
|
|
|
|
|
return value
|
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
|
.replace(/</g, '<')
|
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
|
.replace(/"/g, '"');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QR block: owner-exported Telegram/Zalo QR images, right side of the header
|
|
|
|
|
function renderQr(lang: Language): string {
|
|
|
|
|
return qrAssets
|
|
|
|
|
.map(q => `
|
|
|
|
|
<div class="qr-item">
|
2026-10-22 07:29:06 +07:00
|
|
|
<img class="qr-img" src="${q.dataUri}" alt="${escapeHtml(`${q.label} ${UI[lang].resume.QR_ALT}`)}" />
|
2026-10-19 18:51:27 +07:00
|
|
|
<span class="qr-label">${escapeHtml(q.label)}</span>
|
|
|
|
|
</div>`)
|
|
|
|
|
.join('');
|
2026-10-12 14:50:17 +07:00
|
|
|
}
|
|
|
|
|
|
2026-10-11 00:53:10 +07:00
|
|
|
// --- Render HTML helpers ---
|
2026-10-22 07:29:06 +07:00
|
|
|
function renderExperience(exps: ResolvedExperienceEntry[]): string {
|
2026-10-19 18:51:27 +07:00
|
|
|
return exps
|
|
|
|
|
// open-to-work (variant 'current') is excluded from the resume PDF entirely
|
|
|
|
|
.filter(exp => exp.variant !== 'current')
|
|
|
|
|
.map(exp => {
|
|
|
|
|
const variantClass = exp.variant ? ` ${exp.variant}` : '';
|
|
|
|
|
const companyLine = [exp.company, exp.location].filter(Boolean).join(' · ');
|
2026-10-22 07:29:06 +07:00
|
|
|
const responsibilities = exp.responsibilities;
|
2026-10-19 18:51:27 +07:00
|
|
|
return `
|
|
|
|
|
<div class="exp-entry avoid-break${variantClass}">
|
2026-10-11 00:53:10 +07:00
|
|
|
<div class="exp-header">
|
2026-10-22 07:29:06 +07:00
|
|
|
<span class="exp-title">${escapeHtml(exp.title)}</span>
|
2026-10-19 18:51:27 +07:00
|
|
|
<span class="exp-duration">${escapeHtml(exp.duration)}</span>
|
2026-10-11 00:53:10 +07:00
|
|
|
</div>
|
2026-10-19 18:51:27 +07:00
|
|
|
<div class="exp-company">${exp.logo ? `<img class="exp-logo" src="data:image/svg+xml;base64,${logos[exp.logo]}" alt="" />` : ''}${escapeHtml(companyLine)}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
<ul class="exp-responsibilities">
|
2026-10-19 18:51:27 +07:00
|
|
|
${responsibilities.map(r => `<li>${escapeHtml(r)}</li>`).join('')}
|
2026-10-11 00:53:10 +07:00
|
|
|
</ul>
|
|
|
|
|
</div>
|
2026-10-19 18:51:27 +07:00
|
|
|
`;
|
|
|
|
|
})
|
|
|
|
|
.join('');
|
2026-10-11 00:53:10 +07:00
|
|
|
}
|
|
|
|
|
|
2026-10-19 18:51:27 +07:00
|
|
|
function renderTldr(lang: Language): string {
|
|
|
|
|
return `
|
|
|
|
|
<div class="timeline-tldr avoid-break">
|
2026-10-22 07:29:06 +07:00
|
|
|
<div class="timeline-tldr-chip">${escapeHtml(UI[lang].experience.tldr)}</div>
|
2026-10-22 22:53:26 +07:00
|
|
|
<div class="timeline-tldr-text">${escapeHtml(CONTENT[lang].profile.tldr)}</div>
|
2026-10-19 18:51:27 +07:00
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 07:29:06 +07:00
|
|
|
function renderSkills(groups: ResolvedSkillGroup[]): string {
|
2026-10-11 00:53:10 +07:00
|
|
|
return groups.map(group => `
|
2026-10-19 18:51:27 +07:00
|
|
|
<div class="skill-group avoid-break">
|
2026-10-22 07:29:06 +07:00
|
|
|
<div class="skill-group-title">${escapeHtml(group.title)}</div>
|
2026-10-19 18:51:27 +07:00
|
|
|
<div class="skill-list">${group.skills.map(skill => escapeHtml(skill)).join(' · ')}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 07:29:06 +07:00
|
|
|
function renderProjects(projs: ResolvedProject[]): string {
|
2026-10-11 00:53:10 +07:00
|
|
|
return projs.map(project => `
|
|
|
|
|
<div class="project avoid-break">
|
2026-10-22 07:29:06 +07:00
|
|
|
<div class="project-title">${escapeHtml(project.title)}</div>
|
2026-10-19 18:51:27 +07:00
|
|
|
<div class="project-role">${escapeHtml(project.role)}</div>
|
2026-10-22 07:29:06 +07:00
|
|
|
<div class="project-summary">${escapeHtml(project.summary)}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
<div class="project-tech">
|
2026-10-19 18:51:27 +07:00
|
|
|
${project.technologies.map(t => `<span>${escapeHtml(t)}</span>`).join('')}
|
2026-10-11 00:53:10 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Assemble HTML ---
|
2026-10-19 18:51:27 +07:00
|
|
|
function buildHtml(lang: Language): string {
|
2026-10-22 07:29:06 +07:00
|
|
|
const c = CONTENT[lang];
|
|
|
|
|
const r = UI[lang].resume;
|
2026-10-19 18:51:27 +07:00
|
|
|
const fills: Array<[string, string]> = [
|
2026-10-22 07:29:06 +07:00
|
|
|
['{{NAME}}', escapeHtml(c.profile.name)],
|
|
|
|
|
['{{TITLE}}', escapeHtml(c.profile.title)],
|
|
|
|
|
['{{BIO}}', escapeHtml(c.profile.bio)],
|
|
|
|
|
['{{EMAIL}}', escapeHtml(c.profile.email)],
|
|
|
|
|
['{{GITHUB_URL}}', escapeHtml(c.profile.githubUrl)],
|
|
|
|
|
['{{LINKEDIN_URL}}', escapeHtml(c.profile.linkedinUrl)],
|
2026-10-19 18:51:27 +07:00
|
|
|
['{{PHOTO_BASE64}}', photoBase64],
|
|
|
|
|
['{{FONT_FACES}}', fontFaces],
|
|
|
|
|
['{{UPDATED_DATE}}', updatedDate],
|
2026-10-22 07:29:06 +07:00
|
|
|
['{{UPDATED_LABEL}}', r.UPDATED],
|
2026-10-19 18:51:27 +07:00
|
|
|
['{{SITE_URL}}', escapeHtml(siteUrl)],
|
|
|
|
|
['{{LANG}}', lang],
|
2026-10-22 07:29:06 +07:00
|
|
|
['{{EXPERIENCE_LABEL}}', r.EXPERIENCE],
|
|
|
|
|
['{{SKILLS_LABEL}}', r.SKILLS],
|
|
|
|
|
['{{PROJECTS_LABEL}}', r.PROJECTS],
|
|
|
|
|
['{{EXPERIENCE}}', renderExperience(c.experiences)],
|
2026-10-19 18:51:27 +07:00
|
|
|
['{{TLDR}}', renderTldr(lang)],
|
2026-10-22 07:29:06 +07:00
|
|
|
['{{SKILLS}}', renderSkills(c.skillGroups)],
|
|
|
|
|
['{{PROJECTS}}', renderProjects(c.projects)],
|
2026-10-19 18:51:27 +07:00
|
|
|
['{{QR_CODES}}', renderQr(lang)],
|
|
|
|
|
];
|
|
|
|
|
return fills.reduce((html, [key, value]) => html.split(key).join(value), template);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Build per language; fail fast on unresolved placeholders ---
|
|
|
|
|
const pages: Record<Language, string> = { en: '', vi: '' };
|
|
|
|
|
for (const lang of langs) {
|
|
|
|
|
const html = buildHtml(lang);
|
|
|
|
|
const leftover = html.match(/\{\{[A-Z_]+\}\}/g);
|
|
|
|
|
if (leftover) {
|
|
|
|
|
console.error(`❌ Unresolved placeholders in resume HTML [${lang}]: ${[...new Set(leftover)].join(', ')}`);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
pages[lang] = html;
|
|
|
|
|
}
|
2026-10-11 00:53:10 +07:00
|
|
|
|
2026-10-15 15:00:22 +07:00
|
|
|
// --- Generate output ---
|
|
|
|
|
const browser = await chromium.launch({ headless: !previewMode });
|
2026-10-11 00:53:10 +07:00
|
|
|
|
2026-10-15 15:00:22 +07:00
|
|
|
if (previewMode) {
|
2026-10-19 18:51:27 +07:00
|
|
|
const lang = langs[0];
|
2026-10-15 15:00:22 +07:00
|
|
|
const page = await browser.newPage();
|
2026-10-19 18:51:27 +07:00
|
|
|
await page.setContent(pages[lang]);
|
2026-10-15 15:00:22 +07:00
|
|
|
await page.bringToFront();
|
|
|
|
|
console.log(`Preview opened in browser [${lang}] — Ctrl+C to exit`);
|
|
|
|
|
await Promise.withResolvers<void>().promise;
|
|
|
|
|
} else {
|
|
|
|
|
const page = await browser.newPage();
|
2026-10-19 18:51:27 +07:00
|
|
|
for (const lang of langs) {
|
2026-10-27 12:34:22 +07:00
|
|
|
const outputPath = join(ROOT, `dist/resume-${lang}.pdf`);
|
2026-10-19 18:51:27 +07:00
|
|
|
mkdirSync(dirname(outputPath), { recursive: true });
|
|
|
|
|
await page.setContent(pages[lang]);
|
|
|
|
|
await page.pdf({
|
|
|
|
|
path: outputPath,
|
|
|
|
|
format: 'A4',
|
|
|
|
|
printBackground: true,
|
|
|
|
|
preferCSSPageSize: true,
|
|
|
|
|
tagged: true,
|
|
|
|
|
});
|
2026-10-27 12:34:22 +07:00
|
|
|
console.log(`resume-${lang}.pdf generated at ${outputPath} [${lang}]`);
|
2026-10-19 18:51:27 +07:00
|
|
|
}
|
2026-10-15 15:00:22 +07:00
|
|
|
await browser.close();
|
2026-10-19 18:51:27 +07:00
|
|
|
}
|