Skip to content

Commit 15c0f74

Browse files
committed
Simplify reading stdin code
1 parent 122783f commit 15c0f74

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

bin/handler.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import os from 'node:os';
4+
import { stdin } from 'node:process';
45

56
import { pkg, config, configPath, configDownloadPath } from '../src/static.js'
67
import { parse } from '../src/parse.js';
@@ -141,6 +142,7 @@ async function read(path) {
141142
console.log(`(Press ${key} to finish input.)\n`);
142143
try {
143144
content = await readFromStdin();
145+
console.log('\n');
144146
} catch (e) {
145147
error = e;
146148
}
@@ -166,18 +168,10 @@ async function read(path) {
166168
function readFromStdin() {
167169
return new Promise((resolve, reject) => {
168170
let content = '';
169-
process.stdin.setEncoding('utf8');
170-
process.stdin.on('readable', () => {
171-
let chunk;
172-
while ((chunk = process.stdin.read())) {
173-
content += chunk;
174-
}
175-
});
176-
process.stdin.on('end', () => {
177-
console.log('\n');
178-
resolve(content);
179-
});
180-
process.stdin.on('error', reject);
171+
stdin.setEncoding('utf8');
172+
stdin.on('data', c => content += c);
173+
stdin.on('end', () => resolve(content));
174+
stdin.on('error', reject);
181175
});
182176
}
183177

0 commit comments

Comments
 (0)