forked from WJDDesigns/Ultra-Card
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-build.js
More file actions
141 lines (118 loc) · 4.29 KB
/
prepare-build.js
File metadata and controls
141 lines (118 loc) · 4.29 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
#!/usr/bin/env node
/**
* Pre-build script for Ultra Card
* Syncs translation files to ensure all language files have the same keys as en.json
*/
const fs = require('fs');
const path = require('path');
console.log('🔄 Running pre-build checks...\n');
/**
* Function to sync version from version.ts to package.json
* version.ts is the single source of truth for the version
*/
function syncVersion() {
console.log('📦 Syncing version from version.ts...');
const versionTsPath = path.resolve(__dirname, 'src/version.ts');
const packageJsonPath = path.resolve(__dirname, 'package.json');
try {
const versionTsContent = fs.readFileSync(versionTsPath, 'utf8');
const versionMatch = versionTsContent.match(/VERSION\s*=\s*['"]([^'"]+)['"]/);
if (!versionMatch) {
console.error('❌ Could not extract version from version.ts');
return;
}
const version = versionMatch[1];
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson.version !== version) {
packageJson.version = version;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
console.log(` ✅ Updated package.json version to ${version}`);
} else {
console.log(` ✅ package.json version already at ${version}`);
}
} catch (error) {
console.error('❌ Error syncing version:', error);
}
}
/**
* Function to synchronize translation files
* This ensures all keys present in the English file exist in all other language files
*/
function syncTranslationFiles() {
console.log('📝 Syncing translation files...');
const translationsDir = path.resolve(__dirname, 'src/translations');
const enFilePath = path.resolve(translationsDir, 'en.json');
// Ensure the translations directory exists
if (!fs.existsSync(translationsDir)) {
console.error('❌ Translations directory not found!');
return;
}
// Read the English file as the source of truth
let enTranslations;
try {
enTranslations = JSON.parse(fs.readFileSync(enFilePath, 'utf8'));
} catch (error) {
console.error('❌ Error reading English translations file:', error);
return;
}
// Get all translation files
const translationFiles = fs
.readdirSync(translationsDir)
.filter(file => file.endsWith('.json') && file !== 'en.json');
if (translationFiles.length === 0) {
console.log(' No additional translation files found.');
return;
}
// Create a recursive function to ensure all keys from source exist in target
function ensureKeysExist(sourceObj, targetObj, path = '') {
let modified = false;
// Process all keys in the source object
for (const key of Object.keys(sourceObj)) {
const newPath = path ? `${path}.${key}` : key;
// If the key doesn't exist in the target, add it
if (!(key in targetObj)) {
targetObj[key] = sourceObj[key];
console.log(` ➕ Added missing key: ${newPath}`);
modified = true;
continue;
}
// If both are objects, recursively ensure keys
if (
typeof sourceObj[key] === 'object' &&
sourceObj[key] !== null &&
typeof targetObj[key] === 'object' &&
targetObj[key] !== null
) {
const subModified = ensureKeysExist(sourceObj[key], targetObj[key], newPath);
if (subModified) modified = true;
}
}
return modified;
}
// Process each translation file
let totalModified = 0;
for (const file of translationFiles) {
const filePath = path.resolve(translationsDir, file);
try {
let translations = JSON.parse(fs.readFileSync(filePath, 'utf8'));
// Ensure all keys from English translation exist
const modified = ensureKeysExist(enTranslations, translations);
// Save the file if it was modified
if (modified) {
fs.writeFileSync(filePath, JSON.stringify(translations, null, 2) + '\n');
totalModified++;
}
} catch (error) {
console.error(` ❌ Error processing translation file ${file}:`, error);
}
}
if (totalModified > 0) {
console.log(` ✅ Updated ${totalModified} translation file(s)`);
} else {
console.log(' ✅ All translation files are in sync');
}
}
// Run the synchronization
syncVersion();
syncTranslationFiles();
console.log('\n✨ Pre-build checks complete!\n');