11import fs from "fs" ;
22import path from "path" ;
33import xml2js from "xml2js" ;
4+ import util from "util" ;
45
5- function main ( ) {
6+ const parseString = util . promisify ( xml2js . parseString ) ;
7+
8+ async function main ( ) {
69 const args = process . argv . slice ( 2 ) ;
710 if ( args . length !== 3 ) {
811 console . log ( "Usage:" ) ;
@@ -18,35 +21,28 @@ function main() {
1821 generateLCGFile ( ) ;
1922
2023 // generate other langs
21- fs . readdir ( inputPath , ( err , files ) => {
22- handleError ( err ) ;
23- files . forEach ( visitDirectory ) ;
24- } ) ;
24+ const files = await fs . promises . readdir ( inputPath ) ;
25+ await Promise . all ( files . map ( visitDirectory ) ) ;
2526
2627 return ;
2728
2829 /**
2930 * @param {string } name
3031 */
31- function visitDirectory ( name ) {
32+ async function visitDirectory ( name ) {
3233 const inputFilePath = path . join ( inputPath , name , "diagnosticMessages" , "diagnosticMessages.generated.json.lcl" ) ;
33-
34- fs . readFile ( inputFilePath , ( err , data ) => {
35- handleError ( err ) ;
36- xml2js . parseString ( data . toString ( ) , ( err , result ) => {
37- handleError ( err ) ;
38- if ( ! result || ! result . LCX || ! result . LCX . $ || ! result . LCX . $ . TgtCul ) {
39- console . error ( "Unexpected XML file structure. Expected to find result.LCX.$.TgtCul." ) ;
40- process . exit ( 1 ) ;
41- }
42- const outputDirectoryName = getPreferredLocaleName ( result . LCX . $ . TgtCul ) . toLowerCase ( ) ;
43- if ( ! outputDirectoryName ) {
44- console . error ( `Invalid output locale name for '${ result . LCX . $ . TgtCul } '.` ) ;
45- process . exit ( 1 ) ;
46- }
47- writeFile ( path . join ( outputPath , outputDirectoryName , "diagnosticMessages.generated.json" ) , xmlObjectToString ( result ) ) ;
48- } ) ;
49- } ) ;
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." ) ;
38+ process . exit ( 1 ) ;
39+ }
40+ const outputDirectoryName = getPreferredLocaleName ( result . LCX . $ . TgtCul ) . toLowerCase ( ) ;
41+ if ( ! outputDirectoryName ) {
42+ console . error ( `Invalid output locale name for '${ result . LCX . $ . TgtCul } '.` ) ;
43+ process . exit ( 1 ) ;
44+ }
45+ await writeFile ( path . join ( outputPath , outputDirectoryName , "diagnosticMessages.generated.json" ) , xmlObjectToString ( result ) ) ;
5046 }
5147
5248 /**
@@ -80,16 +76,6 @@ function main() {
8076 }
8177 }
8278
83- /**
84- * @param {null | object } err
85- */
86- function handleError ( err ) {
87- if ( err ) {
88- console . error ( err ) ;
89- process . exit ( 1 ) ;
90- }
91- }
92-
9379 /**
9480 * @param {any } o
9581 */
@@ -115,55 +101,26 @@ function main() {
115101 return JSON . stringify ( out , undefined , 2 ) ;
116102 }
117103
118-
119- /**
120- * @param {string } directoryPath
121- * @param {() => void } action
122- */
123- function ensureDirectoryExists ( directoryPath , action ) {
124- fs . exists ( directoryPath , exists => {
125- if ( ! exists ) {
126- const basePath = path . dirname ( directoryPath ) ;
127- if ( basePath !== directoryPath ) {
128- return ensureDirectoryExists ( basePath , ( ) => fs . mkdir ( directoryPath , action ) ) ;
129- }
130- }
131- action ( ) ;
132- } ) ;
133- }
134-
135104 /**
136105 * @param {string } fileName
137106 * @param {string } contents
138107 */
139- function writeFile ( fileName , contents ) {
140- ensureDirectoryExists ( path . dirname ( fileName ) , ( ) => {
141- fs . writeFile ( fileName , contents , handleError ) ;
142- } ) ;
108+ async function writeFile ( fileName , contents ) {
109+ await fs . promises . mkdir ( path . dirname ( fileName ) , { recursive : true } ) ;
110+ await fs . promises . writeFile ( fileName , contents ) ;
143111 }
144112
145- /**
146- * @param {Record<string, string> } o
147- */
148- function objectToList ( o ) {
149- const list = [ ] ;
150- for ( const key in o ) {
151- list . push ( { key, value : o [ key ] } ) ;
152- }
153- return list ;
154- }
155-
156- function generateLCGFile ( ) {
157- return fs . readFile ( diagnosticsMapFilePath , ( err , data ) => {
158- handleError ( err ) ;
159- writeFile (
160- path . join ( outputPath , "enu" , "diagnosticMessages.generated.json.lcg" ) ,
161- getLCGFileXML (
162- objectToList ( JSON . parse ( data . toString ( ) ) )
163- . sort ( ( a , b ) => a . key > b . key ? 1 : - 1 ) // lcg sorted by property keys
164- . reduce ( ( s , { key, value } ) => s + getItemXML ( key , value ) , "" )
165- ) ) ;
166- } ) ;
113+ async function generateLCGFile ( ) {
114+ const contents = await fs . promises . readFile ( diagnosticsMapFilePath , "utf-8" ) ;
115+ await writeFile (
116+ path . join ( outputPath , "enu" , "diagnosticMessages.generated.json.lcg" ) ,
117+ getLCGFileXML (
118+ Object . entries ( JSON . parse ( contents ) )
119+ . sort ( ( a , b ) => a [ 0 ] > b [ 0 ] ? 1 : - 1 ) // lcg sorted by property keys
120+ . reduce ( ( s , [ key , value ] ) => s + getItemXML ( key , value ) , "" )
121+ ) ,
122+ ) ;
123+ return ;
167124
168125 /**
169126 * @param {string } key
0 commit comments