Skip to content

Commit 5f3ffb5

Browse files
committed
improve lang check script to also check for stale strings
1 parent 5d0b603 commit 5f3ffb5

1 file changed

Lines changed: 72 additions & 90 deletions

File tree

utils/lang.js

Lines changed: 72 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -176,106 +176,88 @@ async function update() {
176176
let key;
177177

178178
if (command === "check") {
179-
let total = 0;
180-
let done = 0;
181-
182-
fs.readFile(enLang, "utf-8", (err, data) => {
183-
if (err) {
184-
console.error(err);
185-
process.exit(0);
186-
return;
187-
}
188-
189-
let error = false;
179+
let error = false;
180+
const fix = arg === "fix";
181+
const enLangData = JSON.parse(fs.readFileSync(enLang, "utf8"));
182+
const enKeys = Object.keys(enLangData);
183+
184+
for (const file of list) {
185+
if (file === "en-us.json") continue;
186+
187+
let flagError = false;
188+
const langFile = path.join(dir, file);
189+
const langData = JSON.parse(fs.readFileSync(langFile, "utf8"));
190+
191+
const langError = () => {
192+
if (!flagError) {
193+
error = true;
194+
flagError = true;
195+
console.log(`-------------- ${file}`);
196+
}
197+
};
198+
199+
for (const enKey of enKeys) {
200+
const key = Object.keys(langData).find((k) => {
201+
try {
202+
return new RegExp(`^${escapeRegExp(k)}$`, "i").test(enKey);
203+
} catch (e) {
204+
console.log({ e, k });
205+
return false;
206+
}
207+
});
190208

191-
const fix = arg === "fix";
192-
const enLangData = JSON.parse(data);
209+
if (!key) {
210+
langError();
211+
if (fix) {
212+
langData[enKey] = enLangData[enKey];
213+
}
193214

194-
list.forEach((file, i) => {
195-
if (file === "en-us.json") return;
215+
console.log(`Missing: ${enKey} ${fix ? "✔" : ""}`);
216+
} else if (key !== enKey) {
217+
langError();
218+
console.log(`Fix: "${key} --> ${enKey}" ${fix ? "✔" : ""}`);
196219

197-
let flagError = false;
198-
let langFile = path.join(dir, file);
199-
const exit = (i, len) => {
200-
if (i + 1 === len) {
201-
if (!error) {
202-
console.log("\nGOOD NEWS! No Error Found\n");
203-
}
204-
process.exit(0);
220+
if (fix) {
221+
const val = langData[key];
222+
delete langData[key];
223+
langData[enKey] = val;
205224
}
206-
};
225+
}
226+
}
207227

208-
fs.readFile(langFile, "utf-8", (err, data) => {
209-
if (err) {
210-
console.error(err);
211-
process.exit(1);
212-
return;
228+
for (const key in langData) {
229+
const enKey = enKeys.find((k) => {
230+
try {
231+
return new RegExp(`^${escapeRegExp(k)}$`, "i").test(key);
232+
} catch (e) {
233+
console.log({ e, k });
234+
return false;
213235
}
236+
});
214237

215-
let langError = () => {
216-
if (!flagError) {
217-
error = true;
218-
flagError = true;
219-
console.log(`-------------- ${file}`);
220-
}
221-
};
222-
const langData = JSON.parse(data);
223-
flagError = false;
224-
225-
for (let enKey in enLangData) {
226-
const key = Object.keys(langData).find((k) => {
227-
try {
228-
if (new RegExp(`^${escapeRegExp(k)}$`, "i").test(enKey)) {
229-
return true;
230-
}
231-
return false;
232-
} catch (e) {
233-
console.log({ e, k });
234-
return false;
235-
}
236-
});
237-
238-
if (!key) {
239-
langError();
240-
if (fix) {
241-
langData[enKey] = enLangData[enKey];
242-
}
243-
244-
console.log(`Missing: ${enKey} ${fix ? "✔" : ""}`);
245-
} else if (key !== enKey) {
246-
langError();
247-
console.log(`Fix: "${key} --> ${enKey}" ${fix ? "✔" : ""}`);
248-
249-
if (fix) {
250-
const val = langData[key];
251-
delete langData[key];
252-
langData[enKey] = val;
253-
}
254-
}
238+
if (!enKey) {
239+
langError();
240+
if (fix) {
241+
delete langData[key];
255242
}
256243

257-
if (flagError) {
258-
if (fix) {
259-
total += 1;
260-
const langJSONData = JSON.stringify(langData, undefined, 2);
261-
fs.writeFile(langFile, langJSONData, (err) => {
262-
if (err) {
263-
console.error(err);
264-
process.exit(1);
265-
}
266-
done += 1;
267-
exit(done, total);
268-
});
269-
}
270-
console.log("\n");
271-
}
244+
console.log(`Stale: ${key} ${fix ? "✔" : ""}`);
245+
}
246+
}
272247

273-
if (!fix) {
274-
exit(i, len);
275-
}
276-
});
277-
});
278-
});
248+
if (flagError) {
249+
if (fix) {
250+
const langJSONData = JSON.stringify(langData, undefined, 2);
251+
fs.writeFileSync(langFile, langJSONData);
252+
}
253+
console.log("\n");
254+
}
255+
}
256+
257+
if (!error) {
258+
console.log("\nGOOD NEWS! No Error Found\n");
259+
}
260+
process.exit(error && !fix ? 1 : 0);
279261
return;
280262
}
281263

0 commit comments

Comments
 (0)