💄 Reformat affected JS files

This commit is contained in:
Belén Albeza 2024-07-01 10:28:40 +02:00
parent 3efd5cb9e8
commit ecbedf847f
28 changed files with 720 additions and 285 deletions

View file

@ -1,26 +1,25 @@
const fs = require('fs').promises;
const fs = require("fs").promises;
const gt = require("gettext-parser");
const path = require('path');
const util = require('node:util');
const execFile = util.promisify(require('node:child_process').execFile);
const path = require("path");
const util = require("node:util");
const execFile = util.promisify(require("node:child_process").execFile);
async function processMsgId(msgId){
return execFile('grep', ['-r', '-o', msgId, './src'])
.catch(()=> { return msgId})
async function processMsgId(msgId) {
return execFile("grep", ["-r", "-o", msgId, "./src"]).catch(() => {
return msgId;
});
}
async function processFile(f) {
const content = await fs.readFile(f);
const data = gt.po.parse(content, "utf-8")
const translations = data.translations[''];
const data = gt.po.parse(content, "utf-8");
const translations = data.translations[""];
const badIds = [];
for (const property in translations) {
const data = await processMsgId(translations[property].msgid);
if (data!=null && data.stdout === undefined){
badIds.push(data)
if (data != null && data.stdout === undefined) {
badIds.push(data);
}
}
@ -28,63 +27,77 @@ async function processFile(f) {
}
async function cleanFile(f, badIds) {
console.log ("\n\nDoing automatic cleanup")
console.log("\n\nDoing automatic cleanup");
const content = await fs.readFile(f);
const data = gt.po.parse(content, "utf-8");
const translations = data.translations[''];
const translations = data.translations[""];
const keys = Object.keys(translations);
for (const key of keys) {
property = translations[key];
if (badIds.includes(property.msgid)){
console.log ('----> deleting', property.msgid)
delete data.translations[''][key];
if (badIds.includes(property.msgid)) {
console.log("----> deleting", property.msgid);
delete data.translations[""][key];
}
}
const buff = gt.po.compile(data, {sort: true});
const buff = gt.po.compile(data, { sort: true });
await fs.writeFile(f, buff);
}
async function findExecutionTimeTranslations() {
const { stdout } = await execFile('grep', ['-r', '-h', '-F', '(tr (', './src']);
const { stdout } = await execFile("grep", [
"-r",
"-h",
"-F",
"(tr (",
"./src",
]);
console.log(stdout);
}
async function welcome() {
console.log ('####################################################################')
console.log ('# UNUSED TRANSLATIONS FINDER #')
console.log ('####################################################################')
console.log ('\n');
console.log ('DISCLAIMER: Some translations are only available at execution time.')
console.log (' This finder can\'t process them, so there can be')
console.log (' false positives.\n')
console.log (' If you want to do an automatic clean anyway,')
console.log (' call the script with:')
console.log (' npm run find-unused-translations -- --clean')
console.log (' For example:');
console.log ('--------------------------------------------------------------------');
console.log(
"####################################################################",
);
console.log(
"# UNUSED TRANSLATIONS FINDER #",
);
console.log(
"####################################################################",
);
console.log("\n");
console.log(
"DISCLAIMER: Some translations are only available at execution time.",
);
console.log(" This finder can't process them, so there can be");
console.log(" false positives.\n");
console.log(" If you want to do an automatic clean anyway,");
console.log(" call the script with:");
console.log(" npm run find-unused-translations -- --clean");
console.log(" For example:");
console.log(
"--------------------------------------------------------------------",
);
await findExecutionTimeTranslations();
console.log ('--------------------------------------------------------------------');
console.log(
"--------------------------------------------------------------------",
);
}
const doCleanup = process.argv.slice(2)[0] == "--clean";
;(async () => {
(async () => {
await welcome();
const target = path.normalize("./translations/en.po");
const badIds = await processFile(target);
if (doCleanup){
if (doCleanup) {
cleanFile(target, badIds);
} else {
for (const badId of badIds){
for (const badId of badIds) {
console.log(badId);
}
}
})()
})();