@@ -7,14 +7,18 @@ const fs = require('fs');
77const tty = require ( 'tty' ) ;
88const { version } = require ( '../package.json' ) ;
99const { ArgumentParser } = require ( 'argparse' ) ;
10+ const { promisify } = require ( 'util' ) ;
11+ const getStdin = require ( 'get-stdin' ) ;
1012
11- class PrettierSQLArgs {
13+ class SqlFormatterCli {
1214 constructor ( ) {
1315 this . parser = this . getParser ( ) ;
1416 this . args = this . parser . parse_args ( ) ;
15- this . cfg = this . readConfig ( ) ;
17+ }
1618
17- this . query = this . getInput ( ) ;
19+ async run ( ) {
20+ this . cfg = await this . readConfig ( ) ;
21+ this . query = await this . getInput ( ) ;
1822 const formattedQuery = format ( this . query , this . cfg ) . trim ( ) + '\n' ;
1923 this . writeOutput ( this . getOutputFile ( this . args ) , formattedQuery ) ;
2024 }
@@ -59,7 +63,7 @@ class PrettierSQLArgs {
5963 return parser ;
6064 }
6165
62- readConfig ( ) {
66+ async readConfig ( ) {
6367 if (
6468 tty . isatty ( 0 ) &&
6569 Object . entries ( this . args ) . every ( ( [ k , v ] ) => k === 'language' || v === undefined )
@@ -68,45 +72,55 @@ class PrettierSQLArgs {
6872 process . exit ( 0 ) ;
6973 }
7074
71- if ( this . args . config )
75+ if ( this . args . config ) {
7276 try {
73- const configFile = fs . readFileSync ( this . args . config ) ;
77+ const configFile = await this . readFile ( this . args . config ) ;
7478 const configJson = JSON . parse ( configFile ) ;
7579 return { language : this . args . language , ...configJson } ;
7680 } catch ( e ) {
7781 if ( e instanceof SyntaxError ) {
7882 console . error ( `Error: unable to parse JSON at file ${ this . args . config } ` ) ;
7983 process . exit ( 1 ) ;
8084 }
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 ) ;
8586 console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
8687 console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
8788 throw e ;
8889 }
90+ }
8991 return {
9092 language : this . args . language ,
9193 } ;
9294 }
9395
94- getInput ( ) {
96+ async getInput ( ) {
9597 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 ;
106106 }
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 ) ;
110124 }
111125 }
112126
@@ -136,4 +150,5 @@ class PrettierSQLArgs {
136150 }
137151}
138152
139- new PrettierSQLArgs ( ) ;
153+ const cli = new SqlFormatterCli ( ) ;
154+ cli . run ( ) ;
0 commit comments