4
4
5
5
const { format, supportedDialects } = require ( '../dist/index.cjs' ) ;
6
6
const fs = require ( 'fs' ) ;
7
+ const path = require ( 'path' ) ;
7
8
const tty = require ( 'tty' ) ;
8
9
const { version } = require ( '../package.json' ) ;
9
10
const { ArgumentParser } = require ( 'argparse' ) ;
@@ -52,7 +53,7 @@ class SqlFormatterCli {
52
53
} ) ;
53
54
54
55
parser . add_argument ( '-c' , '--config' , {
55
- help : 'Path to config JSON file or json string (will use default configs if unspecified)' ,
56
+ help : 'Path to config JSON file or json string (will find a file named \'.sql-formatter.json\' or use default configs if unspecified)' ,
56
57
} ) ;
57
58
58
59
parser . add_argument ( '--version' , {
@@ -72,57 +73,75 @@ class SqlFormatterCli {
72
73
process . exit ( 0 ) ;
73
74
}
74
75
76
+ return {
77
+ language : this . args . language ,
78
+ ...( await this . getConfig ( ) ) ,
79
+ } ;
80
+ }
81
+
82
+ async getConfig ( ) {
75
83
if ( this . args . config ) {
76
84
// First, try to parse --config value as a JSON string
77
85
try {
78
- const configJson = JSON . parse ( this . args . config ) ;
79
- return { language : this . args . language , ...configJson } ;
86
+ return JSON . parse ( this . args . config ) ;
80
87
} catch ( e ) {
81
88
// If that fails, try to read the --config value as a file
82
- try {
83
- const configFile = await this . readFile ( this . args . config ) ;
84
- const configJson = JSON . parse ( configFile ) ;
85
- return { language : this . args . language , ...configJson } ;
86
- } catch ( e ) {
87
- if ( e instanceof SyntaxError ) {
88
- console . error (
89
- `Error: unable to parse as JSON or treat as JSON file: ${ this . args . config } `
90
- ) ;
91
- process . exit ( 1 ) ;
92
- }
93
- this . exitWhenIOError ( e ) ;
94
- console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
95
- console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
96
- throw e ;
97
- }
89
+ return this . parseFile ( this . args . config ) ;
98
90
}
99
91
}
100
- return {
101
- language : this . args . language ,
102
- } ;
92
+
93
+ // Otherwise find a local config file
94
+ const localConfig = await this . findConfig ( ) ;
95
+ if ( ! localConfig ) {
96
+ return null ;
97
+ }
98
+
99
+ return this . parseFile ( localConfig ) ;
100
+ }
101
+
102
+ findConfig ( dir = __dirname ) {
103
+ const filePath = path . join ( dir , '.sql-formatter.json' ) ;
104
+ if ( ! fs . existsSync ( filePath ) ) {
105
+ const parentDir = path . resolve ( dir , '..' ) ;
106
+ if ( parentDir === dir ) {
107
+ return null ;
108
+ }
109
+ return this . findConfig ( parentDir ) ;
110
+ }
111
+
112
+ return filePath ;
103
113
}
104
114
105
115
async getInput ( ) {
106
116
const infile = this . args . file || process . stdin . fd ;
107
117
if ( this . args . file ) {
108
- try {
109
- return await this . readFile ( infile , { encoding : 'utf-8' } ) ;
110
- } catch ( e ) {
111
- this . exitWhenIOError ( e ) ;
112
- console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
113
- console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
114
- throw e ;
115
- }
118
+ return await this . readFile ( infile ) ;
116
119
} else {
117
120
return await getStdin ( ) ;
118
121
}
119
122
}
120
123
124
+ async parseFile ( filename ) {
125
+ try {
126
+ return JSON . parse ( await this . readFile ( filename ) ) ;
127
+ } catch ( e ) {
128
+ console . error ( `Error: unable to parse as JSON or treat as JSON file: ${ filename } ` ) ;
129
+ process . exit ( 1 ) ;
130
+ }
131
+ }
132
+
121
133
async readFile ( filename ) {
122
- return promisify ( fs . readFile ) ( filename , { encoding : 'utf-8' } ) ;
134
+ try {
135
+ return promisify ( fs . readFile ) ( filename , { encoding : 'utf-8' } ) ;
136
+ } catch ( e ) {
137
+ this . exitWhenIOError ( e , filename ) ;
138
+ console . error ( 'An unknown error has occurred, please file a bug report at:' ) ;
139
+ console . log ( 'https://github.com/sql-formatter-org/sql-formatter/issues\n' ) ;
140
+ throw e ;
141
+ }
123
142
}
124
143
125
- exitWhenIOError ( e ) {
144
+ exitWhenIOError ( e , infile ) {
126
145
if ( e . code === 'EAGAIN' ) {
127
146
console . error ( 'Error: no file specified and no data in stdin' ) ;
128
147
process . exit ( 1 ) ;
0 commit comments