Skip to content

Commit

Permalink
fix EAGAIN error for stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Lyng Johansen committed Jul 11, 2023
1 parent d6c830c commit fc2e970
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions bin/sql-formatter-cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ const fs = require('fs');
const tty = require('tty');
const { version } = require('../package.json');
const { ArgumentParser } = require('argparse');
const { promisify } = require('util');

class PrettierSQLArgs {
constructor() {
this.parser = this.getParser();
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() {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit fc2e970

Please sign in to comment.