@@ -33,7 +33,20 @@ in the stdout (in rescript syntax)`,
33
33
"Formatting the whole project " ,
34
34
] ,
35
35
] ;
36
- var formattedExtensions = [ ".res" , ".resi" , ".ml" , ".mli" , ".re" , ".rei" ] ;
36
+ var formattedStdExtensions = [ ".res" , ".resi" , ".ml" , ".mli" , ".re" , ".rei" ] ;
37
+ var formattedFileExtensions = [ ".res" , ".resi" ] ;
38
+ var convertedExtensions = [ ".ml" , ".mli" , ".re" , ".rei" ] ;
39
+ /**
40
+ *
41
+ * @param {string[] } extensions
42
+ */
43
+ function hasExtension ( extensions ) {
44
+ /**
45
+ * @param {string } x
46
+ */
47
+ var pred = ( x ) => extensions . some ( ( ext ) => x . endsWith ( ext ) ) ;
48
+ return pred ;
49
+ }
37
50
async function readStdin ( ) {
38
51
var stream = process . stdin ;
39
52
const chunks = [ ] ;
@@ -47,6 +60,9 @@ async function readStdin() {
47
60
* @param {string } bsc_exe
48
61
*/
49
62
function main ( argv , bsb_exe , bsc_exe ) {
63
+ var isSupportedFile = hasExtension ( formattedFileExtensions ) ;
64
+ var isSupportedStd = hasExtension ( formattedStdExtensions ) ;
65
+ var isSupportedConvert = hasExtension ( convertedExtensions ) ;
50
66
try {
51
67
/**
52
68
* @type {string[] }
@@ -75,7 +91,7 @@ function main(argv, bsb_exe, bsc_exe) {
75
91
}
76
92
files = output . stdout . split ( "\n" ) . map ( ( x ) => x . trim ( ) ) ;
77
93
for ( let arg of files ) {
78
- if ( arg . endsWith ( ".res" ) || arg . endsWith ( ".resi" ) ) {
94
+ if ( isSupportedFile ( arg ) ) {
79
95
// console.log(`processing ${arg}`);
80
96
child_process . execFile (
81
97
bsc_exe ,
@@ -92,7 +108,7 @@ function main(argv, bsb_exe, bsc_exe) {
92
108
}
93
109
}
94
110
} else if ( use_stdin ) {
95
- if ( formattedExtensions . some ( ( x ) => use_stdin . endsWith ( x ) ) ) {
111
+ if ( isSupportedStd ( use_stdin ) ) {
96
112
var crypto = require ( "crypto" ) ;
97
113
var os = require ( "os" ) ;
98
114
var filename = path . join (
@@ -107,7 +123,7 @@ function main(argv, bsb_exe, bsc_exe) {
107
123
[ "-format" , filename ] ,
108
124
( error , stdout , stderr ) => {
109
125
if ( error === null ) {
110
- console . log ( stdout ) ;
126
+ console . log ( stdout . trimEnd ( ) ) ;
111
127
} else {
112
128
console . log ( stderr ) ;
113
129
process . exit ( 2 ) ;
@@ -117,21 +133,27 @@ function main(argv, bsb_exe, bsc_exe) {
117
133
} ) ( ) ;
118
134
} else {
119
135
console . error ( `Unsupported exetnsion ${ use_stdin } ` ) ;
120
- console . error ( `Supported extensions: ${ formattedExtensions } ` ) ;
136
+ console . error ( `Supported extensions: ${ formattedStdExtensions } ` ) ;
121
137
process . exit ( 2 ) ;
122
138
}
123
139
} else {
124
140
if ( files . length === 0 ) {
125
141
// none of argumets set
126
142
// format the current directory
127
- files = fs
128
- . readdirSync ( process . cwd ( ) )
129
- . filter ( ( x ) => x . endsWith ( ".res" ) || x . endsWith ( ".resi" ) ) ;
143
+ files = fs . readdirSync ( process . cwd ( ) ) . filter ( isSupportedFile ) ;
130
144
}
145
+
131
146
for ( let i = 0 ; i < files . length ; ++ i ) {
132
147
let file = files [ i ] ;
133
- if ( ! ( file . endsWith ( ".res" ) || file . endsWith ( ".resi" ) ) ) {
134
- console . error ( `don't know what do with ${ file } ` ) ;
148
+ if ( ! isSupportedFile ( file ) ) {
149
+ if ( isSupportedConvert ( file ) ) {
150
+ console . error (
151
+ `You need convert subcommand to handle such file extensions`
152
+ ) ;
153
+ } else {
154
+ console . error ( `Don't know what do with ${ file } ` ) ;
155
+ }
156
+ console . error ( `Supported extensions: ${ formattedFileExtensions } ` )
135
157
process . exit ( 2 ) ;
136
158
}
137
159
}
0 commit comments