Skip to content

Commit

Permalink
Merge PR #621: fix EAGAIN error for stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jul 23, 2023
2 parents 0d4787c + 0a94042 commit 76d08f0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ For those who have admin access on the repo, the new release publish flow is as
- Romain Rigaux <hello@getromain.com>
- Sasha Aliashkevich <olsender@gmail.com>
- Sean Song <mail@seansong.dev>
- Sebastian Lyng Johansen <seblyng98@gmail.com>
- Sergei Egorov <sergei.egorov@zeroturnaround.com>
- Stanislav Germanovskii <s.germanovskiy@tinkoff.ru>
- Steven Yung <stevenyung@fastmail.com>
Expand Down
65 changes: 40 additions & 25 deletions bin/sql-formatter-cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ const fs = require('fs');
const tty = require('tty');
const { version } = require('../package.json');
const { ArgumentParser } = require('argparse');
const { promisify } = require('util');
const getStdin = require('get-stdin');

class PrettierSQLArgs {
class SqlFormatterCli {
constructor() {
this.parser = this.getParser();
this.args = this.parser.parse_args();
this.cfg = this.readConfig();
}

this.query = this.getInput();
async run() {
this.cfg = await this.readConfig();
this.query = await this.getInput();
const formattedQuery = format(this.query, this.cfg).trim() + '\n';
this.writeOutput(this.getOutputFile(this.args), formattedQuery);
}
Expand Down Expand Up @@ -59,7 +63,7 @@ class PrettierSQLArgs {
return parser;
}

readConfig() {
async readConfig() {
if (
tty.isatty(0) &&
Object.entries(this.args).every(([k, v]) => k === 'language' || v === undefined)
Expand All @@ -68,45 +72,55 @@ class PrettierSQLArgs {
process.exit(0);
}

if (this.args.config)
if (this.args.config) {
try {
const configFile = fs.readFileSync(this.args.config);
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 JSON at file ${this.args.config}`);
process.exit(1);
}
if (e.code === 'ENOENT') {
console.error(`Error: could not open 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 {
language: this.args.language,
};
}

getInput() {
async getInput() {
const infile = this.args.file || process.stdin.fd;
try {
return fs.readFileSync(infile, '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) {
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;
}
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 getStdin();
}
}

async readFile(filename) {
return promisify(fs.readFile)(filename, { encoding: 'utf-8' });
}

exitWhenIOError(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);
}
}

Expand Down Expand Up @@ -136,4 +150,5 @@ class PrettierSQLArgs {
}
}

new PrettierSQLArgs();
const cli = new SqlFormatterCli();
cli.run();
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"Rodrigo Stuchi",
"Romain Rigaux <hello@getromain.com>",
"Sasha Aliashkevich <olsender@gmail.com>",
"Sebastian Lyng Johansen <seblyng98@gmail.com>",
"Sean Song <mail@seansong.dev>",
"Sergei Egorov <sergei.egorov@zeroturnaround.com>",
"Stanislav Germanovskii <s.germanovskiy@tinkoff.ru>",
Expand Down Expand Up @@ -125,6 +126,7 @@
},
"dependencies": {
"argparse": "^2.0.1",
"get-stdin": "=8.0.0",
"nearley": "^2.20.1"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3500,6 +3500,11 @@ get-package-type@^0.1.0:
resolved "https://registry.yarnpkg.com/get-package-type/-/get-package-type-0.1.0.tgz#8de2d803cff44df3bc6c456e6668b36c3926e11a"
integrity sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==

get-stdin@=8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-8.0.0.tgz#cbad6a73feb75f6eeb22ba9e01f89aa28aa97a53"
integrity sha512-sY22aA6xchAzprjyqmSEQv4UbAAzRN0L2dQB0NlN5acTTK9Don6nhoc3eAbUnpZiCANAMfd/+40kVdKfFygohg==

get-stream@^5.1.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-5.2.0.tgz#4966a1795ee5ace65e706c4b7beb71257d6e22d3"
Expand Down

0 comments on commit 76d08f0

Please sign in to comment.