Skip to content

Commit

Permalink
fix: better logs for prefixing prefs key and data-i10n-id (#92)
Browse files Browse the repository at this point in the history
* fix: pThe preference value appears as undefined in the preference window if it is not initialized in pref.js.

* fix: better debug logger

* tweaks

---------

Co-authored-by: Northword <northword@outlook.com>
  • Loading branch information
l0o0 and northword authored Jan 14, 2025
1 parent 03a085f commit 83d3e10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 18 additions & 15 deletions packages/scaffold/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class Build extends Base {
// rename *.ftl to addonRef-*.ftl
if (build.fluent.prefixLocaleFiles === true) {
await move(ftlPath, `${dirname(ftlPath)}/${namespace}-${basename(ftlPath)}`);
this.logger.debug(`Prefix filename: ${ftlPath}`);
this.logger.debug(`FTL file '${ftlPath}' is renamed to '${namespace}-${basename(ftlPath)}'.`);
}
}));

Expand All @@ -205,25 +205,26 @@ export default class Build extends Base {
const [matched, attrKey, attrVal] = match;

if (!allMessages.has(attrVal)) {
this.logger.debug(`HTML data-i10n-id ${attrVal} do not exist in any FTL message, skip to namespace`);
this.logger.warn(`HTML data-i10n-id '${chalk.blue(attrVal)}' in ${chalk.gray(htmlPath)} do not exist in any FTL message, skip to namespace it.`);
continue;
}

messagesInHTML.add(attrVal);
const namespacedAttr = `${namespace}-${attrVal}`;
htmlContent = htmlContent.replace(matched, `${attrKey}="${namespacedAttr}"`);
this.logger.debug(`HTML data-i10n-id '${chalk.blue(attrVal)}' in ${chalk.gray(htmlPath)} is namespaced to ${chalk.blue(namespacedAttr)}.`);
}

if (build.fluent.prefixFluentMessages)
await writeFile(htmlPath, htmlContent);
}));

// Check miss 1: Cross check in diff locale - seems no need
// messagesMap.forEach((messageInThisLang, lang) => {
// messagesByLocale.forEach((messageInThisLang, lang) => {
// // Needs Nodejs 22
// const diff = allMessages.difference(messageInThisLang);
// if (diff.size)
// this.logger.warn(`FTL messages "${Array.from(diff).join(", ")} don't exist the locale ${lang}"`);
// this.logger.warn(`FTL messages '${Array.from(diff).join(", ")}' don't exist the locale '${lang}'`);
// });

// Check miss 2: Check ids in HTML but not in ftl
Expand All @@ -233,7 +234,7 @@ export default class Build extends Base {
.map(([locale]) => locale);

if (missingLocales.length > 0) {
this.logger.warn(`HTML data-l10n-id "${messageInHTML}" is missing in locales: ${missingLocales.join(", ")}`);
this.logger.warn(`HTML data-l10n-id '${chalk.blue(messageInHTML)}' is missing in locales: ${missingLocales.join(", ")}.`);
}
});
}
Expand Down Expand Up @@ -272,27 +273,29 @@ export default class Build extends Base {

// Prefix pref keys in xhtml
if (prefixPrefKeys) {
const HTML_PREFERENCE_PATTERN = new RegExp(`preference="((?!${prefix})\\S*)"`, "g");
const HTML_PREFERENCE_PATTERN = /preference="(\S*)"/g;
const xhtmlPaths = await glob(`${dist}/addon/**/*.xhtml`);
await Promise.all(xhtmlPaths.map(async (path) => {
let content = await readFile(path, "utf-8");
const matchs = [...content.matchAll(HTML_PREFERENCE_PATTERN)];
for (const match of matchs) {
const [matched, key] = match;
if (!(key in prefsWithoutPrefix) && !(key in prefsWithoutPrefix)) {
this.logger.warn(`preference key '${key}' in ${path.replace(`${dist}/`, "")} not init in prefs.js`);
if (key.startsWith(prefix)) {
this.logger.debug(`Pref key '${chalk.blue(key)}' is already starts with '${prefix}', skip prefixing it.`);
continue;
}
if (key.startsWith(prefix)) {
else if (key.startsWith("extensions.")) {
this.logger.warn(`Pref key '${chalk.blue(key)}' in ${chalk.gray(path)} starts with 'extensions.' but not '${chalk.blue(prefix)}', skip prefixing it.`);
continue;
}
else if (!(key in prefsWithPrefix) && !(key in prefsWithoutPrefix)) {
this.logger.warn(`Pref key '${chalk.blue(key)}' in ${chalk.gray(path)} is not found in prefs.js, skip prefixing it.`);
continue;
}
// else if (key.startsWith("extensions.")) {
// this.logger.warn(`Pref key '${key}' in ${path} starts with 'extensions' but not ${prefix}.`);
// this.logger.debug(`Skip prefixing '${key}' since it starts with 'extensions'.`);
// continue;
// }
else {
content = content.replace(matched, `preference="${prefix}.${key}"`);
const prefixed = `${prefix}.${key}`;
this.logger.debug(`Pref key '${chalk.blue(key)}' in ${chalk.gray(path)} is prefixed to ${chalk.blue(prefixed)}.`);
content = content.replace(matched, `preference="${prefixed}"`);
}
}
await outputFile(path, content, "utf-8");
Expand Down
2 changes: 1 addition & 1 deletion packages/scaffold/src/utils/prefs-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class PrefsManager {
const content = this.render();
// console.log(content);
await outputFile(path, content, "utf-8");
logger.debug("The <profile>/prefs.js has been modified.");
logger.debug("The prefs.js has been modified.");
}

setPref(key: string, value: any) {
Expand Down

0 comments on commit 83d3e10

Please sign in to comment.