@@ -68,20 +68,37 @@ async function readStdin() {
68
68
}
69
69
70
70
/**
71
- *
72
- * @param {string } file
73
- * @returns void
74
- */
75
- function printIncorrectlyFormattedFile ( file ) {
76
- console . error ( "[format check]" , file ) ;
77
- }
78
-
79
- /**
80
- *
81
- * @param {number } incorrectlyFormattedFiles
82
- * @returns void
71
+ * @param {string[] } files
72
+ * @param {string } bsc_exe
73
+ * @param {(x: string) => boolean } isSupportedFile
74
+ * @param {boolean } checkFormatting
83
75
*/
84
- function printFormatCheckResult ( incorrectlyFormattedFiles ) {
76
+ async function formatFiles ( files , bsc_exe , isSupportedFile , checkFormatting ) {
77
+ var incorrectlyFormattedFiles = 0 ;
78
+ try {
79
+ const _promises = await Promise . all (
80
+ files . map ( async file => {
81
+ if ( isSupportedFile ( file ) ) {
82
+ // console.log(`processing ${arg}`);
83
+ const flags = checkFormatting
84
+ ? [ "-format" , file ]
85
+ : [ "-o" , file , "-format" , file ] ;
86
+ const { stdout } = await asyncExecFile ( bsc_exe , flags ) ;
87
+ if ( check . val ) {
88
+ const original = await asyncFs . readFile ( file , "utf-8" ) ;
89
+ if ( original != stdout ) {
90
+ console . error ( "[format check]" , file ) ;
91
+ incorrectlyFormattedFiles ++ ;
92
+ }
93
+ }
94
+ }
95
+ return null ;
96
+ } )
97
+ ) ;
98
+ } catch ( err ) {
99
+ console . error ( err ) ;
100
+ process . exit ( 2 ) ;
101
+ }
85
102
if ( incorrectlyFormattedFiles > 0 ) {
86
103
console . error (
87
104
`${ incorrectlyFormattedFiles } file${
@@ -134,35 +151,7 @@ async function main(argv, rescript_exe, bsc_exe) {
134
151
process . exit ( 2 ) ;
135
152
}
136
153
files = output . stdout . split ( "\n" ) . map ( x => x . trim ( ) ) ;
137
- var incorrectlyFormattedFiles = 0 ;
138
- // it's ok that this processing is asynchronous
139
- // given we only write one-liners during it
140
- // so stdout should not get mixed (or can oneliners actually be mixed too?)
141
- try {
142
- const _promises = await Promise . all (
143
- files . map ( async file => {
144
- if ( isSupportedFile ( file ) ) {
145
- // console.log(`processing ${arg}`);
146
- const flags = check . val
147
- ? [ "-format" , file ]
148
- : [ "-o" , file , "-format" , file ] ;
149
- const { stdout } = await asyncExecFile ( bsc_exe , flags ) ;
150
- if ( check . val ) {
151
- const original = await asyncFs . readFile ( file , "utf-8" ) ;
152
- if ( original != stdout ) {
153
- printIncorrectlyFormattedFile ( file ) ;
154
- incorrectlyFormattedFiles ++ ;
155
- }
156
- }
157
- }
158
- return null ;
159
- } )
160
- ) ;
161
- } catch ( err ) {
162
- console . error ( err ) ;
163
- process . exit ( 2 ) ;
164
- }
165
- printFormatCheckResult ( incorrectlyFormattedFiles ) ;
154
+ await formatFiles ( files , bsc_exe , isSupportedFile , check . val ) ;
166
155
} else if ( use_stdin ) {
167
156
if ( check . val ) {
168
157
console . error ( "format -stdin cannot be used with -check flag" ) ;
@@ -216,39 +205,7 @@ async function main(argv, rescript_exe, bsc_exe) {
216
205
process . exit ( 2 ) ;
217
206
}
218
207
}
219
- var hasError = false ;
220
- var incorrectlyFormattedFiles = 0 ;
221
- files . forEach ( file => {
222
- var write = isSupportedFile ( file ) && ! check . val ;
223
- var flags = write ? [ "-o" , file , "-format" , file ] : [ "-format" , file ] ;
224
- // we make this processing synchronous in order not to mix stdout
225
- try {
226
- const formatted = child_process . execFileSync ( bsc_exe , flags ) ;
227
- if ( ! write ) {
228
- if ( check . val ) {
229
- try {
230
- const original = fs . readFileSync ( file , "utf-8" ) ;
231
- if ( original != formatted ) {
232
- printIncorrectlyFormattedFile ( file ) ;
233
- incorrectlyFormattedFiles ++ ;
234
- }
235
- } catch ( err ) {
236
- console . error ( err ) ;
237
- hasError = true ;
238
- }
239
- } else {
240
- process . stdout . write ( formatted ) ;
241
- }
242
- }
243
- } catch ( err ) {
244
- console . error ( err ) ;
245
- hasError = true ;
246
- }
247
- } ) ;
248
- printFormatCheckResult ( incorrectlyFormattedFiles ) ;
249
- if ( hasError ) {
250
- process . exit ( 2 ) ;
251
- }
208
+ await formatFiles ( files , bsc_exe , isSupportedFile , check . val ) ;
252
209
}
253
210
} catch ( e ) {
254
211
if ( e instanceof arg . ArgError ) {
0 commit comments