From dc9f936754f2388cdba5eb6ac98c3f2ba70b3b17 Mon Sep 17 00:00:00 2001 From: Jonathan Schuster <107444983+schusj@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:55:07 +1000 Subject: [PATCH 1/3] Support named config file --- README.md | 4 +- bin/sql-formatter-cli.cjs | 81 +++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 64fcec73c4..80bd69e5b4 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ optional arguments: -l, --language {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,tidb,trino,tsql} SQL dialect (defaults to basic sql) -c, --config CONFIG - Path to config JSON file or json string (will use default configs if unspecified) + Path to config JSON file or json string (will find a file named '.sql-formatter.json' or use default configs if unspecified) --version show program's version number and exit ``` @@ -158,7 +158,7 @@ where id = 3 ``` -The tool also accepts a JSON config file with the `--config` option that takes this form: +The tool also accepts a JSON config file named .sql-formatter.json in the current or any parent directory, or with the `--config` option that takes this form: ```json { diff --git a/bin/sql-formatter-cli.cjs b/bin/sql-formatter-cli.cjs index 6eb8339067..5530d55d71 100755 --- a/bin/sql-formatter-cli.cjs +++ b/bin/sql-formatter-cli.cjs @@ -4,6 +4,7 @@ const { format, supportedDialects } = require('../dist/index.cjs'); const fs = require('fs'); +const path = require('path'); const tty = require('tty'); const { version } = require('../package.json'); const { ArgumentParser } = require('argparse'); @@ -52,7 +53,7 @@ class SqlFormatterCli { }); parser.add_argument('-c', '--config', { - help: 'Path to config JSON file or json string (will use default configs if unspecified)', + help: 'Path to config JSON file or json string (will find a file named \'.sql-formatter.json\' or use default configs if unspecified)', }); parser.add_argument('--version', { @@ -72,57 +73,73 @@ class SqlFormatterCli { process.exit(0); } + return { + language: this.args.language, + ...(await this.getConfig()), + }; + } + + async getConfig() { if (this.args.config) { // First, try to parse --config value as a JSON string try { - const configJson = JSON.parse(this.args.config); - return { language: this.args.language, ...configJson }; + return JSON.parse(this.args.config); } catch (e) { // If that fails, try to read the --config value as a file - try { - const configFile = await this.readFile(this.args.config); - const configJson = JSON.parse(configFile); - return { language: this.args.language, ...configJson }; - } catch (e) { - if (e instanceof SyntaxError) { - console.error( - `Error: unable to parse as JSON or treat as JSON file: ${this.args.config}` - ); - process.exit(1); - } - this.exitWhenIOError(e); - console.error('An unknown error has occurred, please file a bug report at:'); - console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n'); - throw e; - } + return this.parseFile(this.args.config); } } - return { - language: this.args.language, - }; + + // Otherwise find a local config file + const localConfig = await this.findConfig(); + if (!localConfig) { + return null; + } + + return this.parseFile(localConfig); + } + + async findConfig(dir = __dirname) { + const filePath = path.join(dir, '.sql-formatter.json'); + if (!fs.existsSync(filePath)) { + const parentDir = path.resolve(dir, '..'); + if (parentDir === dir) { + return null; + } + return await this.findConfig(parentDir); + } + + return filePath; } async getInput() { const infile = this.args.file || process.stdin.fd; if (this.args.file) { - try { - return await this.readFile(infile, { encoding: 'utf-8' }); - } catch (e) { - this.exitWhenIOError(e); - console.error('An unknown error has occurred, please file a bug report at:'); - console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n'); - throw e; - } + return await this.readFile(infile); } else { return await getStdin(); } } + async parseFile(filename) { + try { + return JSON.parse(await this.readFile(filename)); + } catch (e) { + console.error(`Error: unable to parse as JSON or treat as JSON file: ${filename}`); + process.exit(1); + } + } + async readFile(filename) { - return promisify(fs.readFile)(filename, { encoding: 'utf-8' }); + return promisify(fs.readFile)(filename, { encoding: 'utf-8' }).catch(e => { + this.exitWhenIOError(e, filename); + console.error('An unknown error has occurred, please file a bug report at:'); + console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n'); + throw e; + }); } - exitWhenIOError(e) { + exitWhenIOError(e, infile) { if (e.code === 'EAGAIN') { console.error('Error: no file specified and no data in stdin'); process.exit(1); From 3518fccb68d35ce1398849711b52eef05f355a77 Mon Sep 17 00:00:00 2001 From: Jonathan Schuster <107444983+schusj@users.noreply.github.com> Date: Fri, 9 Aug 2024 18:25:36 +1000 Subject: [PATCH 2/3] Resolve comments --- bin/sql-formatter-cli.cjs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bin/sql-formatter-cli.cjs b/bin/sql-formatter-cli.cjs index 5530d55d71..e3f7383e0b 100755 --- a/bin/sql-formatter-cli.cjs +++ b/bin/sql-formatter-cli.cjs @@ -99,14 +99,14 @@ class SqlFormatterCli { return this.parseFile(localConfig); } - async findConfig(dir = __dirname) { + findConfig(dir = __dirname) { const filePath = path.join(dir, '.sql-formatter.json'); if (!fs.existsSync(filePath)) { const parentDir = path.resolve(dir, '..'); if (parentDir === dir) { return null; } - return await this.findConfig(parentDir); + return this.findConfig(parentDir); } return filePath; @@ -131,12 +131,19 @@ class SqlFormatterCli { } async readFile(filename) { - return promisify(fs.readFile)(filename, { encoding: 'utf-8' }).catch(e => { + try { + + + return promisify(fs.readFile)(filename, { encoding: 'utf-8' }) + } catch (e) { + + this.exitWhenIOError(e, filename); console.error('An unknown error has occurred, please file a bug report at:'); console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n'); throw e; - }); + + } } exitWhenIOError(e, infile) { From 4fd1163d430b46984257829fd0666e69d373532d Mon Sep 17 00:00:00 2001 From: Jonathan Schuster <107444983+schusj@users.noreply.github.com> Date: Fri, 9 Aug 2024 18:28:31 +1000 Subject: [PATCH 3/3] Format --- bin/sql-formatter-cli.cjs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/bin/sql-formatter-cli.cjs b/bin/sql-formatter-cli.cjs index e3f7383e0b..055cd1b543 100755 --- a/bin/sql-formatter-cli.cjs +++ b/bin/sql-formatter-cli.cjs @@ -132,17 +132,12 @@ class SqlFormatterCli { async readFile(filename) { try { - - - return promisify(fs.readFile)(filename, { encoding: 'utf-8' }) - } catch (e) { - - + return promisify(fs.readFile)(filename, { encoding: 'utf-8' }); + } catch (e) { this.exitWhenIOError(e, filename); console.error('An unknown error has occurred, please file a bug report at:'); console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n'); throw e; - } }