Skip to content

Commit

Permalink
Switch to faster XML parsing library for localize
Browse files Browse the repository at this point in the history
Switching to a newer/faster/maintained library speeds up this script by
more than 50%. We can also eliminate some any and such in the process.
  • Loading branch information
jakebailey committed Oct 14, 2022
1 parent 3ddd622 commit 6f8306c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 89 deletions.
110 changes: 39 additions & 71 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"@types/node": "latest",
"@types/source-map-support": "latest",
"@types/which": "^2.0.1",
"@types/xml2js": "^0.4.11",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"@typescript-eslint/utils": "^5.33.1",
Expand All @@ -67,10 +66,11 @@
"eslint-plugin-jsdoc": "^39.3.6",
"eslint-plugin-local": "^1.0.0",
"eslint-plugin-no-null": "^1.0.2",
"fast-xml-parser": "^4.0.11",
"fs-extra": "^9.1.0",
"glob": "latest",
"jsonc-parser": "^3.2.0",
"hereby": "^1.5.0",
"jsonc-parser": "^3.2.0",
"minimist": "latest",
"mkdirp": "latest",
"mocha": "latest",
Expand All @@ -79,8 +79,7 @@
"node-fetch": "^3.2.10",
"source-map-support": "latest",
"typescript": "^4.8.4",
"which": "^2.0.2",
"xml2js": "^0.4.23"
"which": "^2.0.2"
},
"scripts": {
"test": "hereby runtests-parallel --light=false",
Expand Down
47 changes: 33 additions & 14 deletions scripts/generateLocalizedDiagnosticMessages.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import fs from "fs";
import path from "path";
import xml2js from "xml2js";
import util from "util";

const parseString = util.promisify(xml2js.parseString);
import { XMLParser } from "fast-xml-parser";

/** @typedef {{
LCX: {
$_TgtCul: string;
Item: {
Item: {
Item: {
$_ItemId: string;
Str: {
Val: string;
Tgt: {
Val: string;
};
};
}[];
};
};
}
}} ParsedLCL */
void 0;

async function main() {
const args = process.argv.slice(2);
Expand Down Expand Up @@ -31,15 +48,17 @@ async function main() {
*/
async function visitDirectory(name) {
const inputFilePath = path.join(inputPath, name, "diagnosticMessages", "diagnosticMessages.generated.json.lcl");
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.");
const contents = await fs.promises.readFile(inputFilePath);
/** @type {ParsedLCL} */
// eslint-disable-next-line local/object-literal-surrounding-space
const result = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "$_"}).parse(contents);
if (!result || !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();
const outputDirectoryName = getPreferredLocaleName(result.LCX.$_TgtCul).toLowerCase();
if (!outputDirectoryName) {
console.error(`Invalid output locale name for '${result.LCX.$.TgtCul}'.`);
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 @@ -77,14 +96,14 @@ async function main() {
}

/**
* @param {any} o
* @param {ParsedLCL} o
*/
function xmlObjectToString(o) {
/** @type {any} */
const out = {};
for (const item of o.LCX.Item[0].Item[0].Item) {
let ItemId = item.$.ItemId;
let val = item.Str[0].Tgt ? item.Str[0].Tgt[0].Val[0] : item.Str[0].Val[0];
for (const item of o.LCX.Item.Item.Item) {
let ItemId = item.$_ItemId;
let val = item.Str.Tgt ? item.Str.Tgt.Val : item.Str.Val;

if (typeof ItemId !== "string" || typeof val !== "string") {
console.error("Unexpected XML file structure");
Expand Down

0 comments on commit 6f8306c

Please sign in to comment.