Skip to content

Commit

Permalink
Modernize localize script
Browse files Browse the repository at this point in the history
This file is pretty much the same as it was when it was committed in
2017; these days, we can write clearer code with async/await and new FS
APIs.
  • Loading branch information
jakebailey committed Oct 14, 2022
1 parent b876818 commit 1d9a16b
Showing 1 changed file with 33 additions and 76 deletions.
109 changes: 33 additions & 76 deletions scripts/generateLocalizedDiagnosticMessages.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fs from "fs";
import path from "path";
import xml2js from "xml2js";
import util from "util";

function main() {
const parseString = util.promisify(xml2js.parseString);

async function main() {
const args = process.argv.slice(2);
if (args.length !== 3) {
console.log("Usage:");
Expand All @@ -18,35 +21,28 @@ function main() {
generateLCGFile();

// generate other langs
fs.readdir(inputPath, (err, files) => {
handleError(err);
files.forEach(visitDirectory);
});
const files = await fs.promises.readdir(inputPath);
await Promise.all(files.map(visitDirectory));

return;

/**
* @param {string} name
*/
function visitDirectory(name) {
async function visitDirectory(name) {
const inputFilePath = path.join(inputPath, name, "diagnosticMessages", "diagnosticMessages.generated.json.lcl");

fs.readFile(inputFilePath, (err, data) => {
handleError(err);
xml2js.parseString(data.toString(), (err, result) => {
handleError(err);
if (!result || !result.LCX || !result.LCX.$ || !result.LCX.$.TgtCul) {
console.error("Unexpected XML file structure. Expected to find result.LCX.$.TgtCul.");
process.exit(1);
}
const outputDirectoryName = getPreferredLocaleName(result.LCX.$.TgtCul).toLowerCase();
if (!outputDirectoryName) {
console.error(`Invalid output locale name for '${result.LCX.$.TgtCul}'.`);
process.exit(1);
}
writeFile(path.join(outputPath, outputDirectoryName, "diagnosticMessages.generated.json"), xmlObjectToString(result));
});
});
const contents = await fs.promises.readFile(inputFilePath, "utf-8");
const result = await parseString(contents);
if (!result || !result.LCX || !result.LCX.$ || !result.LCX.$.TgtCul) {
console.error("Unexpected XML file structure. Expected to find result.LCX.$.TgtCul.");
process.exit(1);
}
const outputDirectoryName = getPreferredLocaleName(result.LCX.$.TgtCul).toLowerCase();
if (!outputDirectoryName) {
console.error(`Invalid output locale name for '${result.LCX.$.TgtCul}'.`);
process.exit(1);
}
await writeFile(path.join(outputPath, outputDirectoryName, "diagnosticMessages.generated.json"), xmlObjectToString(result));
}

/**
Expand Down Expand Up @@ -80,16 +76,6 @@ function main() {
}
}

/**
* @param {null | object} err
*/
function handleError(err) {
if (err) {
console.error(err);
process.exit(1);
}
}

/**
* @param {any} o
*/
Expand All @@ -115,55 +101,26 @@ function main() {
return JSON.stringify(out, undefined, 2);
}


/**
* @param {string} directoryPath
* @param {() => void} action
*/
function ensureDirectoryExists(directoryPath, action) {
fs.exists(directoryPath, exists => {
if (!exists) {
const basePath = path.dirname(directoryPath);
if (basePath !== directoryPath) {
return ensureDirectoryExists(basePath, () => fs.mkdir(directoryPath, action));
}
}
action();
});
}

/**
* @param {string} fileName
* @param {string} contents
*/
function writeFile(fileName, contents) {
ensureDirectoryExists(path.dirname(fileName), () => {
fs.writeFile(fileName, contents, handleError);
});
async function writeFile(fileName, contents) {
await fs.promises.mkdir(path.dirname(fileName), { recursive: true });
await fs.promises.writeFile(fileName, contents);
}

/**
* @param {Record<string, string>} o
*/
function objectToList(o) {
const list = [];
for (const key in o) {
list.push({ key, value: o[key] });
}
return list;
}

function generateLCGFile() {
return fs.readFile(diagnosticsMapFilePath, (err, data) => {
handleError(err);
writeFile(
path.join(outputPath, "enu", "diagnosticMessages.generated.json.lcg"),
getLCGFileXML(
objectToList(JSON.parse(data.toString()))
.sort((a, b) => a.key > b.key ? 1 : -1) // lcg sorted by property keys
.reduce((s, { key, value }) => s + getItemXML(key, value), "")
));
});
async function generateLCGFile() {
const contents = await fs.promises.readFile(diagnosticsMapFilePath, "utf-8");
await writeFile(
path.join(outputPath, "enu", "diagnosticMessages.generated.json.lcg"),
getLCGFileXML(
Object.entries(JSON.parse(contents))
.sort((a, b) => a[0] > b[0] ? 1 : -1) // lcg sorted by property keys
.reduce((s, [key, value]) => s + getItemXML(key, value), "")
),
);
return;

/**
* @param {string} key
Expand Down

0 comments on commit 1d9a16b

Please sign in to comment.