-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-locales.js
More file actions
751 lines (664 loc) · 22.7 KB
/
generate-locales.js
File metadata and controls
751 lines (664 loc) · 22.7 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#!/usr/bin/env node
/**
* Generate i18n Locale Files from CSV
*
* Transforms Google Sheet CSV exports into namespaced JSON files for react-i18next.
* Supports multi-state deployments with separate CSV files per state.
*
* Usage:
* node content/scripts/generate-locales.js # Generate all locales
* node content/scripts/generate-locales.js --watch # Watch mode (future)
*
* CSV Files:
* content/states/dc.csv # DC-specific content (downloaded from DC tab)
* content/states/co.csv # CO-specific content (downloaded from CO tab)
* content/states/ny.csv # NY-specific content (future)
*
* CSV Format:
* 🟡 Content,English,Español,
* "S1 - Landing Page - Title","Get a one-time...","Obtenga un pago...",
*
* Content Key Format (supports two formats):
* 3-part: "{Section} - {Page} - {Key}"
* Example: "S1 - Landing Page - Title" -> landing.title
* 2-part: "{Section} - {Key}"
* Example: "GLOBAL - Address Format" -> common.addressFormat
*
* - Section: GLOBAL (shared), S1-S8 (screens), PROTO (prototypes)
* - Page: Landing Page, Disclaimer, Personal Information, etc.
* - Key: Descriptive key name (spaces converted to camelCase)
*
* Output Structure:
* content/locales/en/dc/common.json
* content/locales/en/dc/landing.json
* content/locales/es/dc/common.json
* ...
*
* Features:
* - Multi-state CSVs: Each state has its own CSV file (content/states/{state}.csv)
* - Smart caching: Only regenerates if any CSV changed (SHA-256 hash)
* - Namespace splitting: Organizes by page/component for lazy loading
* - Variable interpolation: Preserves {state}, {year} placeholders for runtime
*/
// load-env.js is portal-specific and not needed in the shared package.
// The script does not use any process.env variables.
import { createHash } from 'crypto';
import {
existsSync,
mkdirSync,
readFileSync,
readdirSync,
rmSync,
writeFileSync,
} from 'fs';
import * as path from 'path';
import { dirname, join, relative } from 'path';
import { fileURLToPath } from 'url';
// Parse CLI arguments
const cliArgs = process.argv.slice(2)
function getCliArg(name) {
const idx = cliArgs.indexOf(name)
return idx !== -1 ? cliArgs[idx + 1] : null
}
const csvDirOverride = getCliArg('--csv-dir')
const outDirOverride = getCliArg('--out-dir')
const tsOutOverride = getCliArg('--ts-out')
const appFilter = getCliArg('--app') // 'portal' | 'enrollment' | null (all)
const sectionsFilter = getCliArg('--sections') // comma-separated, e.g., 'S1,GLOBAL'
const allowedSections = sectionsFilter ? sectionsFilter.split(',').map(s => s.trim()) : null
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const contentDir = join(__dirname, '..');
const rootDir = join(contentDir, '..');
const rel = p => relative(rootDir, p);
// Configuration
const CONFIG = {
// State CSV files directory (content/states/{state}.csv)
statesDir: csvDirOverride
? path.resolve(process.cwd(), csvDirOverride)
: join(contentDir, 'states'),
outputDir: outDirOverride
? path.resolve(process.cwd(), outDirOverride)
: join(contentDir, 'locales'),
hashFile: join(contentDir, '.copy-hash'),
locales: {
en: 'English',
es: 'Español',
},
// Map CSV sections to namespaces (fallback when page not mapped)
sectionToNamespace: {
All: 'common', // Shared across all pages
GLOBAL: 'common', // Global shared content
S1: 'landing', // Landing page (Screen 1)
S2: 'disclaimer', // Disclaimer (Screen 2)
S3: 'personalInfo', // Personal Information (Screen 3)
S4: 'confirmInfo', // Confirm Personal Information (Screen 4)
S5: 'optionalId', // Optional ID Information (Screen 5)
S6: 'result', // Result page (Screen 6)
S7: 'dashboard', // Portal Dashboard (Screen 7)
S8: 'edit', // Edit screens (Screen 8)
PROTO: 'proto', // Prototype/demo content
},
// Map page names to namespaces (for "S1 - Landing Page - Title" format)
pageToNamespace: {
// S1 - Landing
'landing page': 'landing',
// S2 - Disclaimer
'disclaimer': 'disclaimer',
// S3 - Personal Information
'personal information': 'personalInfo',
// S4 - Confirm Personal Information
'confirm personal information': 'confirmInfo',
// S5 - Optional ID Information
'optional id information': 'optionalId',
// S6 - Result
'result': 'result',
// S7 - Portal Dashboard
'portal dashboard': 'dashboard',
// S8 - Login/OTP flow
'otp enter email': 'login',
'otp confirm': 'login',
'otp email message': 'login',
// S2 - Log In Disclaimer (maps to login namespace)
'log in disclaimer': 'login',
// S8 - OIDC/MyColorado callback
'callback': 'login',
// S8 - Identity proofing
'id proofing optional id info': 'idProofing',
// S8 - Opt-in preferences
'opt-in': 'optIn',
// S8 - Off-boarding screens
'off-boarding': 'offBoarding',
'co-loaded off-boarding': 'offBoarding',
// S8 - Edit screens
'contact preferences update': 'editContactPreferences',
'mailing address edit': 'editMailingAddress',
// S10 - Step-up verification screens
'step-up disclaimer': 'stepUpDisclaimer',
'step-up failure': 'stepUpFailure',
// Global/Common
'header': 'common',
'footer': 'common',
'help': 'common',
'global': 'common',
// PROTO - Prototype screens
'prototypes': 'proto',
},
// Key prefixes for pages that share a namespace but need distinct keys
// This prevents key collisions (e.g., both OTP Enter Email and OTP Confirm have "title")
pageKeyPrefix: {
'otp confirm': 'verify',
'otp email message': 'email',
'co-loaded off-boarding': 'coLoaded',
'callback': 'callback',
},
};
/**
* Parse CSV content into rows
* Handles quoted fields with commas and newlines
*/
function parseCSV(content) {
const rows = [];
let currentRow = [];
let currentField = '';
let inQuotes = false;
for (let i = 0; i < content.length; i++) {
const char = content[i];
const nextChar = content[i + 1];
if (inQuotes) {
if (char === '"' && nextChar === '"') {
// Escaped quote
currentField += '"';
i++;
} else if (char === '"') {
// End of quoted field
inQuotes = false;
} else {
currentField += char;
}
} else {
if (char === '"') {
// Start of quoted field
inQuotes = true;
} else if (char === ',') {
// Field separator
currentRow.push(currentField.trim());
currentField = '';
} else if (char === '\n' || (char === '\r' && nextChar === '\n')) {
// Row separator
currentRow.push(currentField.trim());
if (currentRow.some((f) => f)) {
// Skip empty rows
rows.push(currentRow);
}
currentRow = [];
currentField = '';
if (char === '\r') i++; // Skip \n in \r\n
} else if (char !== '\r') {
currentField += char;
}
}
}
// Handle last field/row
if (currentField || currentRow.length > 0) {
currentRow.push(currentField.trim());
if (currentRow.some((f) => f)) {
rows.push(currentRow);
}
}
return rows;
}
/**
* Convert space-separated key to camelCase
* "Logo Alt" -> "logoAlt"
*/
function toCamelCase(str) {
return str
.split(/\s+/)
.map((word, index) =>
index === 0
? word.toLowerCase()
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join('');
}
/**
* Parse content key into structured parts
* Supports both formats:
* 3-part: "S1 - Landing Page - Title" -> { namespace: "landing", key: "title" }
* 2-part: "GLOBAL - Address Format" -> { namespace: "common", key: "addressFormat" }
*/
function parseContentKey(contentKey) {
const parts = contentKey.split(' - ').map((p) => p.trim());
if (parts.length < 2) {
console.warn(`⚠️ Invalid content key format: "${contentKey}"`);
return null;
}
// Handle 2-part format: "SECTION - Key Name"
if (parts.length === 2) {
const [section, keyName] = parts;
const key = toCamelCase(keyName);
// Determine namespace from section
const namespace = CONFIG.sectionToNamespace[section] || toCamelCase(section);
return {
section,
page: section.toLowerCase(),
namespace,
key,
};
}
// Handle 3+ part format: "SECTION - Page - Key Name"
const [section, page, ...keyParts] = parts;
let key = toCamelCase(keyParts.join(' '));
const pageLower = page.toLowerCase();
// Determine namespace from page name or section
let namespace;
if (CONFIG.pageToNamespace[pageLower]) {
namespace = CONFIG.pageToNamespace[pageLower];
} else if (CONFIG.sectionToNamespace[section]) {
namespace = CONFIG.sectionToNamespace[section];
} else {
// Fallback: use sanitized page name
namespace = toCamelCase(page);
}
// Apply key prefix for pages that share a namespace but need distinct keys
const keyPrefix = CONFIG.pageKeyPrefix[pageLower];
if (keyPrefix) {
// Capitalize first letter of key and prepend prefix
key = keyPrefix + key.charAt(0).toUpperCase() + key.slice(1);
}
return {
section,
page: pageLower,
namespace,
key,
};
}
/**
* Build locale data structure from CSV rows for a single state
* Returns: { en: { common: { key: value }, landing: { key: value } }, es: { ... } }
*/
function buildStateLocaleData(rows, state) {
const [headerRow, ...dataRows] = rows;
// Find column indices (handle emoji prefixes like "🟡 Content" and renamed headers like "Variable Name/Key")
const contentIdx = headerRow.findIndex((h) => {
const lower = h.toLowerCase()
return lower.includes('content') || lower.includes('variable name')
});
const englishIdx = headerRow.findIndex((h) =>
h.toLowerCase().includes('english')
);
const spanishIdx = headerRow.findIndex((h) =>
h.toLowerCase().includes('español')
);
if (contentIdx === -1 || englishIdx === -1) {
throw new Error(`CSV for ${state} must have "Content" and "English" columns`);
}
// Initialize data structure for this state
const data = {};
for (const locale of Object.keys(CONFIG.locales)) {
data[locale] = {};
}
// Process each row
for (const row of dataRows) {
const contentKey = row[contentIdx];
const englishValue = row[englishIdx] || '';
const spanishValue = spanishIdx !== -1 ? row[spanishIdx] || '' : '';
// Skip empty rows or rows without content keys
if (!contentKey || !contentKey.trim()) continue;
const parsed = parseContentKey(contentKey);
if (!parsed) continue;
// Section-level filter: skip rows from sections not in the allowed list
if (allowedSections && !allowedSections.includes(parsed.section)) continue;
const { namespace, key } = parsed;
// English — don't overwrite a non-empty value with an empty one
// (handles key collisions where multiple CSV rows map to the same namespace+key)
if (!data.en[namespace]) {
data.en[namespace] = {};
}
if (englishValue || !data.en[namespace][key]) {
data.en[namespace][key] = englishValue;
}
// Spanish — same collision protection
if (spanishIdx !== -1) {
if (!data.es[namespace]) {
data.es[namespace] = {};
}
if (spanishValue || !data.es[namespace][key]) {
data.es[namespace][key] = spanishValue;
}
}
}
return data;
}
/**
* Discover state CSV files in the states directory
* Returns array of { state: string, csvPath: string }
*/
function discoverStateCsvFiles() {
if (!existsSync(CONFIG.statesDir)) {
return [];
}
const files = readdirSync(CONFIG.statesDir);
return files
.filter((f) => f.endsWith('.csv'))
.map((f) => ({
state: f.replace('.csv', '').toLowerCase(),
csvPath: join(CONFIG.statesDir, f),
}));
}
/**
* Calculate combined SHA-256 hash of all state CSV files and this script
*/
function calculateCombinedHash(stateFiles) {
const hash = createHash('sha256');
// Include the script itself so logic changes invalidate the cache
hash.update(readFileSync(fileURLToPath(import.meta.url), 'utf8'));
for (const { state, csvPath } of stateFiles) {
if (existsSync(csvPath)) {
const content = readFileSync(csvPath, 'utf8');
hash.update(`${state}:${content}`);
}
}
return hash.digest('hex');
}
/**
* Check if expected locale files exist for discovered states
*/
function localeFilesExist(states) {
for (const locale of Object.keys(CONFIG.locales)) {
for (const state of states) {
// Check for landing.json as a representative file
const landingPath = join(CONFIG.outputDir, locale, state, 'landing.json');
if (!existsSync(landingPath)) {
return false;
}
}
}
return true;
}
/**
* Check if regeneration is needed based on hash
*/
function needsRegeneration(stateFiles) {
if (stateFiles.length === 0) return false;
const currentHash = calculateCombinedHash(stateFiles);
if (!existsSync(CONFIG.hashFile)) return true;
const states = stateFiles.map((f) => f.state);
if (!localeFilesExist(states)) return true;
const storedHash = readFileSync(CONFIG.hashFile, 'utf8').trim();
return currentHash !== storedHash;
}
/**
* Save current hash for cache invalidation
*/
function saveHash(stateFiles) {
const hash = calculateCombinedHash(stateFiles);
writeFileSync(CONFIG.hashFile, hash, 'utf8');
}
/**
* Clean output directory
*/
function cleanOutputDir() {
if (existsSync(CONFIG.outputDir)) {
rmSync(CONFIG.outputDir, { recursive: true });
}
}
/**
* Write locale files for a single state
*/
function writeStateLocaleFiles(stateData, state) {
let fileCount = 0;
for (const [locale, namespaces] of Object.entries(stateData)) {
for (const [namespace, translations] of Object.entries(namespaces)) {
if (Object.keys(translations).length === 0) continue;
const dir = join(CONFIG.outputDir, locale, state);
const filePath = join(dir, `${namespace}.json`);
mkdirSync(dir, { recursive: true });
writeFileSync(
filePath,
JSON.stringify(translations, null, 2) + '\n',
'utf8'
);
fileCount++;
}
}
return fileCount;
}
/**
* Validate that English and Spanish have matching keys for a state
*/
function validateStateCompleteness(stateData, state) {
const warnings = [];
// Collect all keys from English
const englishKeys = new Map();
for (const [namespace, translations] of Object.entries(stateData.en || {})) {
for (const key of Object.keys(translations)) {
englishKeys.set(`${namespace}.${key}`, true);
}
}
// Check Spanish for missing keys
for (const [fullKey] of englishKeys) {
const [namespace, key] = fullKey.split('.');
if (!stateData.es?.[namespace]?.[key]) {
warnings.push(`Missing Spanish translation in ${state}: ${fullKey}`);
}
}
return warnings;
}
// Which namespaces belong to which app.
// 'all' = shared, included in every app's barrel file.
const NAMESPACE_APP = {
common: 'all',
landing: 'all',
disclaimer: 'all',
personalInfo: 'all',
confirmInfo: 'all',
result: 'all',
// Portal-specific
login: 'portal',
idProofing: 'portal',
optIn: 'portal',
offBoarding: 'portal',
dashboard: 'portal',
edit: 'portal',
editContactPreferences: 'portal',
editMailingAddress: 'portal',
stepUpDisclaimer: 'portal',
stepUpFailure: 'portal',
proto: 'portal',
}
function isNamespaceForApp(namespace, app) {
if (!app) return true
// eslint-disable-next-line security/detect-object-injection -- namespace comes from JSON filenames, not user input
const mapped = NAMESPACE_APP[namespace] ?? 'all'
return mapped === 'all' || mapped === app
}
/**
* Generate TypeScript resource file from locale directory
*
* Scans content/locales/ for all JSON files and generates a TypeScript file
* with static imports and a typed resource map. This eliminates the need
* to manually maintain 100+ imports in i18n.ts and translations.ts.
*
* Output: src/lib/generated-locale-resources.ts
*/
function generateResourceFile() {
const outputPath = tsOutOverride
? path.resolve(process.cwd(), tsOutOverride)
: join(rootDir, 'src', 'lib', 'generated-locale-resources.ts');
if (!existsSync(CONFIG.outputDir)) {
console.log(
'⚠️ No locales directory found, skipping resource file generation'
);
return;
}
// Discover all locale JSON files: {lang}/{state}/{namespace}.json
const imports = [];
const stateMap = {}; // { state: { lang: { namespace: varName } } }
const allNamespaces = new Set();
const languages = readdirSync(CONFIG.outputDir)
.filter(
(f) =>
!f.startsWith('.') &&
existsSync(join(CONFIG.outputDir, f)) &&
readdirSync(join(CONFIG.outputDir, f)).length > 0
)
.sort();
for (const lang of languages) {
const langDir = join(CONFIG.outputDir, lang);
const states = readdirSync(langDir)
.filter(
(f) =>
!f.startsWith('.') &&
existsSync(join(langDir, f)) &&
readdirSync(join(langDir, f)).length > 0
)
.sort();
for (const state of states) {
const stateDir = join(CONFIG.outputDir, lang, state);
const files = readdirSync(stateDir)
.filter((f) => f.endsWith('.json'))
.sort();
for (const file of files) {
const namespace = file.replace('.json', '')
if (!isNamespaceForApp(namespace, appFilter)) continue
// Sanitize namespace for use as a JS identifier (handle hyphens → camelCase)
const safeNs = namespace.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
const varName = `${lang}${state.toUpperCase()}${safeNs.charAt(0).toUpperCase()}${safeNs.slice(1)}`;
const importPath = `@/content/locales/${lang}/${state}/${namespace}.json`;
imports.push({ varName, importPath });
allNamespaces.add(namespace);
if (!stateMap[state]) stateMap[state] = {};
if (!stateMap[state][lang]) stateMap[state][lang] = {};
stateMap[state][lang][namespace] = varName;
}
}
}
// Generate TypeScript file content
const lines = [
'// Auto-generated by content/scripts/generate-locales.js — DO NOT EDIT',
'// Run `pnpm copy:generate` to regenerate this file',
'',
];
// Static imports
for (const { varName, importPath } of imports) {
lines.push(`import ${varName} from '${importPath}'`);
}
lines.push('');
// stateResources export
lines.push('export const stateResources = {');
const sortedStates = Object.keys(stateMap).sort();
for (const state of sortedStates) {
lines.push(` ${state}: {`);
const sortedLangs = Object.keys(stateMap[state]).sort();
for (const lang of sortedLangs) {
lines.push(` ${lang}: {`);
const sortedNs = Object.keys(stateMap[state][lang]).sort();
for (const ns of sortedNs) {
// Quote keys that aren't valid JS identifiers (e.g., contain hyphens)
const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(ns) ? ns : `'${ns}'`;
lines.push(` ${key}: ${stateMap[state][lang][ns]},`);
}
lines.push(' },');
}
lines.push(' },');
}
lines.push('}');
lines.push('');
// namespaces export with Namespace type
const sortedNamespaces = [...allNamespaces].sort();
lines.push(
`export const namespaces = [${sortedNamespaces.map((n) => `'${n}'`).join(', ')}] as const`
);
lines.push('export type Namespace = (typeof namespaces)[number]');
lines.push('');
writeFileSync(outputPath, lines.join('\n'), 'utf8');
console.log(`📝 Generated resource file: ${rel(outputPath)}`);
console.log(
` ${imports.length} imports, ${sortedStates.length} states, ${sortedNamespaces.length} namespaces`
);
}
/**
* Main entry point
*/
function main() {
console.log('🌐 Generating i18n locale files...');
// Discover state CSV files
const stateFiles = discoverStateCsvFiles();
if (stateFiles.length === 0) {
console.log('⚠️ No state CSV files found in content/states/');
console.log(' Expected: content/states/dc.csv, content/states/co.csv, etc.');
console.log(' Download from Google Sheets (each tab as separate CSV).');
// Still generate resource file from any existing JSON files
generateResourceFile();
process.exit(0);
}
console.log(`📁 Found ${stateFiles.length} state CSV file(s):`);
for (const { state, csvPath } of stateFiles) {
console.log(` - ${state}: ${rel(csvPath)}`);
}
console.log();
// Check if CSV regeneration needed
if (needsRegeneration(stateFiles)) {
try {
// Clean output directory
cleanOutputDir();
let totalFileCount = 0;
const allWarnings = [];
// Process each state CSV
for (const { state, csvPath } of stateFiles) {
console.log(`📖 Processing ${state.toUpperCase()}:`);
console.log(` Reading: ${rel(csvPath)}`);
const csvContent = readFileSync(csvPath, 'utf8');
const rows = parseCSV(csvContent);
console.log(` Found ${rows.length - 1} content entries`);
// Build locale data for this state
const stateData = buildStateLocaleData(rows, state);
// Validate completeness
const warnings = validateStateCompleteness(stateData, state);
allWarnings.push(...warnings);
// Write locale files
const fileCount = writeStateLocaleFiles(stateData, state);
totalFileCount += fileCount;
console.log(` Generated ${fileCount} files\n`);
}
// Show validation warnings
if (allWarnings.length > 0) {
console.log('⚠️ Validation warnings:');
allWarnings.forEach((w) => console.log(` - ${w}`));
console.log();
}
// Save hash for caching
saveHash(stateFiles);
// Summary
console.log(`✅ Generated ${totalFileCount} total locale files:`);
for (const locale of Object.keys(CONFIG.locales)) {
for (const { state } of stateFiles) {
const dir = join(CONFIG.outputDir, locale, state);
if (existsSync(dir)) {
const files = readdirSync(dir);
console.log(` ${locale}/${state}/: ${files.join(', ')}`);
}
}
}
console.log();
} catch (error) {
console.error('❌ Failed to generate locales:');
console.error(` ${error.message}\n`);
process.exit(1);
}
} else {
console.log('⚡ Locales unchanged (cached)');
// List existing locale files
if (existsSync(CONFIG.outputDir)) {
const locales = readdirSync(CONFIG.outputDir);
console.log(` Existing: ${locales.join(', ')}`);
}
console.log();
}
// Always generate the TypeScript resource file from whatever JSON files exist on disk
generateResourceFile();
process.exit(0);
}
main();