enquirer: esc is fine, but after first prompt SIGINT will be ignore; onRawSIGINT keeps process running -> switch to inquirer

This commit is contained in:
Ralf Vogler 2025-05-14 00:30:39 +02:00
parent 97ef14f514
commit 6aea18836d
3 changed files with 35 additions and 8 deletions

View file

@ -0,0 +1,21 @@
// open issue: prevents handleSIGINT() to work if prompt is cancelled with Ctrl-C instead of Escape: https://github.com/enquirer/enquirer/issues/372
function onRawSIGINT(fn) {
const { stdin, stdout } = process;
stdin.setRawMode(true);
stdin.resume();
stdin.on('data', data => {
const key = data.toString('utf-8');
if (key === '\u0003') { // ctrl + c
fn();
} else {
stdout.write(key);
}
});
}
console.log(1)
onRawSIGINT(() => {
console.log('raw'); process.exit(1);
});
console.log(2)
// onRawSIGINT workaround for enquirer keeps the process from exiting here...

View file

@ -1,10 +1,10 @@
// https://github.com/enquirer/enquirer/issues/372 // https://github.com/enquirer/enquirer/issues/372
import { prompt } from '../src/util.js'; import { prompt, handleSIGINT } from '../src/util.js';
const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C // const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
console.log('\nInterrupted by SIGINT. Exit!'); // console.log('\nInterrupted by SIGINT. Exit!');
process.exitCode = 130; // process.exitCode = 130;
}); // });
handleSIGINT(); handleSIGINT();
function onRawSIGINT(fn) { function onRawSIGINT(fn) {
@ -20,15 +20,16 @@ function onRawSIGINT(fn) {
} }
}); });
} }
onRawSIGINT(() => { // onRawSIGINT(() => {
console.log('raw'); process.exit(1); // console.log('raw'); process.exit(1);
}); // });
console.log('hello'); console.log('hello');
console.error('hello error'); console.error('hello error');
try { try {
let i = 'foo'; let i = 'foo';
i = await prompt(); // SIGINT no longer handled if this is executed i = await prompt(); // SIGINT no longer handled if this is executed
i = await prompt(); // SIGINT no longer handled if this is executed
// handleSIGINT(); // handleSIGINT();
console.log('value:', i); console.log('value:', i);
setTimeout(() => console.log('timeout 3s'), 3000); setTimeout(() => console.log('timeout 3s'), 3000);

View file

@ -13,3 +13,8 @@ await enquirer.prompt({
name: 'username', name: 'username',
message: 'What is your username?', message: 'What is your username?',
}); });
await enquirer.prompt({
type: 'input',
name: 'username',
message: 'What is your username 2?',
});