Skip to content

Commit cddd575

Browse files
committed
fix(core): cancel prompts when the input stream ends
1 parent f325a53 commit cddd575

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎.changeset/quiet-pipes-end.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/core": patch
3+
---
4+
5+
Cancel a prompt when its input stream ends (e.g. stdin is `/dev/null` or piped input runs out) instead of leaving the returned promise pending forever.

‎packages/core/src/prompts/prompt.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default class Prompt<TValue> {
7070

7171
this.opts = opts;
7272
this.onKeypress = this.onKeypress.bind(this);
73+
this.onInputEnd = this.onInputEnd.bind(this);
7374
this.close = this.close.bind(this);
7475
this.render = this.render.bind(this);
7576
this._render = render.bind(this);
@@ -194,9 +195,27 @@ export default class Prompt<TValue> {
194195
setRawMode(this.input, false);
195196
resolve(CANCEL_SYMBOL);
196197
});
198+
199+
// No answer can arrive once the input has ended (e.g. stdin is
200+
// `/dev/null` or a pipe that ran out), so cancel instead of hanging.
201+
if (this.input.readableEnded) {
202+
this.onInputEnd();
203+
} else {
204+
this.input.once('end', this.onInputEnd);
205+
}
197206
});
198207
}
199208

209+
private onInputEnd() {
210+
if (this.state === 'submit' || this.state === 'cancel') {
211+
return;
212+
}
213+
this.state = 'cancel';
214+
this.emit('finalize');
215+
this.render();
216+
this.close();
217+
}
218+
200219
protected _isActionKey(char: string | undefined, _key: Key): boolean {
201220
return char === '\t';
202221
}
@@ -293,6 +312,7 @@ export default class Prompt<TValue> {
293312
protected close() {
294313
this.input.unpipe();
295314
this.input.removeListener('keypress', this.onKeypress);
315+
this.input.removeListener('end', this.onInputEnd);
296316
this.output.write('\n');
297317
setRawMode(this.input, false);
298318
this.rl?.close();

‎packages/core/test/prompts/prompt.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,54 @@ describe('Prompt', () => {
236236
expect(instance.state).to.equal('cancel');
237237
});
238238

239+
test('cancels when the input ends', async () => {
240+
const instance = new Prompt({
241+
input,
242+
output,
243+
render: () => 'foo',
244+
});
245+
const resultPromise = instance.prompt();
246+
input.close();
247+
input.resume();
248+
249+
const result = await resultPromise;
250+
expect(isCancel(result)).to.equal(true);
251+
expect(instance.state).to.equal('cancel');
252+
expect(output.buffer.at(-1)).to.equal(cursor.show);
253+
expect(input.listenerCount('end')).to.equal(0);
254+
});
255+
256+
test('cancels immediately if the input has already ended', async () => {
257+
input.close();
258+
input.resume();
259+
await new Promise((resolve) => input.once('end', resolve));
260+
261+
const instance = new Prompt({
262+
input,
263+
output,
264+
render: () => 'foo',
265+
});
266+
const result = await instance.prompt();
267+
expect(isCancel(result)).to.equal(true);
268+
expect(instance.state).to.equal('cancel');
269+
});
270+
271+
test('does not cancel a submitted prompt when the input ends afterwards', async () => {
272+
const instance = new Prompt({
273+
input,
274+
output,
275+
render: () => 'foo',
276+
});
277+
const resultPromise = instance.prompt();
278+
input.emit('keypress', '', { name: 'return' });
279+
input.close();
280+
input.resume();
281+
282+
const result = await resultPromise;
283+
expect(isCancel(result)).to.equal(false);
284+
expect(instance.state).to.equal('submit');
285+
});
286+
239287
describe('function validation', () => {
240288
test('accepts invalid initial value', () => {
241289
const instance = new Prompt({

0 commit comments

Comments
 (0)