@@ -7,14 +7,18 @@ const fs = require('fs');
7
7
const tty = require ( 'tty' ) ;
8
8
const { version } = require ( '../package.json' ) ;
9
9
const { ArgumentParser } = require ( 'argparse' ) ;
10
+ const { promisify } = require ( 'util' ) ;
11
+ const getStdin = require ( 'get-stdin' ) ;
10
12
11
- class PrettierSQLArgs {
13
+ class SqlFormatterCli {
12
14
constructor ( ) {
13
15
this . parser = this . getParser ( ) ;
14
16
this . args = this . parser . parse_args ( ) ;
15
- this . cfg = this . readConfig ( ) ;
17
+ }
16
18
17
- this . query = this . getInput ( ) ;
19
+ async run ( ) {
20
+ this . cfg = await this . readConfig ( ) ;
21
+ this . query = await this . getInput ( ) ;
18
22
const formattedQuery = format ( this . query , this . cfg ) . trim ( ) + '\n' ;
19
23
this . writeOutput ( this . getOutputFile ( this . args ) , formattedQuery ) ;
20
24
}
@@ -59,7 +63,7 @@ class PrettierSQLArgs {
59
63
return parser ;
60
64
}
61
65
62
- readConfig ( ) {
66
+ async readConfig ( ) {
63
67
if (
64
68
tty . isatty ( 0 ) &&
65
69
Object . entries ( this . args ) . every ( ( [ k , v ] ) => k === 'language' || v === undefined )
@@ -68,45 +72,55 @@ class PrettierSQLArgs {
68
72
process . exit ( 0 ) ;
69
73
}
70
74
71
- if ( this . args . config )
75
+ if ( this . args . config ) {
72
76
try {
73
- const configFile = fs . readFileSync ( this . args . config ) ;
77
+ const configFile = await this . readFile ( this . args . config ) ;
74
78
const configJson = JSON . parse ( configFile ) ;
75
79
return { language : this . args . language , ...configJson } ;
76
80
} catch ( e ) {
77
81
if ( e instanceof SyntaxError ) {
78
82
console . error ( `Error: unable to parse JSON at file ${ this . args . config } ` ) ;
79
83
process . exit ( 1 ) ;
80
84
}
81
- if ( e . code === 'ENOENT' ) {
82
- console . error ( `Error: could not open file ${ this . args . config } ` ) ;
83
- process . exit ( 1 ) ;
84
- }
85
+ this . exitWhenIOError ( e ) ;
85
86
console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
86
87
console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
87
88
throw e ;
88
89
}
90
+ }
89
91
return {
90
92
language : this . args . language ,
91
93
} ;
92
94
}
93
95
94
- getInput ( ) {
96
+ async getInput ( ) {
95
97
const infile = this . args . file || process . stdin . fd ;
96
- try {
97
- return fs . readFileSync ( infile , 'utf-8' ) ;
98
- } catch ( e ) {
99
- if ( e . code === 'EAGAIN' ) {
100
- console . error ( 'Error: no file specified and no data in stdin' ) ;
101
- process . exit ( 1 ) ;
102
- }
103
- if ( e . code === 'ENOENT' ) {
104
- console . error ( `Error: could not open file ${ infile } ` ) ;
105
- process . exit ( 1 ) ;
98
+ if ( this . args . file ) {
99
+ try {
100
+ return await this . readFile ( infile , { encoding : 'utf-8' } ) ;
101
+ } catch ( e ) {
102
+ this . exitWhenIOError ( e ) ;
103
+ console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
104
+ console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
105
+ throw e ;
106
106
}
107
- console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
108
- console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
109
- throw e ;
107
+ } else {
108
+ return await getStdin ( ) ;
109
+ }
110
+ }
111
+
112
+ async readFile ( filename ) {
113
+ return promisify ( fs . readFile ) ( filename , { encoding : 'utf-8' } ) ;
114
+ }
115
+
116
+ exitWhenIOError ( e ) {
117
+ if ( e . code === 'EAGAIN' ) {
118
+ console . error ( 'Error: no file specified and no data in stdin' ) ;
119
+ process . exit ( 1 ) ;
120
+ }
121
+ if ( e . code === 'ENOENT' ) {
122
+ console . error ( `Error: could not open file ${ infile } ` ) ;
123
+ process . exit ( 1 ) ;
110
124
}
111
125
}
112
126
@@ -136,4 +150,5 @@ class PrettierSQLArgs {
136
150
}
137
151
}
138
152
139
- new PrettierSQLArgs ( ) ;
153
+ const cli = new SqlFormatterCli ( ) ;
154
+ cli . run ( ) ;
0 commit comments