11// compile-latex-template.js
22
33import fs from 'fs' ;
4+ import YAML from 'yaml' ;
45import { Command } from 'commander' ;
5- import { PrismaClient } from '@prisma/client' ;
66import { default as nunjucks } from 'nunjucks' ;
77import { default as nunjucksDate } from 'nunjucks-date' ;
88
9- // Load the Prisma Client
10- const prisma = new PrismaClient ( ) ;
9+ const main = ( templatePath , outputPath , profilePath ) => {
10+ console . log ( `Compiling ${ templatePath } with ${ profilePath } ` ) ;
11+ // Read and parse profile from profilePath
12+ const profileString = fs . readFileSync ( profilePath , 'utf8' ) ;
13+ const profile = YAML . parse ( profileString ) ;
1114
12- // Function to load data from the database
13- const loadData = async ( profileVisibility ) => {
14- console . log ( 'Loading data from the database...' ) ;
15- // Read and parse profile from database
16- const profile = await prisma . profile . findFirstOrThrow ( { where : { visibility : profileVisibility } } ) ;
17- if ( ! profile ) {
18- throw new Error ( 'Profile not found' ) ;
19- }
20- const educations = await prisma . education . findMany ( ) ;
21- const skills = await prisma . skill . findMany ( ) ;
22- const experiences = await prisma . experience . findMany ( ) ;
23- const resumes = await prisma . resume . findFirst ( { include : { projects : { include : { project : true } } } } ) ;
24- const projects = resumes . projects . map ( ( resume ) => resume . project ) . sort ( ( a , b ) => parseInt ( b . year ) - parseInt ( a . year ) ) ;
25- return { profile, educations, skills, experiences, projects } ;
26- }
27-
28-
29- // Main function to compile the template
30- const main = async ( templatePath , outputPath , profileVisibility ) => {
31- console . log ( `Compiling ${ templatePath } ` ) ;
32- const data = await loadData ( profileVisibility ) ;
3315 // Configure Nunjucks with Custom Tags for LaTeX
3416 const env = nunjucks . configure ( {
3517 autoescape : false ,
@@ -48,7 +30,7 @@ const main = async (templatePath, outputPath, profileVisibility) => {
4830 nunjucksDate . install ( env ) ;
4931
5032 // Compile the Template based on profile.yaml
51- const result = env . render ( templatePath , data ) ;
33+ const result = env . render ( templatePath , profile ) ;
5234
5335 // Write the result to a file
5436 fs . writeFileSync ( outputPath , result ) ;
@@ -62,7 +44,7 @@ program
6244 . description ( 'Compile and export LATEX template using nunjucks' )
6345 . option ( '-t, --template <path>' , 'Path to template.tex' , './tex/template.tex' )
6446 . option ( '-o, --output <path>' , 'Path to output.tex' , './tex/WongZhaoWu-resume.tex' )
65- . option ( '-p, --profile <visibility >' , 'Profile visibility. Only `PUBLIC` or `PRIVATE` are accepted ' , 'PUBLIC ' )
47+ . option ( '-p, --profile <path >' , 'Path to profile.yaml ' , './src/lib/profile.yaml ' )
6648 . action ( ( options ) => {
6749 return main ( options . template , options . output , options . profile ) ;
6850 } ) ;
0 commit comments