|
1 | 1 | import fs from "fs";
|
2 | 2 | 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; |
7 | 24 |
|
8 | 25 | async function main() {
|
9 | 26 | const args = process.argv.slice(2);
|
@@ -31,15 +48,17 @@ async function main() {
|
31 | 48 | */
|
32 | 49 | async function visitDirectory(name) {
|
33 | 50 | 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."); |
38 | 57 | process.exit(1);
|
39 | 58 | }
|
40 |
| - const outputDirectoryName = getPreferredLocaleName(result.LCX.$.TgtCul).toLowerCase(); |
| 59 | + const outputDirectoryName = getPreferredLocaleName(result.LCX.$_TgtCul).toLowerCase(); |
41 | 60 | if (!outputDirectoryName) {
|
42 |
| - console.error(`Invalid output locale name for '${result.LCX.$.TgtCul}'.`); |
| 61 | + console.error(`Invalid output locale name for '${result.LCX.$_TgtCul}'.`); |
43 | 62 | process.exit(1);
|
44 | 63 | }
|
45 | 64 | await writeFile(path.join(outputPath, outputDirectoryName, "diagnosticMessages.generated.json"), xmlObjectToString(result));
|
@@ -77,14 +96,14 @@ async function main() {
|
77 | 96 | }
|
78 | 97 |
|
79 | 98 | /**
|
80 |
| - * @param {any} o |
| 99 | + * @param {ParsedLCL} o |
81 | 100 | */
|
82 | 101 | function xmlObjectToString(o) {
|
83 | 102 | /** @type {any} */
|
84 | 103 | 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; |
88 | 107 |
|
89 | 108 | if (typeof ItemId !== "string" || typeof val !== "string") {
|
90 | 109 | console.error("Unexpected XML file structure");
|
|
0 commit comments