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-11 00:53:10 +07:00
|
|
|
import { profile } from '../src/data/profile.js';
|
|
|
|
|
import { experiences } from '../src/data/experience.js';
|
|
|
|
|
import { skillGroups } from '../src/data/skills.js';
|
|
|
|
|
import { projects } from '../src/data/projects.js';
|
2026-10-19 18:51:27 +07:00
|
|
|
import { resumeQr } from '../src/data/resume.js';
|
|
|
|
|
import { siteUrl } from '../src/data/site.js';
|
|
|
|
|
import { strings } from '../src/i18n/strings.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 ---
|
|
|
|
|
const logoIds = [...new Set(experiences.map(exp => exp.logo).filter((id): id is string => id !== undefined))];
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// --- Language-specific label map ---
|
|
|
|
|
const labels: Record<Language, Record<string, string>> = {
|
|
|
|
|
en: {
|
|
|
|
|
EXPERIENCE: 'Experience',
|
|
|
|
|
SKILLS: 'Skills',
|
|
|
|
|
PROJECTS: 'Projects',
|
2026-10-19 18:51:27 +07:00
|
|
|
UPDATED: 'Last updated',
|
|
|
|
|
QR_ALT: 'QR code',
|
2026-10-12 14:50:17 +07:00
|
|
|
},
|
|
|
|
|
vi: {
|
|
|
|
|
EXPERIENCE: 'Kinh nghiệm',
|
|
|
|
|
SKILLS: 'Kỹ năng',
|
|
|
|
|
PROJECTS: 'Dự án',
|
2026-10-19 18:51:27 +07:00
|
|
|
UPDATED: 'Cập nhật lần cuối',
|
|
|
|
|
QR_ALT: 'mã QR',
|
2026-10-12 14:50:17 +07:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// --- Resolve bilingual field ---
|
2026-10-19 18:51:27 +07:00
|
|
|
function resolveField<T>(en: T | undefined, vi: T | undefined, lang: Language): T {
|
|
|
|
|
return lang === 'en' && en !== undefined ? en : (vi !== undefined ? vi : (en as T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- 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">
|
|
|
|
|
<img class="qr-img" src="${q.dataUri}" alt="${escapeHtml(`${q.label} ${labels[lang].QR_ALT}`)}" />
|
|
|
|
|
<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-19 18:51:27 +07:00
|
|
|
function renderExperience(exps: typeof experiences, lang: Language): string {
|
|
|
|
|
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(' · ');
|
|
|
|
|
const responsibilities = resolveField(exp.responsibilitiesEn, exp.responsibilities, lang);
|
|
|
|
|
return `
|
|
|
|
|
<div class="exp-entry avoid-break${variantClass}">
|
2026-10-11 00:53:10 +07:00
|
|
|
<div class="exp-header">
|
2026-10-19 18:51:27 +07:00
|
|
|
<span class="exp-title">${escapeHtml(resolveField(exp.titleEn, exp.title, lang))}</span>
|
|
|
|
|
<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">
|
|
|
|
|
<div class="timeline-tldr-chip">${escapeHtml(strings.experience.tldr[lang])}</div>
|
|
|
|
|
<div class="timeline-tldr-text">${escapeHtml(strings.experience.tldrText[lang])}</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderSkills(groups: typeof skillGroups, lang: Language): 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">
|
|
|
|
|
<div class="skill-group-title">${escapeHtml(resolveField(group.titleEn, group.title, lang))}</div>
|
|
|
|
|
<div class="skill-list">${group.skills.map(skill => escapeHtml(skill)).join(' · ')}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 18:51:27 +07:00
|
|
|
function renderProjects(projs: typeof projects, lang: Language): string {
|
2026-10-11 00:53:10 +07:00
|
|
|
return projs.map(project => `
|
|
|
|
|
<div class="project avoid-break">
|
2026-10-19 18:51:27 +07:00
|
|
|
<div class="project-title">${escapeHtml(resolveField(project.titleEn, project.title, lang))}</div>
|
|
|
|
|
<div class="project-role">${escapeHtml(project.role)}</div>
|
|
|
|
|
<div class="project-summary">${escapeHtml(resolveField(project.summaryEn, project.summary, lang))}</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 {
|
|
|
|
|
const fills: Array<[string, string]> = [
|
|
|
|
|
['{{NAME}}', escapeHtml(resolveField(profile.nameEn, profile.name, lang))],
|
|
|
|
|
['{{TITLE}}', escapeHtml(resolveField(profile.titleEn, profile.title, lang))],
|
|
|
|
|
['{{BIO}}', escapeHtml(resolveField(profile.bioEn, profile.bio, lang))],
|
|
|
|
|
['{{EMAIL}}', escapeHtml(profile.email)],
|
|
|
|
|
['{{GITHUB_URL}}', escapeHtml(profile.githubUrl)],
|
|
|
|
|
['{{LINKEDIN_URL}}', escapeHtml(profile.linkedinUrl)],
|
|
|
|
|
['{{PHOTO_BASE64}}', photoBase64],
|
|
|
|
|
['{{FONT_FACES}}', fontFaces],
|
|
|
|
|
['{{UPDATED_DATE}}', updatedDate],
|
|
|
|
|
['{{UPDATED_LABEL}}', labels[lang].UPDATED],
|
|
|
|
|
['{{SITE_URL}}', escapeHtml(siteUrl)],
|
|
|
|
|
['{{LANG}}', lang],
|
|
|
|
|
['{{EXPERIENCE_LABEL}}', labels[lang].EXPERIENCE],
|
|
|
|
|
['{{SKILLS_LABEL}}', labels[lang].SKILLS],
|
|
|
|
|
['{{PROJECTS_LABEL}}', labels[lang].PROJECTS],
|
|
|
|
|
['{{EXPERIENCE}}', renderExperience(experiences, lang)],
|
|
|
|
|
['{{TLDR}}', renderTldr(lang)],
|
|
|
|
|
['{{SKILLS}}', renderSkills(skillGroups, lang)],
|
|
|
|
|
['{{PROJECTS}}', renderProjects(projects, lang)],
|
|
|
|
|
['{{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) {
|
|
|
|
|
const langSuffix = lang === 'en' ? '' : `-${lang}`;
|
|
|
|
|
const outputPath = join(ROOT, `dist/resume${langSuffix}.pdf`);
|
|
|
|
|
mkdirSync(dirname(outputPath), { recursive: true });
|
|
|
|
|
await page.setContent(pages[lang]);
|
|
|
|
|
await page.pdf({
|
|
|
|
|
path: outputPath,
|
|
|
|
|
format: 'A4',
|
|
|
|
|
printBackground: true,
|
|
|
|
|
preferCSSPageSize: true,
|
|
|
|
|
tagged: true,
|
|
|
|
|
});
|
|
|
|
|
console.log(`resume${langSuffix}.pdf generated at ${outputPath} [${lang}]`);
|
|
|
|
|
}
|
2026-10-15 15:00:22 +07:00
|
|
|
await browser.close();
|
2026-10-19 18:51:27 +07:00
|
|
|
}
|