1
1
import fs from "fs" ;
2
2
import path from "path" ;
3
3
import xml2js from "xml2js" ;
4
+ import util from "util" ;
4
5
5
- function main ( ) {
6
+ const parseString = util . promisify ( xml2js . parseString ) ;
7
+
8
+ async function main ( ) {
6
9
const args = process . argv . slice ( 2 ) ;
7
10
if ( args . length !== 3 ) {
8
11
console . log ( "Usage:" ) ;
@@ -18,35 +21,28 @@ function main() {
18
21
generateLCGFile ( ) ;
19
22
20
23
// 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 ) ) ;
25
26
26
27
return ;
27
28
28
29
/**
29
30
* @param {string } name
30
31
*/
31
- function visitDirectory ( name ) {
32
+ async function visitDirectory ( name ) {
32
33
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 ) ) ;
50
46
}
51
47
52
48
/**
@@ -80,16 +76,6 @@ function main() {
80
76
}
81
77
}
82
78
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
-
93
79
/**
94
80
* @param {any } o
95
81
*/
@@ -115,55 +101,26 @@ function main() {
115
101
return JSON . stringify ( out , undefined , 2 ) ;
116
102
}
117
103
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
-
135
104
/**
136
105
* @param {string } fileName
137
106
* @param {string } contents
138
107
*/
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 ) ;
143
111
}
144
112
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 ;
167
124
168
125
/**
169
126
* @param {string } key
0 commit comments