Use the Fetch API to download the l10n files

Given that the Fetch API is supported since Node.js 18 we should be able to use it when downloading l10n files, which allows us to simplify the code and to make it fully `async`.
This commit is contained in:
Jonas Jenwald 2024-02-22 13:24:12 +01:00
parent fb9e438442
commit 2b407990e2

View File

@ -14,7 +14,6 @@
*/ */
import fs from "fs"; import fs from "fs";
import https from "https";
import path from "path"; import path from "path";
// Fetches all languages that have an *active* translation in mozilla-central. // Fetches all languages that have an *active* translation in mozilla-central.
@ -22,47 +21,34 @@ import path from "path";
const DEFAULT_LOCALE = "en-US"; const DEFAULT_LOCALE = "en-US";
const EXCLUDE_LANG_CODES = ["ca-valencia", "ja-JP-mac"]; const EXCLUDE_LANG_CODES = new Set(["ca-valencia", "ja-JP-mac"]);
function normalizeText(s) { function normalizeText(s) {
return s.replaceAll(/\r\n?/g, "\n").replaceAll("\uFEFF", ""); return s.replaceAll(/\r\n?/g, "\n").replaceAll("\uFEFF", "");
} }
function downloadLanguageCodes() { async function downloadLanguageCodes() {
console.log("Downloading language codes...\n"); console.log("Downloading language codes...\n");
const ALL_LOCALES = const ALL_LOCALES =
"https://hg.mozilla.org/mozilla-central/raw-file/tip/browser/locales/all-locales"; "https://hg.mozilla.org/mozilla-central/raw-file/tip/browser/locales/all-locales";
return new Promise(function (resolve) { const response = await fetch(ALL_LOCALES);
https.get(ALL_LOCALES, function (response) { if (!response.ok) {
if (response.statusCode === 200) { throw new Error(response.statusText);
let content = ""; }
response.setEncoding("utf8"); const content = await response.text();
response.on("data", function (chunk) {
content += chunk; // Remove any leading/trailing white-space.
}); const langCodes = normalizeText(content.trim()).split("\n");
response.on("end", function () { // Remove all locales that we don't want to download below.
content = content.trim(); // Remove any leading/trailing white-space. return langCodes.filter(
const langCodes = normalizeText(content).split("\n"); langCode => langCode !== DEFAULT_LOCALE && !EXCLUDE_LANG_CODES.has(langCode)
// Remove all locales that we don't want to download below. );
for (const langCode of [DEFAULT_LOCALE, ...EXCLUDE_LANG_CODES]) {
const i = langCodes.indexOf(langCode);
if (i > -1) {
langCodes.splice(i, 1);
}
}
resolve(langCodes);
});
} else {
resolve([]);
}
});
});
} }
function downloadLanguageFiles(root, langCode) { async function downloadLanguageFiles(root, langCode) {
console.log("Downloading " + langCode + "..."); console.log(`Downloading ${langCode}...`);
// Constants for constructing the URLs. Translations are taken from the // Constants for constructing the URLs. Translations are taken from the
// Nightly channel as those are the most recent ones. // Nightly channel as those are the most recent ones.
@ -71,41 +57,27 @@ function downloadLanguageFiles(root, langCode) {
// Defines which files to download for each language. // Defines which files to download for each language.
const files = ["viewer.ftl"]; const files = ["viewer.ftl"];
let downloadsLeft = files.length;
const outputDir = path.join(root, langCode); const outputDir = path.join(root, langCode);
if (!fs.existsSync(outputDir)) { if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir); fs.mkdirSync(outputDir);
} }
return new Promise(function (resolve) { // Download the necessary files for this language.
// Download the necessary files for this language. for (const fileName of files) {
files.forEach(function (fileName) { const outputPath = path.join(outputDir, fileName);
const outputPath = path.join(outputDir, fileName); const url = MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
const url =
MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
https.get(url, function (response) { const response = await fetch(url);
// Not all files exist for each language. Files without translations if (!response.ok) {
// have been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175). // Not all files exist for each language. Files without translations
if (response.statusCode === 200) { // have been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175).
let content = ""; continue;
response.setEncoding("utf8"); }
response.on("data", function (chunk) { const content = await response.text();
content += chunk;
}); fs.writeFileSync(outputPath, normalizeText(content), "utf8");
response.on("end", function () { }
fs.writeFileSync(outputPath, normalizeText(content), "utf8");
if (--downloadsLeft === 0) {
resolve();
}
});
} else if (--downloadsLeft === 0) {
resolve();
}
});
});
});
} }
async function downloadL10n(root) { async function downloadL10n(root) {