Skip to content

Commit c7de568

Browse files
committed
Switch to faster XML parsing library for localize
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.
1 parent c1c022f commit c7de568

File tree

3 files changed

+74
-89
lines changed

3 files changed

+74
-89
lines changed

package-lock.json

Lines changed: 39 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"@types/node": "latest",
5252
"@types/source-map-support": "latest",
5353
"@types/which": "^2.0.1",
54-
"@types/xml2js": "^0.4.11",
5554
"@typescript-eslint/eslint-plugin": "^5.33.1",
5655
"@typescript-eslint/parser": "^5.33.1",
5756
"@typescript-eslint/utils": "^5.33.1",
@@ -67,10 +66,11 @@
6766
"eslint-plugin-jsdoc": "^39.3.6",
6867
"eslint-plugin-local": "^1.0.0",
6968
"eslint-plugin-no-null": "^1.0.2",
69+
"fast-xml-parser": "^4.0.11",
7070
"fs-extra": "^9.1.0",
7171
"glob": "latest",
72-
"jsonc-parser": "^3.2.0",
7372
"hereby": "^1.5.0",
73+
"jsonc-parser": "^3.2.0",
7474
"minimist": "latest",
7575
"mkdirp": "latest",
7676
"mocha": "latest",
@@ -79,8 +79,7 @@
7979
"node-fetch": "^3.2.10",
8080
"source-map-support": "latest",
8181
"typescript": "^4.8.4",
82-
"which": "^2.0.2",
83-
"xml2js": "^0.4.23"
82+
"which": "^2.0.2"
8483
},
8584
"scripts": {
8685
"test": "hereby runtests-parallel --light=false",

scripts/generateLocalizedDiagnosticMessages.mjs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import fs from "fs";
22
import path from "path";
3-
import xml2js from "xml2js";
4-
import util from "util";
5-
6-
const parseString = util.promisify(xml2js.parseString);
3+
import { XMLParser } from "fast-xml-parser"
4+
5+
/** @typedef {{
6+
LCX: {
7+
$_TgtCul: string;
8+
Item: {
9+
Item: {
10+
Item: {
11+
$_ItemId: string;
12+
Str: {
13+
Val: string;
14+
Tgt: {
15+
Val: string;
16+
};
17+
};
18+
}[];
19+
};
20+
};
21+
}
22+
}} ParsedLCL */
23+
void 0;
724

825
async function main() {
926
const args = process.argv.slice(2);
@@ -31,15 +48,16 @@ async function main() {
3148
*/
3249
async function visitDirectory(name) {
3350
const inputFilePath = path.join(inputPath, name, "diagnosticMessages", "diagnosticMessages.generated.json.lcl");
34-
const contents = await fs.promises.readFile(inputFilePath, "utf-8");
35-
const result = await parseString(contents);
36-
if (!result || !result.LCX || !result.LCX.$ || !result.LCX.$.TgtCul) {
37-
console.error("Unexpected XML file structure. Expected to find result.LCX.$.TgtCul.");
51+
const contents = await fs.promises.readFile(inputFilePath);
52+
/** @type {ParsedLCL} */
53+
const result = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "$_"}).parse(contents);
54+
if (!result || !result.LCX || !result.LCX.$_TgtCul) {
55+
console.error("Unexpected XML file structure. Expected to find result.LCX.$_TgtCul.");
3856
process.exit(1);
3957
}
40-
const outputDirectoryName = getPreferredLocaleName(result.LCX.$.TgtCul).toLowerCase();
58+
const outputDirectoryName = getPreferredLocaleName(result.LCX.$_TgtCul).toLowerCase();
4159
if (!outputDirectoryName) {
42-
console.error(`Invalid output locale name for '${result.LCX.$.TgtCul}'.`);
60+
console.error(`Invalid output locale name for '${result.LCX.$_TgtCul}'.`);
4361
process.exit(1);
4462
}
4563
await writeFile(path.join(outputPath, outputDirectoryName, "diagnosticMessages.generated.json"), xmlObjectToString(result));
@@ -77,14 +95,14 @@ async function main() {
7795
}
7896

7997
/**
80-
* @param {any} o
98+
* @param {ParsedLCL} o
8199
*/
82100
function xmlObjectToString(o) {
83101
/** @type {any} */
84102
const out = {};
85-
for (const item of o.LCX.Item[0].Item[0].Item) {
86-
let ItemId = item.$.ItemId;
87-
let val = item.Str[0].Tgt ? item.Str[0].Tgt[0].Val[0] : item.Str[0].Val[0];
103+
for (const item of o.LCX.Item.Item.Item) {
104+
let ItemId = item.$_ItemId;
105+
let val = item.Str.Tgt ? item.Str.Tgt.Val : item.Str.Val;
88106

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

0 commit comments

Comments
 (0)