2026-10-11 00:53:10 +07:00
|
|
|
import { chromium } from 'playwright';
|
|
|
|
|
import { readFileSync, existsSync } from 'node:fs';
|
|
|
|
|
import { join, resolve } from 'node:path';
|
|
|
|
|
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-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);
|
|
|
|
|
const langArg = (args.find(a => a.startsWith('--lang=')) || '').replace('--lang=', '') as Language;
|
|
|
|
|
const lang: Language = (langArg === 'en' || langArg === 'vi') ? langArg : 'en';
|
2026-10-15 15:00:22 +07:00
|
|
|
const previewMode = args.includes('--preview');
|
2026-10-12 14:50:17 +07:00
|
|
|
|
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'],
|
|
|
|
|
['SEMIBOLD', 'SourceSans3-Semibold.woff2'],
|
|
|
|
|
['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-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-15 15:00:22 +07:00
|
|
|
ABOUT_ME: 'About me',
|
2026-10-12 14:50:17 +07:00
|
|
|
},
|
|
|
|
|
vi: {
|
|
|
|
|
EXPERIENCE: 'Kinh nghiệm',
|
|
|
|
|
SKILLS: 'Kỹ năng',
|
|
|
|
|
PROJECTS: 'Dự án',
|
2026-10-15 15:00:22 +07:00
|
|
|
ABOUT_ME: 'Về tôi',
|
2026-10-12 14:50:17 +07:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// --- Resolve bilingual field ---
|
|
|
|
|
function resolveField<T>(en?: T, vi?: T): T {
|
|
|
|
|
return lang === 'en' && en !== undefined ? en : (vi !== undefined ? vi : en as T);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 00:53:10 +07:00
|
|
|
// --- Render HTML helpers ---
|
|
|
|
|
function renderExperience(exps: typeof experiences): string {
|
|
|
|
|
return exps.map(exp => `
|
|
|
|
|
<div class="exp-entry avoid-break">
|
|
|
|
|
<div class="exp-header">
|
2026-10-12 14:50:17 +07:00
|
|
|
<span class="exp-title">${resolveField(exp.titleEn, exp.title)}</span>
|
2026-10-11 00:53:10 +07:00
|
|
|
<span class="exp-duration">${exp.duration}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="exp-company">${exp.company}</div>
|
|
|
|
|
${exp.location ? `<div class="exp-location">${exp.location}</div>` : ''}
|
|
|
|
|
<ul class="exp-responsibilities">
|
2026-10-12 14:50:17 +07:00
|
|
|
${resolveField(exp.responsibilitiesEn, exp.responsibilities).map(r => `<li>${r}</li>`).join('')}
|
2026-10-11 00:53:10 +07:00
|
|
|
</ul>
|
|
|
|
|
${exp.achievements?.map(ach => `
|
|
|
|
|
<div class="achievement avoid-break">
|
2026-10-12 14:50:17 +07:00
|
|
|
<div class="achievement-title">${resolveField(ach.titleEn, ach.title)}</div>
|
|
|
|
|
<div class="achievement-context">${resolveField(ach.contextEn, ach.context)}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
${ach.technicalWork?.length ? `
|
|
|
|
|
<ul class="achievement-technical">
|
|
|
|
|
${ach.technicalWork.map(t => `<li>${t}</li>`).join('')}
|
|
|
|
|
</ul>
|
|
|
|
|
` : ''}
|
|
|
|
|
<div class="achievement-tech">
|
|
|
|
|
${ach.technologies.map(t => `<span>${t}</span>`).join('')}
|
|
|
|
|
</div>
|
2026-10-12 14:50:17 +07:00
|
|
|
${ach.impact ? `<div class="achievement-impact">${resolveField(ach.impactEn, ach.impact)}</div>` : ''}
|
2026-10-11 00:53:10 +07:00
|
|
|
${ach.metrics?.length ? `<div class="achievement-metrics">${ach.metrics.join(', ')}</div>` : ''}
|
|
|
|
|
</div>
|
|
|
|
|
`).join('')}
|
|
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderSkills(groups: typeof skillGroups): string {
|
|
|
|
|
return groups.map(group => `
|
|
|
|
|
<div class="avoid-break">
|
2026-10-12 14:50:17 +07:00
|
|
|
<div class="skill-group-title">${resolveField(group.titleEn, group.title)}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
<div class="skill-list">${group.skills.join(' · ')}</div>
|
|
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderProjects(projs: typeof projects): string {
|
|
|
|
|
return projs.map(project => `
|
|
|
|
|
<div class="project avoid-break">
|
2026-10-12 14:50:17 +07:00
|
|
|
<div class="project-title">${resolveField(project.titleEn, project.title)}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
<div class="project-role">${project.role}</div>
|
2026-10-12 14:50:17 +07:00
|
|
|
<div class="project-summary">${resolveField(project.summaryEn, project.summary)}</div>
|
2026-10-11 00:53:10 +07:00
|
|
|
<div class="project-tech">
|
|
|
|
|
${project.technologies.map(t => `<span>${t}</span>`).join('')}
|
|
|
|
|
</div>
|
|
|
|
|
${project.metrics?.length ? `<div class="project-metrics">${project.metrics.join(', ')}</div>` : ''}
|
|
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Assemble HTML ---
|
|
|
|
|
const html = template
|
2026-10-14 23:34:50 +07:00
|
|
|
.replace('{{NAME}}', resolveField(profile.nameEn, profile.name))
|
2026-10-12 14:50:17 +07:00
|
|
|
.replace('{{TITLE}}', resolveField(profile.titleEn, profile.title))
|
|
|
|
|
.replace('{{BIO}}', resolveField(profile.bioEn, profile.bio))
|
2026-10-11 00:53:10 +07:00
|
|
|
.replace('{{EMAIL}}', profile.email)
|
2026-10-14 23:34:50 +07:00
|
|
|
.replace('{{GITHUB}}', profile.githubUrl)
|
|
|
|
|
.replace('{{LINKEDIN}}', profile.linkedinUrl)
|
2026-10-11 00:53:10 +07:00
|
|
|
.replace('{{PHOTO_BASE64}}', photoBase64)
|
|
|
|
|
.replace('{{FONT_FACES}}', fontFaces)
|
2026-10-12 14:50:17 +07:00
|
|
|
.replace('{{UPDATED_DATE}}', updatedDate)
|
|
|
|
|
.replace('{{LANG}}', lang)
|
2026-10-15 15:00:22 +07:00
|
|
|
.replace('{{ABOUT_ME_LABEL}}', labels[lang].ABOUT_ME)
|
2026-10-12 14:50:17 +07:00
|
|
|
.replace('{{EXPERIENCE_LABEL}}', labels[lang].EXPERIENCE)
|
|
|
|
|
.replace('{{SKILLS_LABEL}}', labels[lang].SKILLS)
|
|
|
|
|
.replace('{{PROJECTS_LABEL}}', labels[lang].PROJECTS)
|
2026-10-11 00:53:10 +07:00
|
|
|
.replace('{{EXPERIENCE}}', renderExperience(experiences))
|
|
|
|
|
.replace('{{SKILLS}}', renderSkills(skillGroups))
|
|
|
|
|
.replace('{{PROJECTS}}', renderProjects(projects));
|
|
|
|
|
|
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) {
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
await page.setContent(html);
|
|
|
|
|
await page.bringToFront();
|
|
|
|
|
console.log(`Preview opened in browser [${lang}] — Ctrl+C to exit`);
|
|
|
|
|
await Promise.withResolvers<void>().promise;
|
|
|
|
|
} else {
|
|
|
|
|
const langSuffix = lang === 'en' ? '' : `-${lang}`;
|
|
|
|
|
const outputPath = join(ROOT, `dist/resume${langSuffix}.pdf`);
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
await page.setContent(html);
|
|
|
|
|
await page.pdf({
|
|
|
|
|
path: outputPath,
|
|
|
|
|
format: 'A4',
|
|
|
|
|
printBackground: true,
|
|
|
|
|
title: `Resume - ${profile.name} (${lang === 'en' ? 'English' : 'Vietnamese'})`,
|
|
|
|
|
author: profile.name,
|
|
|
|
|
});
|
2026-10-11 00:53:10 +07:00
|
|
|
|
2026-10-15 15:00:22 +07:00
|
|
|
await browser.close();
|
|
|
|
|
console.log(`resume${langSuffix}.pdf generated at ${outputPath} [${lang}]`);
|
|
|
|
|
}
|