From fc2e970645c53bdb5c3f1e2de5f932067d969aee Mon Sep 17 00:00:00 2001 From: Sebastian Lyng Johansen Date: Tue, 11 Jul 2023 00:09:22 +0200 Subject: [PATCH 1/2] fix EAGAIN error for stdin --- bin/sql-formatter-cli.cjs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/sql-formatter-cli.cjs b/bin/sql-formatter-cli.cjs index 358f7d59cf..c2fd3471c5 100755 --- a/bin/sql-formatter-cli.cjs +++ b/bin/sql-formatter-cli.cjs @@ -7,6 +7,7 @@ const fs = require('fs'); const tty = require('tty'); const { version } = require('../package.json'); const { ArgumentParser } = require('argparse'); +const { promisify } = require('util'); class PrettierSQLArgs { constructor() { @@ -14,9 +15,12 @@ class PrettierSQLArgs { this.args = this.parser.parse_args(); this.cfg = this.readConfig(); - this.query = this.getInput(); - const formattedQuery = format(this.query, this.cfg).trim() + '\n'; - this.writeOutput(this.getOutputFile(this.args), formattedQuery); + this.readFile = promisify(fs.readFile); + this.getInput().then(input => { + this.query = input; + const formattedQuery = format(this.query, this.cfg).trim() + '\n'; + this.writeOutput(this.getOutputFile(this.args), formattedQuery); + }); } getParser() { @@ -91,10 +95,10 @@ class PrettierSQLArgs { }; } - getInput() { + async getInput() { const infile = this.args.file || process.stdin.fd; try { - return fs.readFileSync(infile, 'utf-8'); + return await this.readFile(infile, { encoding: 'utf-8' }); } catch (e) { if (e.code === 'EAGAIN') { console.error('Error: no file specified and no data in stdin'); From b4136837753cd8a4223ee06781f83c251ac2ec51 Mon Sep 17 00:00:00 2001 From: Sebastian Lyng Johansen Date: Tue, 11 Jul 2023 17:33:48 +0200 Subject: [PATCH 2/2] use get-stdin impl of getStdin --- bin/sql-formatter-cli.cjs | 46 ++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/bin/sql-formatter-cli.cjs b/bin/sql-formatter-cli.cjs index c2fd3471c5..c2940f4f42 100755 --- a/bin/sql-formatter-cli.cjs +++ b/bin/sql-formatter-cli.cjs @@ -23,6 +23,22 @@ class PrettierSQLArgs { }); } + async getStdin() { + if (process.stdin.isTTY) { + return Buffer.alloc(0); + } + + const result = []; + let length = 0; + + for await (const chunk of process.stdin) { + result.push(chunk); + length += chunk.length; + } + + return Buffer.concat(result, length).toString(); + } + getParser() { const parser = new ArgumentParser({ add_help: true, @@ -97,20 +113,24 @@ class PrettierSQLArgs { async getInput() { const infile = this.args.file || process.stdin.fd; - try { - return await this.readFile(infile, { encoding: 'utf-8' }); - } catch (e) { - if (e.code === 'EAGAIN') { - console.error('Error: no file specified and no data in stdin'); - process.exit(1); - } - if (e.code === 'ENOENT') { - console.error(`Error: could not open file ${infile}`); - process.exit(1); + if (this.args.file) { + try { + return await this.readFile(infile, { encoding: 'utf-8' }); + } catch (e) { + if (e.code === 'EAGAIN') { + console.error('Error: no file specified and no data in stdin'); + process.exit(1); + } + if (e.code === 'ENOENT') { + console.error(`Error: could not open file ${infile}`); + process.exit(1); + } + 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; } - 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; + } else { + return await this.getStdin(); } }