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,83 +64,75 @@ 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({
.filter((dirent) => dirent.isDirectory()) files: [`${buildDir}/addon/**/*.xhtml`, `${buildDir}/addon/**/*.html`],
.map((dirent) => dirent.name);
for (const localeSubFolder of localeFolders) {
const localeSubDir = path.join(localeDir, localeSubFolder);
const localeSubFiles = readdirSync(localeSubDir, {
withFileTypes: true,
})
.filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name);
for (const localeSubFile of localeSubFiles) {
if (localeSubFile.endsWith(".ftl")) {
renameSync(
path.join(localeSubDir, localeSubFile),
path.join(localeSubDir, `${config.addonRef}-${localeSubFile}`),
);
}
}
}
const localeMessage = new Set();
const localeMessageMiss = new Set();
const replaceResultFlt = replaceInFileSync({
files: [`${buildDir}/addon/locale/**/*.ftl`],
processor: (fltContent) => {
const lines = fltContent.split("\n");
const prefixedLines = lines.map((line) => {
// https://regex101.com/r/lQ9x5p/1
const match = line.match(
/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m,
);
if (match) {
localeMessage.add(match.groups.message);
return `${config.addonRef}-${line}`;
} else {
return line;
}
});
return prefixedLines.join("\n");
},
});
const replaceResultXhtml = replaceInFileSync({
files: [`${buildDir}/addon/**/*.xhtml`],
processor: (input) => { processor: (input) => {
const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)];
matchs.map((match) => { matchs.map((match) => {
if (localeMessage.has(match[2])) { input = input.replace(
input = input.replace( match[0],
match[0], `${match[1]}="${config.addonRef}-${match[2]}"`,
`${match[1]}="${config.addonRef}-${match[2]}"`, );
); MessagesInHTML.add(match[2]);
} else {
localeMessageMiss.add(match[2]);
}
}); });
return input; return input;
}, },
}); });
Logger.debug( // Walk the sub folders of `build/addon/locale`
"[Build] Prepare locale files OK", const localesPath = path.join(buildDir, "addon/locale"),
// replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`), localeNames = readdirSync(localesPath, { withFileTypes: true })
// replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`), .filter((dirent) => dirent.isDirectory())
); .map((dirent) => dirent.name);
if (localeMessageMiss.size !== 0) { for (const localeName of localeNames) {
Logger.warn( const localePath = path.join(localesPath, localeName);
`[Build] Fluent message [${new Array( const ftlFiles = readdirSync(localePath, {
...localeMessageMiss, withFileTypes: true,
)}] do not exsit in addon's locale files.`, })
); .filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name);
// rename *.ftl to addonRef-*.ftl
for (const ftlFile of ftlFiles) {
if (ftlFile.endsWith(".ftl")) {
renameSync(
path.join(localePath, ftlFile),
path.join(localePath, `${config.addonRef}-${ftlFile}`),
);
}
}
// Prefix Fluent messages in each ftl
const MessageInThisLang = new Set();
replaceInFileSync({
files: [`${buildDir}/addon/locale/${localeName}/*.ftl`],
processor: (fltContent) => {
const lines = fltContent.split("\n");
const prefixedLines = lines.map((line) => {
// https://regex101.com/r/lQ9x5p/1
const match = line.match(
/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m,
);
if (match) {
MessageInThisLang.add(match.groups.message);
return `${config.addonRef}-${line}`;
} else {
return line;
}
});
return prefixedLines.join("\n");
},
});
// If a message in xhtml but not in ftl of current language, log it
MessagesInHTML.forEach((message) => {
if (!MessageInThisLang.has(message)) {
Logger.error(`[Build] ${message} don't exist in ${localeName}`);
}
});
} }
} }
@ -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();