Skip to content

Commit 21bb51f

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 1d9a16b commit 21bb51f

File tree

3 files changed

+75
-89
lines changed

3 files changed

+75
-89
lines changed

Diff for: package-lock.json

+39-71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-4
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",

Diff for: scripts/generateLocalizedDiagnosticMessages.mjs

+33-14
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,17 @@ 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+
// eslint-disable-next-line local/object-literal-surrounding-space
54+
const result = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "$_"}).parse(contents);
55+
if (!result || !result.LCX || !result.LCX.$_TgtCul) {
56+
console.error("Unexpected XML file structure. Expected to find result.LCX.$_TgtCul.");
3857
process.exit(1);
3958
}
40-
const outputDirectoryName = getPreferredLocaleName(result.LCX.$.TgtCul).toLowerCase();
59+
const outputDirectoryName = getPreferredLocaleName(result.LCX.$_TgtCul).toLowerCase();
4160
if (!outputDirectoryName) {
42-
console.error(`Invalid output locale name for '${result.LCX.$.TgtCul}'.`);
61+
console.error(`Invalid output locale name for '${result.LCX.$_TgtCul}'.`);
4362
process.exit(1);
4463
}
4564
await writeFile(path.join(outputPath, outputDirectoryName, "diagnosticMessages.generated.json"), xmlObjectToString(result));
@@ -77,14 +96,14 @@ async function main() {
7796
}
7897

7998
/**
80-
* @param {any} o
99+
* @param {ParsedLCL} o
81100
*/
82101
function xmlObjectToString(o) {
83102
/** @type {any} */
84103
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];
104+
for (const item of o.LCX.Item.Item.Item) {
105+
let ItemId = item.$_ItemId;
106+
let val = item.Str.Tgt ? item.Str.Tgt.Val : item.Str.Val;
88107

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

0 commit comments

Comments
 (0)