Skip to content

Commit 76d08f0

Browse files
committed
Merge PR #621: fix EAGAIN error for stdin
2 parents 0d4787c + 0a94042 commit 76d08f0

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ For those who have admin access on the repo, the new release publish flow is as
5858
- Romain Rigaux <[email protected]>
5959
- Sasha Aliashkevich <[email protected]>
6060
- Sean Song <[email protected]>
61+
- Sebastian Lyng Johansen <[email protected]>
6162
- Sergei Egorov <[email protected]>
6263
- Stanislav Germanovskii <[email protected]>
6364
- Steven Yung <[email protected]>

bin/sql-formatter-cli.cjs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ const fs = require('fs');
77
const tty = require('tty');
88
const { version } = require('../package.json');
99
const { ArgumentParser } = require('argparse');
10+
const { promisify } = require('util');
11+
const getStdin = require('get-stdin');
1012

11-
class PrettierSQLArgs {
13+
class SqlFormatterCli {
1214
constructor() {
1315
this.parser = this.getParser();
1416
this.args = this.parser.parse_args();
15-
this.cfg = this.readConfig();
17+
}
1618

17-
this.query = this.getInput();
19+
async run() {
20+
this.cfg = await this.readConfig();
21+
this.query = await this.getInput();
1822
const formattedQuery = format(this.query, this.cfg).trim() + '\n';
1923
this.writeOutput(this.getOutputFile(this.args), formattedQuery);
2024
}
@@ -59,7 +63,7 @@ class PrettierSQLArgs {
5963
return parser;
6064
}
6165

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

71-
if (this.args.config)
75+
if (this.args.config) {
7276
try {
73-
const configFile = fs.readFileSync(this.args.config);
77+
const configFile = await this.readFile(this.args.config);
7478
const configJson = JSON.parse(configFile);
7579
return { language: this.args.language, ...configJson };
7680
} catch (e) {
7781
if (e instanceof SyntaxError) {
7882
console.error(`Error: unable to parse JSON at file ${this.args.config}`);
7983
process.exit(1);
8084
}
81-
if (e.code === 'ENOENT') {
82-
console.error(`Error: could not open file ${this.args.config}`);
83-
process.exit(1);
84-
}
85+
this.exitWhenIOError(e);
8586
console.error('An unknown error has occurred, please file a bug report at:');
8687
console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n');
8788
throw e;
8889
}
90+
}
8991
return {
9092
language: this.args.language,
9193
};
9294
}
9395

94-
getInput() {
96+
async getInput() {
9597
const infile = this.args.file || process.stdin.fd;
96-
try {
97-
return fs.readFileSync(infile, 'utf-8');
98-
} catch (e) {
99-
if (e.code === 'EAGAIN') {
100-
console.error('Error: no file specified and no data in stdin');
101-
process.exit(1);
102-
}
103-
if (e.code === 'ENOENT') {
104-
console.error(`Error: could not open file ${infile}`);
105-
process.exit(1);
98+
if (this.args.file) {
99+
try {
100+
return await this.readFile(infile, { encoding: 'utf-8' });
101+
} catch (e) {
102+
this.exitWhenIOError(e);
103+
console.error('An unknown error has occurred, please file a bug report at:');
104+
console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n');
105+
throw e;
106106
}
107-
console.error('An unknown error has occurred, please file a bug report at:');
108-
console.log('https://github.com/sql-formatter-org/sql-formatter/issues\n');
109-
throw e;
107+
} else {
108+
return await getStdin();
109+
}
110+
}
111+
112+
async readFile(filename) {
113+
return promisify(fs.readFile)(filename, { encoding: 'utf-8' });
114+
}
115+
116+
exitWhenIOError(e) {
117+
if (e.code === 'EAGAIN') {
118+
console.error('Error: no file specified and no data in stdin');
119+
process.exit(1);
120+
}
121+
if (e.code === 'ENOENT') {
122+
console.error(`Error: could not open file ${infile}`);
123+
process.exit(1);
110124
}
111125
}
112126

@@ -136,4 +150,5 @@ class PrettierSQLArgs {
136150
}
137151
}
138152

139-
new PrettierSQLArgs();
153+
const cli = new SqlFormatterCli();
154+
cli.run();

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"Rodrigo Stuchi",
7979
"Romain Rigaux <[email protected]>",
8080
"Sasha Aliashkevich <[email protected]>",
81+
"Sebastian Lyng Johansen <[email protected]>",
8182
"Sean Song <[email protected]>",
8283
"Sergei Egorov <[email protected]>",
8384
"Stanislav Germanovskii <[email protected]>",
@@ -125,6 +126,7 @@
125126
},
126127
"dependencies": {
127128
"argparse": "^2.0.1",
129+
"get-stdin": "=8.0.0",
128130
"nearley": "^2.20.1"
129131
},
130132
"devDependencies": {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,6 +3500,11 @@ get-package-type@^0.1.0:
35003500
resolved "https://registry.yarnpkg.com/get-package-type/-/get-package-type-0.1.0.tgz#8de2d803cff44df3bc6c456e6668b36c3926e11a"
35013501
integrity sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==
35023502

3503+
get-stdin@=8.0.0:
3504+
version "8.0.0"
3505+
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-8.0.0.tgz#cbad6a73feb75f6eeb22ba9e01f89aa28aa97a53"
3506+
integrity sha512-sY22aA6xchAzprjyqmSEQv4UbAAzRN0L2dQB0NlN5acTTK9Don6nhoc3eAbUnpZiCANAMfd/+40kVdKfFygohg==
3507+
35033508
get-stream@^5.1.0:
35043509
version "5.2.0"
35053510
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-5.2.0.tgz#4966a1795ee5ace65e706c4b7beb71257d6e22d3"

0 commit comments

Comments
 (0)