1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-24 22:00:22 +01:00
CanvasBlocker/.tools/findUnusedMessages.js

174 lines
4.4 KiB
JavaScript
Raw Normal View History

const fs = require("fs");
const path = require("path");
const util = require("util");
function getMessagesInContent(content){
2019-11-30 02:05:37 +01:00
"use strict";
const foundMessages = [];
[
2019-06-29 16:55:26 +02:00
/\b(?:_|browser.i18n.getMessage|extension.getTranslation|notify|extension)\(["']([^"']+)["']\s*(?:\)|,)/g,
2019-04-09 08:29:52 +02:00
/\b(?:messageId|name|getTranslation)\s*:\s*["']([^"']+)["']/g,
].forEach(function(re){
let match;
while ((match = re.exec(content)) !== null){
foundMessages.push(match[1].toLowerCase());
}
});
return foundMessages;
}
async function getMessagesInFile(path){
2019-11-30 02:05:37 +01:00
"use strict";
const exists = await util.promisify(fs.exists)(path);
if (exists){
const content = await util.promisify(fs.readFile)(path, {encoding: "UTF-8"});
return getMessagesInContent(content);
}
else {
console.log("file does not exist:", path);
return [];
}
}
async function getMessagesInFolder(folder){
2019-11-30 02:05:37 +01:00
"use strict";
const files = await util.promisify(fs.readdir)(folder, {encoding: "UTF-8"});
const messages = await Promise.all(
files.filter(function(file){
return !file.startsWith(".");
}).map(function(file){
return path.join(folder, file);
}).map(async function(path){
const stat = await util.promisify(fs.stat)(path);
if (stat.isDirectory()){
return getMessagesInFolder(path);
}
else {
if (path.endsWith(".js")){
return getMessagesInFile(path);
}
else {
return [];
}
}
})
);
const flat = [];
messages.forEach(function(messages){
messages.forEach(function(message){
flat.push(message);
});
2019-11-30 02:05:37 +01:00
});
return flat;
}
2019-11-30 02:05:37 +01:00
async function getSettingMessages(){
"use strict";
const settingStrings = require("../lib/settingStrings");
const settingDefinitions = require("../lib/settingDefinitions");
function getDefinition(name){
return settingDefinitions.filter(function(settingDefinition){
return settingDefinition.name === name;
})[0];
}
const settingsDisplay = require("../options/settingsDisplay");
const foundMessages = [];
2019-06-29 16:55:26 +02:00
settingsDisplay.forEach(function(groupDefinition){
if (groupDefinition.name){
foundMessages.push("group_" + groupDefinition.name.toLowerCase());
}
2019-06-29 16:55:26 +02:00
groupDefinition.sections.forEach(function(sectionDefinition){
if (sectionDefinition.name){
foundMessages.push("section_" + sectionDefinition.name.toLowerCase());
}
2019-06-29 16:55:26 +02:00
sectionDefinition.settings.forEach(function(display){
let settingDefinition = getDefinition(display.name);
if (!settingDefinition){
settingDefinition = display;
display.action = true;
}
2019-06-29 16:55:26 +02:00
if (settingDefinition){
if (display.inputs){
settingDefinition.inputs = display.inputs.map(function(input){
return getDefinition(input);
});
}
else if (display.actions){
settingDefinition.actions = display.actions.map(function(action){
return {name: action};
});
}
settingStrings.getMessages(settingDefinition).forEach(function(message){
foundMessages.push(message.toLowerCase());
});
}
2019-06-29 16:55:26 +02:00
});
});
});
2019-11-30 02:05:37 +01:00
const presets = require("../options/presets.json");
Object.keys(presets).forEach(function(preset){
foundMessages.push("preset_" + preset + "_title");
foundMessages.push("preset_" + preset + "_description");
});
return foundMessages.map(function(message){return message.toLowerCase();});
}
async function getKnownMessages(){
2019-11-30 02:05:37 +01:00
"use strict";
return [
"addon_title",
"addon_description",
2019-11-30 02:05:37 +01:00
"urlSettings_title",
"installnotice",
2019-11-30 02:05:37 +01:00
"presets_installnotice",
"updatenotice",
2019-11-30 02:05:37 +01:00
"disableNotifications",
"showoptions",
2019-11-30 02:05:37 +01:00
"displayHiddenSettings_title",
"displayHiddenSettings_description",
"browseraction_settings",
"browseraction_test",
"browseraction_review",
2019-11-30 02:05:37 +01:00
"browseraction_reportIssue",
].map(function(message){
return message.toLowerCase();
});
}
2019-11-30 02:05:37 +01:00
async function main(){
"use strict";
const en = require("../_locales/en/messages.json");
const declaredMessages = Object.keys(en)
// .filter(function(key){return en[key].message;})
.map(function(key){
return key.toLowerCase();
});
const [settingMessages, fileMessages, knownMessages] = await Promise.all([
getSettingMessages(),
getMessagesInFolder(path.join(__dirname, "..")),
getKnownMessages()]
);
declaredMessages.forEach(function(message){
2019-11-30 02:05:37 +01:00
if (
fileMessages.indexOf(message) === -1 &&
settingMessages.indexOf(message) === -1 &&
knownMessages.indexOf(message) === -1
){
2019-06-29 16:55:26 +02:00
console.log(`usage of ${message} not found`);
}
});
2019-11-30 02:05:37 +01:00
}
main();