-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·191 lines (167 loc) · 5.67 KB
/
build.js
File metadata and controls
executable file
·191 lines (167 loc) · 5.67 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
#!/usr/bin/env node
/**
* Unified build script for current branch
* Builds HTML and PDFs for the current Git branch
*
* Usage:
* node build.js # Build everything (HTML + PDF)
* node build.js --web-only # Build HTML only
* node build.js --pdf-only # Build PDF only
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const CONFIG = require('./build.config.json');
/**
* Get current Git branch name
*/
function getCurrentBranch() {
const envBranch = process.env.BRANCH_NAME || process.env.GITHUB_REF_NAME;
if (envBranch) {
return envBranch;
}
try {
return execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' }).trim();
} catch (error) {
console.warn('⚠️ Unable to detect git branch, defaulting to "main"');
return 'main';
}
}
/**
* Main build function
*/
async function build() {
const args = process.argv.slice(2);
const webOnly = args.includes('--web-only');
const pdfOnly = args.includes('--pdf-only');
const forceFlatPdfOutput = process.env.FORCE_FLAT_PDF_OUTPUT === 'true';
// Get current branch
const currentBranch = getCurrentBranch();
console.log(`📦 Building branch: ${currentBranch}\n`);
let branchConfig;
if (pdfOnly && forceFlatPdfOutput) {
branchConfig = { outputPath: '', enabled: true };
console.log('ℹ️ FORCE_FLAT_PDF_OUTPUT enabled: using flat PDF output path (dist/pdf)');
} else {
// Check if branch is configured
branchConfig = CONFIG.branches[currentBranch];
if (!branchConfig) {
if (pdfOnly) {
const fallbackBranch = CONFIG.defaultBranch || 'main';
branchConfig = CONFIG.branches[fallbackBranch] || { outputPath: '', enabled: true };
console.warn(`⚠️ Branch "${currentBranch}" is not configured in build.config.json`);
console.warn(`⚠️ Using fallback settings from "${fallbackBranch}" for PDF-only build`);
} else {
console.error(`❌ Branch "${currentBranch}" is not configured in build.config.json`);
console.log('Available branches:', Object.keys(CONFIG.branches).join(', '));
process.exit(1);
}
}
if (!branchConfig.enabled) {
console.log(`⚠️ Branch "${currentBranch}" is disabled in config`);
console.log('To enable it, set "enabled": true in build.config.json');
process.exit(0);
}
}
// Calculate output paths
const outputPath = branchConfig.outputPath;
const webOutputDir = outputPath
? path.join(CONFIG.build.outputDirs.web, outputPath)
: CONFIG.build.outputDirs.web;
const pdfOutputDir = outputPath
? path.join(CONFIG.build.outputDirs.pdf, outputPath)
: CONFIG.build.outputDirs.pdf;
// Calculate BASE_PATH for links
const basePath = outputPath ? `/cv/${outputPath}` : '/cv';
console.log(`📁 Output directories:`);
console.log(` Web: ${webOutputDir}`);
console.log(` PDF: ${pdfOutputDir}`);
console.log(` Base path: ${basePath}\n`);
// Ensure output directories exist
fs.mkdirSync(webOutputDir, { recursive: true });
fs.mkdirSync(pdfOutputDir, { recursive: true });
try {
// Build web (HTML + CSS)
if (!pdfOnly) {
console.log('🎨 Building web assets...\n');
// 1. Minify CSS and JS
execSync('node build-minify.js', {
stdio: 'inherit',
env: {
...process.env,
OUTPUT_DIR: webOutputDir
}
});
// 2. Generate static HTML files
execSync('node build-static-locales.js', {
stdio: 'inherit',
env: {
...process.env,
OUTPUT_DIR: webOutputDir,
BASE_PATH: basePath,
BRANCH_NAME: currentBranch
}
});
}
// In PDF-only mode, we still need static HTML files as PDF input
if (pdfOnly) {
console.log('🧩 Preparing static HTML for PDF generation...\n');
execSync('node build-static-locales.js', {
stdio: 'inherit',
env: {
...process.env,
OUTPUT_DIR: webOutputDir,
BASE_PATH: basePath,
BRANCH_NAME: currentBranch
}
});
}
// Build PDFs
if (!webOnly) {
console.log('\n📄 Building PDFs...\n');
// Use Typst pipeline if available, fall back to Puppeteer otherwise
const typstBuildScript = path.join(__dirname, 'build-pdf-typst.js');
if (fs.existsSync(typstBuildScript)) {
execSync(`node ${typstBuildScript}`, {
stdio: 'inherit',
env: {
...process.env,
OUTPUT_DIR: pdfOutputDir,
}
});
} else {
// Legacy fallback
execSync('node generate-pdf.js --all --vector', {
stdio: 'inherit',
env: {
...process.env,
HTML_DIR: webOutputDir,
OUTPUT_DIR: pdfOutputDir,
BASE_PATH: basePath,
BRANCH_NAME: currentBranch
}
});
}
// Copy PDFs to web directory for GitHub Pages serving
const webPdfDir = path.join(webOutputDir, 'pdf');
console.log(`\n📋 Copying PDFs to ${webPdfDir} for GitHub Pages...`);
fs.mkdirSync(webPdfDir, { recursive: true });
execSync(`cp -r ${pdfOutputDir}/* ${webPdfDir}/`, { stdio: 'inherit' });
}
console.log('\n✅ Build completed successfully!');
console.log(`\n📂 Files generated in:`);
if (!pdfOnly) console.log(` ${webOutputDir}`);
if (!webOnly) console.log(` ${pdfOutputDir}`);
} catch (error) {
console.error('\n❌ Build failed:', error.message);
process.exit(1);
}
}
// Run if called directly
if (require.main === module) {
build().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});
}
module.exports = { build };