-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
424 lines (357 loc) · 11.9 KB
/
Copy pathbuild.js
File metadata and controls
424 lines (357 loc) · 11.9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
const fs = require('fs-extra');
const path = require('path');
const Handlebars = require('handlebars');
// Configuration
const config = {
baseUrl: 'https://www.Logo.iT',
supportedLanguages: {
'en': { name: 'English', dir: 'ltr' },
'zh-hans': { name: '简体中文', dir: 'ltr' },
'zh-hant': { name: '繁體中文', dir: 'ltr' },
'es': { name: 'Español', dir: 'ltr' },
'fr': { name: 'Français', dir: 'ltr' },
'de': { name: 'Deutsch', dir: 'ltr' },
'ja': { name: '日本語', dir: 'ltr' },
'ko': { name: '한국어', dir: 'ltr' },
'pt': { name: 'Português', dir: 'ltr' },
'ru': { name: 'Русский', dir: 'ltr' },
'ar': { name: 'العربية', dir: 'rtl' },
'it': { name: 'Italiano', dir: 'ltr' },
'nl': { name: 'Nederlands', dir: 'ltr' },
'pl': { name: 'Polski', dir: 'ltr' },
'tr': { name: 'Türkçe', dir: 'ltr' },
'hi': { name: 'हिंदी', dir: 'ltr' }
},
outputDir: 'dist',
templateFile: 'index.hbs',
translationsDir: 'translations'
};
// Helper functions
function loadTranslations() {
const translations = {};
for (const lang of Object.keys(config.supportedLanguages)) {
// Map language codes to file names
const fileMapping = {
'zh-hans': 'zh-hans',
'zh-hant': 'zh-hant'
};
const fileName = fileMapping[lang] || lang;
const filePath = path.join(config.translationsDir, `${fileName}.json`);
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf8');
if (content.trim()) {
translations[lang] = JSON.parse(content);
} else {
console.warn(`Translation file is empty: ${filePath}`);
}
} else {
console.warn(`Translation file not found: ${filePath}`);
}
}
return translations;
}
function getLanguageUrl(lang) {
if (lang === 'en') {
return config.baseUrl + '/';
}
// Use proper language codes for URLs
const urlMapping = {
'zh-hans': 'zh-CN',
'zh-hant': 'zh-TW'
};
const urlLang = urlMapping[lang] || lang;
return config.baseUrl + '/' + urlLang + '/';
}
function getFilePath(lang) {
if (lang === 'en') {
return 'index.html';
}
// Use proper language codes for file paths
const pathMapping = {
'zh-hans': 'zh-CN',
'zh-hant': 'zh-TW'
};
const pathLang = pathMapping[lang] || lang;
return path.join(pathLang, 'index.html');
}
function generateAlternateUrls() {
const alternateUrls = {};
for (const lang of Object.keys(config.supportedLanguages)) {
alternateUrls[lang] = getLanguageUrl(lang);
}
return alternateUrls;
}
function generateLanguageOptions(currentLang) {
return Object.entries(config.supportedLanguages).map(([code, info]) => {
const urlMapping = {
'zh-hans': 'zh-CN',
'zh-hant': 'zh-TW'
};
const urlCode = urlMapping[code] || code;
return {
code,
name: info.name,
url: code === 'en' ? '/' : `/${urlCode}/`,
current: code === currentLang
};
});
}
function ensureDirectoryExists(filePath) {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirpSync(dir);
}
}
async function copyStaticAssets() {
console.log('📁 Copying static assets...');
const staticAssets = [
'app.js',
'apple-touch-icon.png',
'favicon-16x16.png',
'favicon-32x32.png',
'favicon-2048x2048.png',
'favicon.ico',
'assets',
'LICENSE',
'README.MD'
];
for (const asset of staticAssets) {
const srcPath = path.join('.', asset);
const destPath = path.join(config.outputDir, asset);
if (fs.existsSync(srcPath)) {
if (fs.statSync(srcPath).isDirectory()) {
fs.copySync(srcPath, destPath, { overwrite: true });
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
}
function generateSitemap() {
console.log('🗺️ Generating sitemap.xml...');
const urls = [];
for (const lang of Object.keys(config.supportedLanguages)) {
const url = getLanguageUrl(lang);
const alternates = Object.entries(config.supportedLanguages).map(([altLang]) => {
// Use proper hreflang codes
const hreflangMapping = {
'zh-hans': 'zh-CN',
'zh-hant': 'zh-TW'
};
const hreflangCode = hreflangMapping[altLang] || altLang;
return {
lang: hreflangCode,
url: getLanguageUrl(altLang)
};
});
urls.push({
loc: url,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'weekly',
priority: lang === 'en' ? '1.0' : '0.8',
alternates: alternates
});
}
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
${urls.map(url => ` <url>
<loc>${url.loc}</loc>
<lastmod>${url.lastmod}</lastmod>
<changefreq>${url.changefreq}</changefreq>
<priority>${url.priority}</priority>
${url.alternates.map(alt => ` <xhtml:link rel="alternate" hreflang="${alt.lang}" href="${alt.url}"/>`).join('\n')}
</url>`).join('\n')}
</urlset>`;
fs.writeFileSync(path.join(config.outputDir, 'sitemap.xml'), sitemap);
}
function generateRobotsTxt() {
console.log('🤖 Generating robots.txt...');
const robots = `User-agent: *
Allow: /
# Sitemaps
Sitemap: ${config.baseUrl}/sitemap.xml
# Block common bot patterns
User-agent: AhrefsBot
Disallow: /
User-agent: MJ12bot
Disallow: /
User-agent: DotBot
Disallow: /`;
fs.writeFileSync(path.join(config.outputDir, 'robots.txt'), robots);
}
async function buildSite() {
console.log('🚀 Starting multi-language website build...\n');
// Clean and create output directory
if (fs.existsSync(config.outputDir)) {
fs.removeSync(config.outputDir);
}
fs.mkdirpSync(config.outputDir);
// Load template and translations
console.log('📖 Loading template and translations...');
const templateContent = fs.readFileSync(config.templateFile, 'utf8');
const template = Handlebars.compile(templateContent);
const translations = loadTranslations();
console.log(`✅ Loaded ${Object.keys(translations).length} translation files\n`);
// Generate pages for each language
console.log('🌍 Generating language-specific pages...');
for (const [lang, langInfo] of Object.entries(config.supportedLanguages)) {
if (!translations[lang]) {
console.warn(`⚠️ No translation found for ${lang}, skipping...`);
continue;
}
console.log(` 📄 Building ${lang} (${langInfo.name})...`);
const context = {
lang: lang,
isRtl: langInfo.dir === 'rtl',
languageName: langInfo.name,
t: translations[lang],
canonicalUrl: getLanguageUrl(lang),
alternateUrls: generateAlternateUrls(),
languageOptions: generateLanguageOptions(lang)
};
const html = template(context);
const outputPath = path.join(config.outputDir, getFilePath(lang));
ensureDirectoryExists(outputPath);
fs.writeFileSync(outputPath, html);
}
// Copy static assets
await copyStaticAssets();
// Generate SEO files
generateSitemap();
generateRobotsTxt();
// Create redirects for common language patterns
console.log('🔗 Generating redirect rules...');
const htaccess = `# Language redirects based on Accept-Language header
RewriteEngine On
# Redirect based on browser language (only if no specific path is requested)
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^zh-CN [NC]
RewriteRule ^$ /zh-CN/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^zh-TW [NC]
RewriteRule ^$ /zh-TW/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^zh [NC]
RewriteRule ^$ /zh-CN/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^ja [NC]
RewriteRule ^$ /ja/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ /ko/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ /pt/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^ru [NC]
RewriteRule ^$ /ru/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^ar [NC]
RewriteRule ^$ /ar/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^$ /it/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ /nl/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^pl [NC]
RewriteRule ^$ /pl/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^tr [NC]
RewriteRule ^$ /tr/ [R=302,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^hi [NC]
RewriteRule ^$ /hi/ [R=302,L]
# Cache static assets
<FilesMatch "\\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public, immutable"
</FilesMatch>
# Cache HTML files for a shorter period
<FilesMatch "\\.html$">
ExpiresActive On
ExpiresDefault "access plus 1 week"
Header append Cache-Control "public"
</FilesMatch>`;
fs.writeFileSync(path.join(config.outputDir, '.htaccess'), htaccess);
// Generate JSON-LD structured data
console.log('📊 Generating structured data...');
const structuredData = {
"@context": "https://schema.org",
"@type": "WebApplication",
"name": "LogoiT",
"description": "Free text-to-logo and favicon generator",
"url": config.baseUrl,
"applicationCategory": "DesignApplication",
"operatingSystem": "Web Browser",
"offers": {
"@type": "Offer",
"price": "0",
"priceCurrency": "USD"
},
"creator": {
"@type": "Organization",
"name": "LogoiT",
"url": config.baseUrl
},
"featureList": [
"Text to Logo Generation",
"Text to Favicon Generation",
"Multiple Format Support (PNG, SVG, ICO)",
"Customizable Colors and Fonts",
"Instant Preview",
"Free Download"
],
"inLanguage": Object.keys(config.supportedLanguages)
};
fs.writeFileSync(
path.join(config.outputDir, 'structured-data.json'),
JSON.stringify(structuredData, null, 2)
);
// Build summary
console.log('\n✨ Build completed successfully!');
console.log('📊 Build Summary:');
console.log(` • Generated ${Object.keys(config.supportedLanguages).length} language versions`);
console.log(` • Languages: ${Object.values(config.supportedLanguages).map(l => l.name).join(', ')}`);
console.log(` • Output directory: ${config.outputDir}/`);
console.log(' • SEO files: sitemap.xml, robots.txt, .htaccess');
console.log(' • Structured data: structured-data.json');
console.log('\n🌍 Language URLs:');
for (const [lang, langInfo] of Object.entries(config.supportedLanguages)) {
const url = getLanguageUrl(lang);
const localPath = getFilePath(lang);
console.log(` • ${langInfo.name.padEnd(15)} ${url.padEnd(35)} → ${localPath}`);
}
console.log('\n🚀 To test locally, run:');
console.log(' pnpm start');
console.log(' # or');
console.log(' python3 -m http.server 8000 --directory dist');
console.log(` # then visit http://localhost:8000\n`);
}
// Register Handlebars helpers
Handlebars.registerHelper('eq', function(a, b, options) {
return a === b ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});
// Run the build
if (require.main === module) {
buildSite().catch(error => {
console.error('❌ Build failed:', error);
process.exit(1);
});
}
module.exports = { buildSite, config };