fix: log miss ftl messages in build (#95)

This commit is contained in:
Northword 2024-01-06 00:14:07 +08:00 committed by GitHub
parent 7738410680
commit 0663e5b0b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,35 +64,51 @@ function replaceString(buildTime) {
} }
function prepareLocaleFiles() { function prepareLocaleFiles() {
// Walk the builds/addon/locale folder's sub folders and rename *.ftl to addonRef-*.ftl // Prefix Fluent messages in xhtml
const localeDir = path.join(buildDir, "addon/locale"); const MessagesInHTML = new Set();
const localeFolders = readdirSync(localeDir, { withFileTypes: true }) replaceInFileSync({
files: [`${buildDir}/addon/**/*.xhtml`, `${buildDir}/addon/**/*.html`],
processor: (input) => {
const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)];
matchs.map((match) => {
input = input.replace(
match[0],
`${match[1]}="${config.addonRef}-${match[2]}"`,
);
MessagesInHTML.add(match[2]);
});
return input;
},
});
// Walk the sub folders of `build/addon/locale`
const localesPath = path.join(buildDir, "addon/locale"),
localeNames = readdirSync(localesPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory()) .filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name); .map((dirent) => dirent.name);
for (const localeSubFolder of localeFolders) { for (const localeName of localeNames) {
const localeSubDir = path.join(localeDir, localeSubFolder); const localePath = path.join(localesPath, localeName);
const localeSubFiles = readdirSync(localeSubDir, { const ftlFiles = readdirSync(localePath, {
withFileTypes: true, withFileTypes: true,
}) })
.filter((dirent) => dirent.isFile()) .filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name); .map((dirent) => dirent.name);
for (const localeSubFile of localeSubFiles) { // rename *.ftl to addonRef-*.ftl
if (localeSubFile.endsWith(".ftl")) { for (const ftlFile of ftlFiles) {
if (ftlFile.endsWith(".ftl")) {
renameSync( renameSync(
path.join(localeSubDir, localeSubFile), path.join(localePath, ftlFile),
path.join(localeSubDir, `${config.addonRef}-${localeSubFile}`), path.join(localePath, `${config.addonRef}-${ftlFile}`),
); );
} }
} }
}
const localeMessage = new Set(); // Prefix Fluent messages in each ftl
const localeMessageMiss = new Set(); const MessageInThisLang = new Set();
replaceInFileSync({
const replaceResultFlt = replaceInFileSync({ files: [`${buildDir}/addon/locale/${localeName}/*.ftl`],
files: [`${buildDir}/addon/locale/**/*.ftl`],
processor: (fltContent) => { processor: (fltContent) => {
const lines = fltContent.split("\n"); const lines = fltContent.split("\n");
const prefixedLines = lines.map((line) => { const prefixedLines = lines.map((line) => {
@ -101,7 +117,7 @@ function prepareLocaleFiles() {
/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m, /^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m,
); );
if (match) { if (match) {
localeMessage.add(match.groups.message); MessageInThisLang.add(match.groups.message);
return `${config.addonRef}-${line}`; return `${config.addonRef}-${line}`;
} else { } else {
return line; return line;
@ -111,36 +127,12 @@ function prepareLocaleFiles() {
}, },
}); });
const replaceResultXhtml = replaceInFileSync({ // If a message in xhtml but not in ftl of current language, log it
files: [`${buildDir}/addon/**/*.xhtml`], MessagesInHTML.forEach((message) => {
processor: (input) => { if (!MessageInThisLang.has(message)) {
const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; Logger.error(`[Build] ${message} don't exist in ${localeName}`);
matchs.map((match) => {
if (localeMessage.has(match[2])) {
input = input.replace(
match[0],
`${match[1]}="${config.addonRef}-${match[2]}"`,
);
} else {
localeMessageMiss.add(match[2]);
} }
}); });
return input;
},
});
Logger.debug(
"[Build] Prepare locale files OK",
// replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`),
// replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`),
);
if (localeMessageMiss.size !== 0) {
Logger.warn(
`[Build] Fluent message [${new Array(
...localeMessageMiss,
)}] do not exsit in addon's locale files.`,
);
} }
} }
@ -212,19 +204,21 @@ export async function main() {
); );
clearFolder(buildDir); clearFolder(buildDir);
copyFolderRecursiveSync("addon", buildDir); copyFolderRecursiveSync("addon", buildDir);
replaceString(buildTime);
Logger.debug("[Build] Replace OK");
Logger.debug("[Build] Replacing");
replaceString(buildTime);
Logger.debug("[Build] Preparing locale files");
prepareLocaleFiles(); prepareLocaleFiles();
Logger.debug("[Build] Running esbuild");
await build(esbuildOptions); await build(esbuildOptions);
Logger.debug("[Build] Run esbuild OK");
Logger.debug("[Build] Addon prepare OK"); Logger.debug("[Build] Addon prepare OK");
if (process.env.NODE_ENV === "production") { if (process.env.NODE_ENV === "production") {
Logger.debug("[Build] Packing Addon");
await zip.compressDir( await zip.compressDir(
path.join(buildDir, "addon"), path.join(buildDir, "addon"),
path.join(buildDir, `${name}.xpi`), path.join(buildDir, `${name}.xpi`),
@ -232,7 +226,6 @@ export async function main() {
ignoreBase: true, ignoreBase: true,
}, },
); );
Logger.debug("[Build] Addon pack OK");
prepareUpdateJson(); prepareUpdateJson();